00001
00029 #include <itpp/protocol/tcp.h>
00030
00031
00032 namespace itpp
00033 {
00034
00036
00037
00041 class TCP_Server_Application
00042 {
00043 public:
00045 TCP_Server_Application() {
00046 write.set_name("TcpServerApplicationWriteSignal");
00047 write.set_debug();
00048 }
00050 ~TCP_Server_Application() { }
00051
00053 Signal<itpp::Packet*> write;
00054
00056 void write_to_net(unsigned byte_size, double delta_time) {
00057 itpp::Packet *packet = new Packet(8*byte_size);
00058 write(packet, delta_time);
00059
00060 std::cout << "TcpServerApplication::write_to_net,"
00061 << " byte_size=" << packet->bit_size() / 8
00062 << " ptr=" << packet
00063 << " time=" << Event_Queue::now() << std::endl;
00064 }
00065 };
00066
00070 class TCP_Client_Application
00071 {
00072 public:
00074 TCP_Client_Application(TCP_Sender *tcp_snd_p, TCP_Receiver *tcp_recv_p) {
00075 tcp_receiver_p = tcp_recv_p;
00076 tcp_sender_p = tcp_snd_p;
00077 nbr_bytes_received = 0;
00078 select.forward(this, &TCP_Client_Application::received_packet_indication);
00079 select.set_name("TcpClientApplicationSelectSlot");
00080 seq_num_index = 0;
00081 }
00082
00084 ~TCP_Client_Application() { }
00085
00087 Slot<TCP_Client_Application, int> select;
00088
00090 void read_from_net(unsigned byte_size) {
00091 nbr_bytes_to_receive = byte_size;
00092 seq_num_val.set_size(10 + byte_size / 1460);
00093 seq_num_val.zeros();
00094 seq_num_time.set_size(10 + byte_size / 1460);
00095 seq_num_time.zeros();
00096 seq_num_val(0) = 0;
00097 seq_num_time(0) = 0;
00098 seq_num_index = 1;
00099 };
00100
00101 private:
00102 TCP_Receiver *tcp_receiver_p;
00103 TCP_Sender *tcp_sender_p;
00104 unsigned nbr_bytes_received;
00105 unsigned nbr_bytes_to_receive;
00106
00107 vec seq_num_val;
00108 vec seq_num_time;
00109 int seq_num_index;
00110
00111 void TCP_Client_Application::received_packet_indication(int label) {
00112
00113 itpp::Packet &packet = tcp_receiver_p->get_user_message();
00114 nbr_bytes_received = nbr_bytes_received + packet.bit_size() / 8;
00115 delete &packet;
00116
00117 if (seq_num_index >= seq_num_time.size()) {
00118 seq_num_time.set_size(2*seq_num_time.size(), true);
00119 seq_num_val.set_size(2*seq_num_val.size(), true);
00120 }
00121
00122 seq_num_val(seq_num_index) = nbr_bytes_received;
00123 seq_num_time(seq_num_index) = Event_Queue::now();
00124 seq_num_index++;
00125
00126 std::cout << "### sequence number: " << nbr_bytes_received
00127 << " ### time:" << Event_Queue::now() << std::endl;
00128
00129 if (nbr_bytes_received >= nbr_bytes_to_receive) {
00130 std::cout << "###### Stop sender and receiver" << std::endl;
00131 tcp_receiver_p->release();
00132 tcp_sender_p->release();
00133 tcp_sender_p->save_trace("seq_num.it");
00134 seq_num_val.set_size(seq_num_index, true);
00135 seq_num_time.set_size(seq_num_index, true);
00136 save_to_file("seq_num.it");
00137 }
00138 }
00139
00140 void TCP_Client_Application::save_to_file(string file) {
00141
00142 it_file ff2(file);
00143 ff2 << Name("seq_num_val") << seq_num_val;
00144 ff2 << Name("seq_num_time") << seq_num_time;
00145 ff2 << Name("seq_num_index") << seq_num_index;
00146 ff2.flush();
00147 ff2.close();
00148 }
00149
00150 };
00151
00153
00154 }