File: tcp_client_server.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (152 lines) | stat: -rw-r--r-- 4,561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*!
 * \file
 * \brief Definitions of TCP Client and Server Applications
 * \author Krister Norlund
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/protocol/tcp.h>


namespace itpp {

  //! \addtogroup protocol
  //@{

  /*! ADD DOCUMENTATION HERE

   */
  class TCP_Server_Application {
  public:
    //! Default constructor
    TCP_Server_Application() {
      write.set_name("TcpServerApplicationWriteSignal");
      write.set_debug();
    }
    //! Destructor
    ~TCP_Server_Application() { }

    //! ADD DOCUMENTATION HERE
    Signal<itpp::Packet*> write;

    //! ADD DOCUMENTATION HERE
    void write_to_net(unsigned byte_size, double delta_time) {
      itpp::Packet *packet = new Packet(8*byte_size);
      write(packet, delta_time);

      std::cout << "TcpServerApplication::write_to_net,"
                << " byte_size=" << packet->bit_size()/8
                << " ptr=" << packet
                << " time=" << Event_Queue::now() << std::endl;
    }
  };

  /*! ADD DOCUMENTATION HERE

   */
  class TCP_Client_Application {
  public:
    //! Default constructor
    TCP_Client_Application(TCP_Sender *tcp_snd_p, TCP_Receiver *tcp_recv_p) {
      tcp_receiver_p = tcp_recv_p;
      tcp_sender_p = tcp_snd_p;
      nbr_bytes_received = 0;
      select.forward(this, &TCP_Client_Application::received_packet_indication);
      select.set_name("TcpClientApplicationSelectSlot");
      seq_num_index = 0;
    }

    //! Destructor
    ~TCP_Client_Application() { }

    //! ADD DOCUMENTATION HERE
    Slot<TCP_Client_Application, int> select;

    //! ADD DOCUMENTATION HERE
    void read_from_net(unsigned byte_size) {
      nbr_bytes_to_receive = byte_size;
      seq_num_val.set_size(10+byte_size/1460);
      seq_num_val.zeros();
      seq_num_time.set_size(10+byte_size/1460);
      seq_num_time.zeros();
      seq_num_val(0) = 0;
      seq_num_time(0) = 0;
      seq_num_index=1;
    };

  private:
    TCP_Receiver *tcp_receiver_p;
    TCP_Sender *tcp_sender_p;
    unsigned nbr_bytes_received;
    unsigned nbr_bytes_to_receive;

    vec seq_num_val;
    vec seq_num_time;
    int seq_num_index;

    void TCP_Client_Application::received_packet_indication(int label) {

      itpp::Packet &packet = tcp_receiver_p->get_user_message();
      nbr_bytes_received = nbr_bytes_received + packet.bit_size()/8;
      delete &packet;

      if (seq_num_index >= seq_num_time.size()) {
        seq_num_time.set_size(2*seq_num_time.size(),true);
        seq_num_val.set_size(2*seq_num_val.size(),true);
      }

      seq_num_val(seq_num_index) = nbr_bytes_received;
      seq_num_time(seq_num_index) = Event_Queue::now();
      seq_num_index++;

      std::cout << "### sequence number: " << nbr_bytes_received
                << " ### time:" << Event_Queue::now() << std::endl;

      if (nbr_bytes_received >= nbr_bytes_to_receive) {
        std::cout << "###### Stop sender and receiver" << std::endl;
        tcp_receiver_p->release();
        tcp_sender_p->release();
        tcp_sender_p->save_trace("seq_num.it");
        seq_num_val.set_size(seq_num_index, true);
        seq_num_time.set_size(seq_num_index,true);
        save_to_file("seq_num.it");
      }
    }

    void TCP_Client_Application::save_to_file(string file) {

      it_file ff2(file);
      ff2 << Name("seq_num_val") << seq_num_val;
      ff2 << Name("seq_num_time") << seq_num_time;
      ff2 << Name("seq_num_index") << seq_num_index;
      ff2.flush();
      ff2.close();
    }

  };

  //@}

} // namespace itpp