| 1 |
#include "protoApp.h" |
| 2 |
#include "protoSocket.h" |
| 3 |
|
| 4 |
class UdpTest : public ProtoApp |
| 5 |
{ |
| 6 |
public: |
| 7 |
UdpTest(); |
| 8 |
~UdpTest(); |
| 9 |
|
| 10 |
/** |
| 11 |
* Override from ProtoApp or NsProtoSimAgent base |
| 12 |
*/ |
| 13 |
bool OnStartup(int argc, const char*const* argv); |
| 14 |
/** |
| 15 |
* Override from ProtoApp or NsProtoSimAgent base |
| 16 |
*/ |
| 17 |
bool ProcessCommands(int argc, const char*const* argv) {return true;} |
| 18 |
/** |
| 19 |
* Override from ProtoApp or NsProtoSimAgent base |
| 20 |
*/ |
| 21 |
void OnShutdown(); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private: |
| 26 |
void OnTxTimeout(ProtoTimer& theTimer); |
| 27 |
void OnClientSocketEvent(ProtoSocket& theSocket, |
| 28 |
ProtoSocket::Event theEvent); |
| 29 |
void OnServerSocketEvent(ProtoSocket& theSocket, |
| 30 |
ProtoSocket::Event theEvent); |
| 31 |
|
| 32 |
// ProtoTimer/ UDP socket demo members |
| 33 |
ProtoTimer tx_timer; |
| 34 |
ProtoSocket server_socket; |
| 35 |
ProtoSocket client_socket; |
| 36 |
}; // end class UdpTest |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
// Our application instance |
| 41 |
PROTO_INSTANTIATE_APP(UdpTest) |
| 42 |
|
| 43 |
UdpTest::UdpTest() |
| 44 |
: server_socket(ProtoSocket::UDP), |
| 45 |
client_socket(ProtoSocket::UDP) |
| 46 |
{ |
| 47 |
tx_timer.SetListener(this, &UdpTest::OnTxTimeout); |
| 48 |
client_socket.SetNotifier(&GetSocketNotifier()); |
| 49 |
client_socket.SetListener(this, &UdpTest::OnClientSocketEvent); |
| 50 |
server_socket.SetNotifier(&GetSocketNotifier()); |
| 51 |
server_socket.SetListener(this, &UdpTest::OnServerSocketEvent); |
| 52 |
} |
| 53 |
|
| 54 |
UdpTest::~UdpTest() |
| 55 |
{ |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
bool UdpTest::OnStartup(int argc, const char*const* argv) |
| 60 |
{ |
| 61 |
if (argc > 1) |
| 62 |
{ |
| 63 |
// Act as client (sender) |
| 64 |
client_socket.Bind(6000 + argc - 2); |
| 65 |
tx_timer.SetInterval(1.0); |
| 66 |
tx_timer.SetRepeat(-1.0); |
| 67 |
ActivateTimer(tx_timer); |
| 68 |
TRACE("udptest: Client startup ..\n"); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
// Act as server (receiver) |
| 73 |
/*server_socket.Open(0, ProtoAddress::IPv4, false); |
| 74 |
server_socket.SetReuse(true); |
| 75 |
server_socket.Bind(5000);*/ |
| 76 |
|
| 77 |
ProtoAddress addr; |
| 78 |
addr.ConvertFromString("127.0.0.1"); |
| 79 |
addr.SetPort(6000); |
| 80 |
client_socket.Open(0, ProtoAddress::IPv4, false); |
| 81 |
client_socket.SetReuse(true); |
| 82 |
client_socket.Bind(5000); |
| 83 |
client_socket.Connect(addr); |
| 84 |
|
| 85 |
// Toggle server |
| 86 |
/*server_socket.Close();*/ |
| 87 |
server_socket.Open(0, ProtoAddress::IPv4, false); |
| 88 |
server_socket.SetReuse(true); |
| 89 |
server_socket.Bind(5000); |
| 90 |
TRACE("udptest: Server startup ..\n"); |
| 91 |
} |
| 92 |
return true; |
| 93 |
} // end UdpTest::OnStartup() |
| 94 |
|
| 95 |
void UdpTest::OnShutdown() |
| 96 |
{ |
| 97 |
if (tx_timer.IsActive()) tx_timer.Deactivate(); |
| 98 |
client_socket.Close(); |
| 99 |
server_socket.Close(); |
| 100 |
|
| 101 |
} // end UdpTest::OnShutdown() |
| 102 |
|
| 103 |
|
| 104 |
void UdpTest::OnTxTimeout(ProtoTimer& /*theTimer*/) |
| 105 |
{ |
| 106 |
ProtoAddress dst; |
| 107 |
dst.ConvertFromString("127.0.0.1"); |
| 108 |
dst.SetPort(5000); |
| 109 |
char buffer[256]; |
| 110 |
sprintf(buffer, "Hi, this is client %d\n", client_socket.GetPort()); |
| 111 |
unsigned int buflen = strlen(buffer) + 1; |
| 112 |
client_socket.SendTo(buffer, buflen, dst); |
| 113 |
TRACE("udptest: Client %d sent message to server ..\n", client_socket.GetPort()); |
| 114 |
|
| 115 |
} // end UdpTest::OnTxTimeout() |
| 116 |
|
| 117 |
|
| 118 |
void UdpTest::OnClientSocketEvent(ProtoSocket& theSocket, |
| 119 |
ProtoSocket::Event theEvent) |
| 120 |
{ |
| 121 |
if (ProtoSocket::RECV == theEvent) |
| 122 |
{ |
| 123 |
char buffer[1024]; |
| 124 |
unsigned int buflen = 1024; |
| 125 |
ProtoAddress src; |
| 126 |
theSocket.RecvFrom(buffer, buflen, src); |
| 127 |
if (buflen > 0) |
| 128 |
TRACE("udptest: server's client socket received: %s", buffer); |
| 129 |
} |
| 130 |
|
| 131 |
} // end UdpTest::OnClientSocketEvent() |
| 132 |
|
| 133 |
|
| 134 |
void UdpTest::OnServerSocketEvent(ProtoSocket& theSocket, |
| 135 |
ProtoSocket::Event theEvent) |
| 136 |
{ |
| 137 |
if (ProtoSocket::RECV == theEvent) |
| 138 |
{ |
| 139 |
char buffer[1024]; |
| 140 |
unsigned int buflen = 1024; |
| 141 |
ProtoAddress src; |
| 142 |
theSocket.RecvFrom(buffer, buflen, src); |
| 143 |
if (buflen > 0) |
| 144 |
TRACE("udptest: server's server socket received: %s", buffer); |
| 145 |
} |
| 146 |
} // end UdpTest::OnServerSocketEvent() |
| 147 |
|
| 148 |
|