TCPSinkApp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2004 Andras Varga
00003 //
00004 // This library is free software, you can redistribute it and/or modify
00005 // it under  the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation;
00007 // either version 2 of the License, or any later version.
00008 // The library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 // See the GNU Lesser General Public License for more details.
00012 //
00013 
00014 
00015 #include "TCPSinkApp.h"
00016 #include "TCPSocket.h"
00017 #include "TCPCommand_m.h"
00018 
00019 
00020 Define_Module(TCPSinkApp);
00021 
00022 void TCPSinkApp::initialize()
00023 {
00024     const char *address = par("address");
00025     int port = par("port");
00026 
00027     bytesRcvd = 0;
00028     WATCH(bytesRcvd);
00029 
00030     TCPSocket socket;
00031     socket.setOutputGate(gate("tcpOut"));
00032     socket.bind(address[0] ? IPvXAddress(address) : IPvXAddress(), port);
00033     socket.listen();
00034 }
00035 
00036 void TCPSinkApp::handleMessage(cMessage *msg)
00037 {
00038     if (msg->getKind()==TCP_I_PEER_CLOSED)
00039     {
00040         // we close too
00041         msg->setKind(TCP_C_CLOSE);
00042         send(msg, "tcpOut");
00043     }
00044     else if (msg->getKind()==TCP_I_DATA || msg->getKind()==TCP_I_URGENT_DATA)
00045     {
00046         bytesRcvd += PK(msg)->getByteLength();
00047         delete msg;
00048 
00049         if (ev.isGUI())
00050         {
00051             char buf[32];
00052             sprintf(buf, "rcvd: %ld bytes", bytesRcvd);
00053             getDisplayString().setTagArg("t",0,buf);
00054         }
00055     }
00056     else
00057     {
00058         // must be data or some kind of indication -- can be dropped
00059         delete msg;
00060     }
00061 }
00062 
00063 void TCPSinkApp::finish()
00064 {
00065     recordScalar("bytesRcvd", bytesRcvd);
00066 }
00067