Debian Codegraph @

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

raw
1
2 //
3 // Tcp application interface: extension of the TCPApp code to do duplex connections to transmit
4 // real application data and also allow multiple TCP connections
5 //
6 // The single connections model underlying TCP connection as a
7 // FIFO byte stream, use this to deliver user data
8
9 #include "agent.h"
10 #include "app.h"
11 #include "TCPSocketApp.h"
12
13
14 // TCPSocketApp that accept TCP agents as a parameter
15
16 static class TcpAppIntClass : public TclClass {
17 public:
18 TcpAppIntClass() : TclClass("Application/TCPSocketApp") {}
19 TclObject* create(int argc, const char*const* argv) {
20 if (argc != 5)
21 return NULL;
22 Agent *tcp = (Agent *)TclObject::lookup(argv[4]);
23 if (tcp == NULL)
24 return NULL;
25 return (new TCPSocketApp(tcp));
26 }
27 } class_TCPSocketApp;
28
29 // Socket Apps that don't have any underlying TCP protocol defined - these are used
30 // in the dynamic scenario, where we generate these.
31
32 TCPSocketApp::TCPSocketApp(Agent *tcp) :
33 Application(), curdata_(0), readOffset(0), bytesSent_(0), curbytes_(0) {
34 setTCPAgent(tcp);
35 }
36
37
38 TCPSocketApp::~TCPSocketApp() {
39 agent_->attachApp(NULL);
40 }
41
42 void TCPSocketApp::setTCPAgent(Agent *tcp) {
43 agent_ = tcp;
44 agent_->attachApp(this);
45 }
46
47
48 /**
49 * This method is on the CLIENT and is called by the receiving socket to get the data that has just been send
50 * across Ns-2 by the underlying TCP transport
51 */
52 TcpData* TCPSocketApp::receiveDataFromClient() {
53 PLOG(PL_DEBUG, "LJT Entering TCPSocketApp:: receiveDataFromClient\n");
54
55 TcpData* data = (TcpData*)tcpDataList_.getTail();
56 if (data!=NULL) tcpDataList_.remove((ListItem *)data); // SimpleList
57
58 PLOG(PL_DEBUG, "LJT Leaving TCPSocketApp:: receiveDataFromClient\n");
59 return data;
60 }
61
62
63 AppData* TCPSocketApp::get_data(int&, AppData*) {
64 PLOG(PL_FATAL, "TCPSokcetApp: get_data called - aborting ...\n");
65 // Not supported
66 abort();
67 return NULL;
68 }
69
70 /**
71 * Send with calls to add data to this pipe by adding to a FIFO buffer before allowing
72 * the underlying TCP implementation to make a simulated transfer.
73 */
74 void TCPSocketApp::send(AppData *cbk) {
75 PLOG(PL_DEBUG, "TCPSocketApp:: Send Called\n");
76
77 TcpData *tcpData = (TcpData*)cbk;
78 int nbytes = tcpData->getDataSize();
79
80 PLOG(PL_DEBUG, "TCPSocketApp:: Send Called after cast\n");
81
82 tcpDataList_.prepend(tcpData); // SimpleList
83
84 PLOG(PL_DETAIL, "TCPSocketApp:: Send Called, sending %i bytes\n", tcpData->getDataSize());
85 PLOG(PL_DETAIL, "TCPSocketApp sending: %s\n", tcpData->getData());
86
87 // send number of bytes to the underlying protocol i.e. FullTCP or whatever
88
89 PLOG(PL_DEBUG, "TCPSocketApp:: Sending bytes now \n");
90
91 Application::send(nbytes);
92
93 if (connectedSocketApp==NULL) {
94 PLOG(PL_ERROR, "TCPSocketApp::getDataFromOtherSocket, Destination for SocketApp is NULL -> the setup is incorrect. Check the code\n");
95 exit(0);
96 }
97
98 connectedSocketApp->setBytesSent(nbytes); // so we can check they all are received for SEND event
99
100 PLOG(PL_DEBUG, "TCPSocketApp:: Send Called \n");
101
102 }
103
104 void TCPSocketApp::getDataFromOtherSocket() {
105 if (connectedSocketApp==NULL) {
106 PLOG(PL_ERROR, "TCPSocketApp::getDataFromOtherSocket, Destination for SocketApp is NULL -> the setup is incorrect. Check the code\n");
107 exit(0);
108 }
109
110 curdata_ = connectedSocketApp->receiveDataFromClient();
111 if ((curdata_ == 0) || (curdata_->getDataSize()==0)) {
112 PLOG(PL_DEBUG, "TCPSocketApp::getDataFromOtherSocket, received data from TCP but no data to read!\n");
113 abort();
114 }
115 }
116
117
118 void TCPSocketApp::recv(int tcpDataArrivedSize) {
119 PLOG(PL_DEBUG, "TCPSocketApp::recv, tcpDataArrivedSize = %i, cur - %i\n", tcpDataArrivedSize, curbytes_);
120 if (curdata_ == 0)
121 getDataFromOtherSocket();
122 if (curdata_ == 0) {
123 fprintf(stderr, "[%g] %s receives a packet but no callback!\n",
124 Scheduler::instance().clock(), name_);
125 return;
126 }
127 curbytes_ += tcpDataArrivedSize;
128 if (curbytes_ == curdata_->getDataSize()) {
129 PLOG(PL_DEBUG, "TCPSocketApp::recv, equal and buffered data size = %i\n", curdata_->getDataSize());
130 upcallToApp(curdata_->getData(), curdata_->getDataSize());
131 delete curdata_;
132 curdata_ = NULL;
133 curbytes_ = 0;
134 } else if (curbytes_ > curdata_->getDataSize()) {
135 PLOG(PL_DEBUG, "TCPSocketApp::recv, not equal and buffered data size = %i\n", curdata_->getDataSize());
136 while (curbytes_ >= curdata_->getDataSize()) {
137 upcallToApp(curdata_->getData(), curdata_->getDataSize());
138 curbytes_ -= curdata_->getDataSize();
139 delete curdata_;
140 getDataFromOtherSocket();
141 if (curdata_ != 0)
142 continue;
143 if ((curdata_ == 0) && (curbytes_ > 0)) {
144 fprintf(stderr, "[%g] %s gets extra data!\n",
145 Scheduler::instance().clock(), name_);
146 Tcl::instance().eval("[Simulator instance] flush-trace");
147 abort();
148 } else
149 // Get out of the look without doing a check
150 break;
151 }
152 }
153 }
154
155
156 /**
157 * Application Callback HERE:
158 *
159 * upcallToApp() is called when data is delivered from the TCP agent. This effectively is the callback
160 * an application gets from an agent when data arrives. Since TCPSocketApp uses the application
161 * Ns2 interface, then this method will be called upon receipt of data the the Ns-2 node.
162 */
163 void TCPSocketApp::upcallToApp(char* data, unsigned int size)
164 {
165 PLOG(PL_DEBUG, "TCPSocketApp:: upcallToApp called \n");
166
167 if (data == NULL)
168 return;
169
170 PLOG(PL_DEBUG, "TCPSocketApp:: upcallToApp, receiving size %i \n", size);
171
172 if (target()) {
173 TcpData *tcpdata = new TcpData();
174 tcpdata->setData(data,size);
175 PLOG(PL_DETAIL, "TCPSocketApp:: upcallToApp, Sending to Target\n");
176 send_data(size, tcpdata);
177 PLOG(PL_DETAIL, "TCPSocketApp: upcallToApp, finished Sending Data to Target\n");
178 } else if (tcpSocketListenerAgent) { // pass it along
179 PLOG(PL_DETAIL, "TCPSocketApp:: upcallToApp, Sending to Listener\n");
180 char* datasend = new char[size];
181 memcpy(datasend, data, size);
182 tcpSocketListenerAgent->tcpEventReceived(new TCPEvent(TCPEvent::RECEIVE, NULL, datasend, size));
183 PLOG(PL_DETAIL, "TCPSocketApp: upcallToApp, finished Callback\n");
184 }
185 else {
186 PLOG(PL_INFO, "TCPSocketApp: Receiving %i of size %i at node %i\n", data, size, getTCPAgent()->addr());
187 }
188 }
189
190
191 void TCPSocketApp::resume()
192 {
193 // Do nothing
194 }
195
196 int TCPSocketApp::command(int argc, const char*const* argv)
197 {
198 Tcl& tcl = Tcl::instance();
199
200 // cout << "Command: " << argv[1] << endl;
201
202 if (strcmp(argv[1], "connect") == 0) {
203 connectedSocketApp = (TCPSocketApp *)TclObject::lookup(argv[2]);
204 if (connectedSocketApp == NULL) {
205 tcl.resultf("%s: connected to null object.", name_);
206 return (TCL_ERROR);
207 }
208 connectedSocketApp->connect(this);
209 return (TCL_OK);
210 } else if (strcmp(argv[1], "send") == 0) {
211
212 const char *bytes = argv[3];
213 int size = atoi(argv[2]);
214
215 // cout << "Sending " << bytes << ", size " << size << " from node " << getTCPAgent()->addr() << endl;
216
217 if (argc > 3) {
218 TcpData *tmp = new TcpData();
219 tmp->setData(bytes, size);
220 send(tmp);
221 }
222
223 return (TCL_OK);
224
225 } else if (strcmp(argv[1], "dst") == 0) {
226 tcl.resultf("%s", connectedSocketApp->name());
227 return TCL_OK;
228 }
229 return Application::command(argc, argv);
230 }
231
232
233
234
235
236
237
238 /** void TCPSocketApp::recv(int tcpDataArrivedSize) {
239
240
241 PLOG(PL_DEBUG, "TCPSocketApp:: Recv Called, %i bytes have arrived from TCP. Reading now\n", tcpDataArrivedSize);
242 int anythingLeftToRead=0;
243 char *writeBuffer = new char[tcpDataArrivedSize]; // buffer to read data into
244 int writeOffset=0;
245 int readingNow=0;
246 int datasize=0;
247 char *databuf=0;
248 do {
249 PLOG(PL_DEBUG, "TCPSocketApp:: Left to read = %i bytes\n", anythingLeftToRead);
250 if (curdata_==NULL) // get the next data from the FIFO
251 getDataFromOtherSocket();
252
253 if (curdata_==NULL) break;
254
255 datasize=curdata_->getDataSize();
256 databuf=curdata_->getData(); // current buffer to read from
257
258 if (anythingLeftToRead>0)
259 readingNow=anythingLeftToRead;
260 else
261 readingNow=tcpDataArrivedSize;
262
263 if (readOffset+readingNow > datasize) {
264 readingNow = datasize-readOffset;
265 anythingLeftToRead=tcpDataArrivedSize-readingNow; // left over to read
266 } else
267 anythingLeftToRead=0;
268
269 PLOG(PL_DETAIL, "TCPSocketApp: reading %u bytes at position %u, Total buffer is %i\n", readingNow, readOffset, datasize);
270 PLOG(PL_DETAIL, "TCPSocketApp: Writing %u bytes at position %i, total size of buffer = %i bytes\n", readingNow, writeOffset, tcpDataArrivedSize);
271
272 if ((writeOffset+readingNow) > tcpDataArrivedSize)
273 break;
274
275 memcpy(writeBuffer+writeOffset, databuf+readOffset, readingNow);
276
277 writeOffset+=readingNow; // so that all bytes are read
278 readOffset+=readingNow;
279
280 if (anythingLeftToRead>0) { // ran out of data, move on to next data
281 delete curdata_;
282 curdata_=NULL;
283 readOffset=0;
284 }
285
286 PLOG(PL_DETAIL, "TCPSocketApp: readOffset %u, WriteOffset %i, total = %u\n", readOffset, writeOffset, tcpDataArrivedSize);
287 } while(anythingLeftToRead>0); // Got everything
288
289 PLOG(PL_DETAIL, "TCPSocketApp::recv upcall to app\n");
290
291 PLOG(PL_DETAIL, "TCPSocketApp::recv TcpDataArrived = %i\n", tcpDataArrivedSize);
292
293 bytesSent_-=tcpDataArrivedSize; // bytesSent_ is the original bytes sent from the sender.
294
295 upcallToApp(writeBuffer, tcpDataArrivedSize); // always read in all the data, even if it is over several buffers
296
297 if (bytesSent_<0) {
298 PLOG(PL_FATAL, "TCPSocketApp::FATAL, ran out of data in buffer - BytesSent = %i\n", bytesSent_);
299 PLOG(PL_DETAIL, "TCPSocketApp::recv TcpDataArrived = %i\n", tcpDataArrivedSize);
300 PLOG(PL_DETAIL, "TCPSocketApp: Read now %u read offset %u, Total read buffer %i\n", readingNow, readOffset, datasize);
301 PLOG(PL_DETAIL, "TCPSocketApp: Write Offset %i, total size of buffer = %i bytes\n", writeOffset, tcpDataArrivedSize);
302 abort();
303 }
304
305 PLOG(PL_DETAIL, "TCPSocketApp::recv bytesSent = %i\n", bytesSent_);
306 PLOG(PL_DETAIL, "TCPSocketApp::recv listener = %i\n", connectedSocketApp->tcpSocketListenerAgent);
307
308 if (bytesSent_==0) { // data has been read in full from sender, so:
309 if (connectedSocketApp->tcpSocketListenerAgent) { // data is removed from data queue, so notify on SENDER there is more room
310 PLOG(PL_DETAIL, "TCPSocketApp::recv generating SEND event\n");
311 tcpSocketListenerAgent->tcpEventReceived(new TCPEvent(TCPEvent::SENDACK, NULL, NULL, tcpDataArrivedSize));
312 }
313 }
314
315 PLOG(PL_DETAIL, "TCPSocketApp:: Recv Done \n");
316 } */
317