|
INET Framework for OMNeT++/OMNEST
|
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 "TCPEchoApp.h" 00016 #include "TCPSocket.h" 00017 #include "TCPCommand_m.h" 00018 00019 00020 Define_Module(TCPEchoApp); 00021 00022 void TCPEchoApp::initialize() 00023 { 00024 const char *address = par("address"); 00025 int port = par("port"); 00026 delay = par("echoDelay"); 00027 echoFactor = par("echoFactor"); 00028 00029 bytesRcvd = bytesSent = 0; 00030 WATCH(bytesRcvd); 00031 WATCH(bytesSent); 00032 00033 TCPSocket socket; 00034 socket.setOutputGate(gate("tcpOut")); 00035 socket.bind(address[0] ? IPvXAddress(address) : IPvXAddress(), port); 00036 socket.listen(); 00037 } 00038 00039 void TCPEchoApp::sendDown(cMessage *msg) 00040 { 00041 if (msg->isPacket()) 00042 bytesSent += ((cPacket *)msg)->getByteLength(); 00043 send(msg, "tcpOut"); 00044 } 00045 00046 void TCPEchoApp::handleMessage(cMessage *msg) 00047 { 00048 if (msg->isSelfMessage()) 00049 { 00050 sendDown(msg); 00051 } 00052 else if (msg->getKind()==TCP_I_PEER_CLOSED) 00053 { 00054 // we'll close too 00055 msg->setKind(TCP_C_CLOSE); 00056 if (delay==0) 00057 sendDown(msg); 00058 else 00059 scheduleAt(simTime()+delay, msg); // send after a delay 00060 } 00061 else if (msg->getKind()==TCP_I_DATA || msg->getKind()==TCP_I_URGENT_DATA) 00062 { 00063 cPacket *pkt = check_and_cast<cPacket *>(msg); 00064 bytesRcvd += pkt->getByteLength(); 00065 00066 if (echoFactor==0) 00067 { 00068 delete pkt; 00069 } 00070 else 00071 { 00072 // reverse direction, modify length, and send it back 00073 pkt->setKind(TCP_C_SEND); 00074 TCPCommand *ind = check_and_cast<TCPCommand *>(pkt->removeControlInfo()); 00075 TCPSendCommand *cmd = new TCPSendCommand(); 00076 cmd->setConnId(ind->getConnId()); 00077 pkt->setControlInfo(cmd); 00078 delete ind; 00079 00080 long byteLen = pkt->getByteLength()*echoFactor; 00081 if (byteLen<1) byteLen=1; 00082 pkt->setByteLength(byteLen); 00083 00084 if (delay==0) 00085 sendDown(pkt); 00086 else 00087 scheduleAt(simTime()+delay, pkt); // send after a delay 00088 } 00089 } 00090 else 00091 { 00092 // some indication -- ignore 00093 delete msg; 00094 } 00095 00096 if (ev.isGUI()) 00097 { 00098 char buf[80]; 00099 sprintf(buf, "rcvd: %ld bytes\nsent: %ld bytes", bytesRcvd, bytesSent); 00100 getDisplayString().setTagArg("t",0,buf); 00101 } 00102 } 00103 00104 void TCPEchoApp::finish() 00105 { 00106 recordScalar("bytesRcvd", bytesRcvd); 00107 recordScalar("bytesSent", bytesSent); 00108 }