IT++ Logo Newcom Logo

tcp_client_server.h

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

Generated on Thu Apr 19 14:23:59 2007 for IT++ by Doxygen 1.4.6