Debian Codegraph @

protolib/src/sim/ns/tcp/TCPSocketExample.cpp

raw
1 /*
2 * TCPSocketExample.h
3 *
4 * Created by Ian Taylor on 01/03/2007.
5 *
6 */
7
8 #include <agent.h>
9 #include <ip.h>
10
11 #include "TCPSocketAgent.h"
12 #include "TCPServerSocketAgent.h"
13
14 #include "TCPSocketFactory.h"
15
16 // C++ example ...
17
18 class TCPSocketExample : public Agent, TCPSocketAgentListener {
19 public:
20 int CLIENT_CONNECTIONS;
21 int SERVER_CONNECTIONS;
22 int PORT;
23
24 TCPSocketAgent **clients;
25 TCPServerSocketAgent *server;
26 TCPSocketAgent **serversockets;
27
28 int connectTo; // address that the server is on
29
30 int sockno;
31 int transmits;
32 int leftoverread;
33 unsigned int *startByte;
34 int msgsize;
35
36 TCPSocketAgent::TCPSendTriggerMode sendMode;
37
38 TCPSocketExample() : Agent(PT_TCP) {
39 CLIENT_CONNECTIONS=1;
40 SERVER_CONNECTIONS=1;
41 PORT=55;
42 sockno=0;
43 transmits=0;
44 SetDebugLevel(PL_DETAIL);
45 startByte=0;
46 sendMode=TCPSocketAgent::LEVEL;
47 leftoverread=0;
48 msgsize=53;
49 }
50
51 ~TCPSocketExample() {
52 PLOG(PL_INFO, "TCPSocketExample: Destructing\n");
53 free(startByte);
54 for (int i=0; i<CLIENT_CONNECTIONS; ++i)
55 delete clients[i];
56 delete[] clients;
57 delete server;
58 for (int i=0; i<SERVER_CONNECTIONS; ++i)
59 delete serversockets[i];
60 delete[] serversockets;
61 }
62
63 void createServer() {
64 server = TCPSocketFactory::createServerSocketAgent(this); // must set yoursef as the simulation agent
65
66 if (server->bind(PORT)==FALSE) PLOG(PL_ERROR, "Problem with Binding node to port\n");
67
68 server->setTCPAgentProtocol(TCPSocketAgent::FULLTCP);
69 server->listen();
70 server->setListener(this);
71
72 PLOG(PL_INFO, "TCPSocketExample: Created server on port %i\n", server->port());
73
74 PLOG(PL_INFO, "TCPSocketExample: Creating Server Sockets\n");
75
76 serversockets = new TCPSocketAgent *[SERVER_CONNECTIONS];
77
78 PLOG(PL_INFO, "TCPSocketExample: Created Server, ok\n");
79 }
80
81 void createClients() {
82 clients = new TCPSocketAgent *[CLIENT_CONNECTIONS];
83 startByte = (unsigned int*)malloc(CLIENT_CONNECTIONS*sizeof(unsigned int));
84
85 for (int i=0; i<CLIENT_CONNECTIONS; ++i) {
86 clients[i] = TCPSocketFactory::createSocketAgent(this);
87 clients[i]->setTCPAgentProtocol(TCPSocketAgent::FULLTCP);
88 clients[i]->bind(PORT+i);
89 clients[i]->setListener(this);
90 clients[i]->setTxBufferSize(200);
91 startByte[i] = 0;
92 clients[i]->setSENDTriggerMode(sendMode);
93 clients[i]->setOutputNotification(false);
94 }
95 PLOG(PL_INFO, "TCPSocketExample: Created Clients\n");
96 }
97
98 void connectClients() {
99 for (int i=0; i<CLIENT_CONNECTIONS; ++i)
100 clients[i]->connect(connectTo, PORT);
101 PLOG(PL_INFO, "TCPSocketExample: Connected Clients\n");
102 }
103
104 void sendData() {
105 PLOG(PL_INFO, "TCPSocketExample: Sending Data\n");
106 unsigned int bytesSent; // anything greater than
107 unsigned int bytesSending;
108 bool continueloop=false;
109
110 char *message;
111
112 for (int i=0; i<CLIENT_CONNECTIONS; ++i) {
113 do {
114 ++transmits;
115 if (transmits>10) break;
116 message= new char[msgsize]; // max buffer size
117 sprintf(message, "Hello another long message %2i from node %i and port %i", transmits, addr(), clients[i]->port());
118 bytesSending= msgsize - startByte[i]; // send actual length
119 PLOG(PL_INFO, "TCPSocketExample: StartByte = %u \n", startByte[i]);
120 PLOG(PL_INFO, "TCPSocketExample: Sending %u bytes = %s\n", bytesSending, (message+startByte[i]));
121 bytesSent = clients[i]->send(bytesSending, (const char *)(message+startByte[i]));
122 PLOG(PL_INFO, "TCPSocketExample: Sent %u bytes\n", bytesSent);
123 if (bytesSent<bytesSending) {
124 startByte[i]=bytesSent;
125 --transmits;
126 PLOG(PL_INFO, "TCPSocketExample: Out of space in send buffer - startByte = %u \n", startByte);
127 } else startByte[i]=0;
128 delete[] message;
129
130 if ((sendMode ==TCPSocketAgent::EDGE) || (!clients[i]->isOutputNotificationEnabled())) {
131 if (bytesSent==bytesSending)
132 continueloop=true;
133 else
134 continueloop=false;
135 }
136 else {
137 continueloop=false;
138 }
139 } while (continueloop);
140 }
141 PLOG(PL_INFO, "TCPSocketExample: send, leaving ...\n");
142 }
143
144 void recv(char *databuf, int size, unsigned int from) {
145 int offset=0;
146 int toread;
147 PLOG(PL_INFO, "TCPSocketExample: Data Arrived %i bytes\n", size);
148 do {
149 if (leftoverread>0)
150 toread=leftoverread;
151 else
152 toread=msgsize;
153
154 if (offset+toread > size) {
155 toread = size-offset;
156 leftoverread=msgsize-toread;
157 } else
158 leftoverread=0;
159
160 PLOG(PL_INFO, "TCPSocketExample: Reading %i bytes\n", toread);
161
162 char *message = new char[toread];
163 memcpy(message, databuf+offset, toread);
164 PLOG(PL_INFO, "TCPSocketExample: Received data \"%s\" of size %i at node %i\n", message,toread,from);
165 offset+=toread;
166 delete message;
167 } while(offset<size);
168 }
169
170 virtual bool tcpEventReceived(TCPEvent *event) {
171 PLOG(PL_INFO, "TCPSocketExample: An Event !, Type %i \n", event->getType());
172
173 if (event->getType()==TCPEvent::SEND) {
174 PLOG(PL_INFO, "TCPSocketExample: SEND Received - buffer ready to write\n");
175 sendData();
176 return true;
177 }
178 else if (event->getSource()==server) {
179 if (event->getType()==TCPEvent::ACCEPT) {
180 PLOG(PL_INFO, "TCPSocketExample: ACCEPT Notification\n");
181 serversockets[sockno] = server->accept(); // accept the connection and get the socket responsible for it
182 serversockets[sockno]->setListener(this); // set the listener for RECEIVE events to this class
183 ++sockno;
184 return true;
185 }
186 } else if (event->getType()==TCPEvent::RECEIVE) {
187 PLOG(PL_INFO, "TCPSocketExample: Received Data\n");
188 recv((char *)event->getData(), event->getDataSize(), this->addr());
189 return true;
190 } else if (event->getType()==TCPEvent::CONNECTED) {
191 PLOG(PL_INFO, "TCPSocketExample: CONECTED to Server\n");
192 return true;
193 } else if (event->getType()==TCPEvent::DISCONNECTED) {
194 PLOG(PL_INFO, "TCPSocketExample: Socket has been successfully closed\n");
195 return true;
196 }
197
198 return false;
199 }
200
201 int command(int argc, const char*const* argv) {
202 PLOG(PL_INFO, "Node = %s, command = %s\n", name(), argv[1]);
203
204 for (int i =0; i<argc; ++i)
205 PLOG(PL_INFO," arg[%i] = %s," , i, argv[i]);
206
207 PLOG(PL_INFO,"\n");
208
209 if (argc==2) {
210 if (strcmp(argv[1], "createClients") == 0) {
211 createClients();
212 return (TCL_OK);
213 } else if (strcmp(argv[1], "createServer") == 0) {
214 createServer();
215 return (TCL_OK);
216 } else if (strcmp(argv[1], "connectClients") == 0) {
217 connectClients();
218 return (TCL_OK);
219 } else if (strcmp(argv[1], "sendData") == 0) {
220 // sendData();
221 return (TCL_OK);
222 } else if (strcmp(argv[1], "close") == 0) {
223 server->close();
224 return (TCL_OK);
225 }
226 } else if (argc==3) {
227 if (strcmp(argv[1], "setClientConnections") == 0) {
228 CLIENT_CONNECTIONS = atoi(argv[2]);
229 return (TCL_OK);
230 }
231 else if (strcmp(argv[1], "setServerConnections") == 0) {
232 SERVER_CONNECTIONS = atoi(argv[2]);
233 return (TCL_OK);
234 }
235 else if (strcmp(argv[1], "setConnectTo") == 0) {
236 connectTo = atoi(argv[2]);
237 return (TCL_OK);
238 }
239 }
240
241 return Agent::command(argc, argv);
242 }
243 };
244
245 static class TCPSocketExampleClass : public TclClass {
246 public:
247 TCPSocketExampleClass() : TclClass("Agent/TCP/TCPSocketExample") {}
248 TclObject* create(int argc, const char*const* argv) {
249 if (argc != 4)
250 return NULL;
251 return (new TCPSocketExample());
252 }
253 } class_TCP_Socket_Example;
254