tcp_client_server.h
Go to the documentation of this file.
#ifndef TCP_CLIENT_SERVER_H
#define TCP_CLIENT_SERVER_H
#include <itpp/itexports.h>
#if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB) && !defined(itpp_EXPORTS))
#ifndef ITPP_PROTOCOL_EXCLUDED
#define ITPP_PROTOCOL_EXCLUDED
#pragma message( "PROTOCOL definitions are not available for MSVC shared builds" )
#endif
#else
#include <itpp/protocol/tcp.h>
namespace itpp
{
class TCP_Server_Application
{
public:
TCP_Server_Application() {
write.set_name("TcpServerApplicationWriteSignal");
write.set_debug();
}
~TCP_Server_Application() { }
Signal<itpp::Packet*> write;
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;
}
};
class TCP_Client_Application
{
public:
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;
}
~TCP_Client_Application() { }
Slot<TCP_Client_Application, int> select;
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
#endif
#endif //TCP_CLIENT_SERVER_H
itpp::Signal::set_debug
void set_debug(const bool enable_debug=true)
Set debug mode. If true all signals are printed to stdout.
Definition: signals_slots.h:425
itpp::TCP_Sender::release
virtual void release(std::string trace_filename="")
ADD DOCUMENTATION HERE.
itpp::Signal< itpp::Packet * >
itpp
itpp namespace
Definition: itmex.h:36
itpp::TCP_Receiver
Definition: tcp.h:582
itpp::TCP_Server_Application::write_to_net
void write_to_net(unsigned byte_size, double delta_time)
ADD DOCUMENTATION HERE.
Definition: tcp_client_server.h:69
itpp::TCP_Sender
Definition: tcp.h:323
itpp::TCP_Receiver::release
virtual void release(std::string trace_filename="")
ADD DOCUMENTATION HERE.
itpp::TCP_Client_Application::~TCP_Client_Application
~TCP_Client_Application()
Destructor.
Definition: tcp_client_server.h:97
itpp::TCP_Server_Application::~TCP_Server_Application
~TCP_Server_Application()
Destructor.
Definition: tcp_client_server.h:63
itpp::Signal::set_name
void set_name(const std::string &signal_name)
set name of signal
Definition: signals_slots.h:419
itpp::Slot
Slot Class.
Definition: signals_slots.h:57
itpp::TCP_Client_Application::TCP_Client_Application
TCP_Client_Application(TCP_Sender *tcp_snd_p, TCP_Receiver *tcp_recv_p)
Default constructor.
Definition: tcp_client_server.h:87
itpp::TCP_Client_Application::read_from_net
void read_from_net(unsigned byte_size)
ADD DOCUMENTATION HERE.
Definition: tcp_client_server.h:103
itpp::TCP_Sender::save_trace
virtual void save_trace(std::string filename)
ADD DOCUMENTATION HERE.
itpp::TCP_Client_Application
Definition: tcp_client_server.h:83
itpp::TCP_Client_Application::select
Slot< TCP_Client_Application, int > select
ADD DOCUMENTATION HERE.
Definition: tcp_client_server.h:100
itpp::TCP_Server_Application::write
Signal< itpp::Packet * > write
ADD DOCUMENTATION HERE.
Definition: tcp_client_server.h:66
itpp::TCP_Server_Application
Definition: tcp_client_server.h:54
tcp.h
Definition of Transport Control Protocol (TCP)
itpp::TCP_Server_Application::TCP_Server_Application
TCP_Server_Application()
Default constructor.
Definition: tcp_client_server.h:58
itpp::Packet
Definition: packet.h:55
itpp::TCP_Receiver::get_user_message
itpp::Packet & get_user_message()
called by higher layer
itpp::Packet::bit_size
int bit_size()
get size of packet in bits
Definition: packet.h:67
itpp::Event_Queue::now
static Ttype now()
Return current time.
Definition: events.h:139

Generated on Mon Mar 23 2020 06:18:03 for IT++ by Doxygen 1.8.17