TCPGenericCliAppBase.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 "TCPGenericCliAppBase.h"
00016 #include "IPAddressResolver.h"
00017 #include "GenericAppMsg_m.h"
00018 
00019 
00020 
00021 void TCPGenericCliAppBase::initialize()
00022 {
00023     numSessions = numBroken = packetsSent = packetsRcvd = bytesSent = bytesRcvd = 0;
00024     WATCH(numSessions);
00025     WATCH(numBroken);
00026     WATCH(packetsSent);
00027     WATCH(packetsRcvd);
00028     WATCH(bytesSent);
00029     WATCH(bytesRcvd);
00030 
00031     // parameters
00032     const char *address = par("address");
00033     int port = par("port");
00034     socket.bind(*address ? IPvXAddress(address) : IPvXAddress(), port);
00035 
00036     socket.setCallbackObject(this);
00037     socket.setOutputGate(gate("tcpOut"));
00038 
00039     setStatusString("waiting");
00040 }
00041 
00042 void TCPGenericCliAppBase::handleMessage(cMessage *msg)
00043 {
00044     if (msg->isSelfMessage())
00045         handleTimer(msg);
00046     else
00047         socket.processMessage(msg);
00048 }
00049 
00050 void TCPGenericCliAppBase::connect()
00051 {
00052     // we need a new connId if this is not the first connection
00053     socket.renewSocket();
00054 
00055     // connect
00056     const char *connectAddress = par("connectAddress");
00057     int connectPort = par("connectPort");
00058 
00059     EV << "issuing OPEN command\n";
00060     setStatusString("connecting");
00061 
00062     socket.connect(IPAddressResolver().resolve(connectAddress), connectPort);
00063 
00064     numSessions++;
00065 }
00066 
00067 void TCPGenericCliAppBase::close()
00068 {
00069     setStatusString("closing");
00070     EV << "issuing CLOSE command\n";
00071     socket.close();
00072 }
00073 
00074 void TCPGenericCliAppBase::sendPacket(int numBytes, int expectedReplyBytes, bool serverClose)
00075 {
00076     EV << "sending " << numBytes << " bytes, expecting " << expectedReplyBytes << (serverClose ? ", and server should close afterwards\n" : "\n");
00077 
00078     GenericAppMsg *msg = new GenericAppMsg("data");
00079     msg->setByteLength(numBytes);
00080     msg->setExpectedReplyLength(expectedReplyBytes);
00081     msg->setServerClose(serverClose);
00082 
00083     socket.send(msg);
00084 
00085     packetsSent++;
00086     bytesSent+=numBytes;
00087 }
00088 
00089 void TCPGenericCliAppBase::setStatusString(const char *s)
00090 {
00091     if (ev.isGUI()) getDisplayString().setTagArg("t", 0, s);
00092 }
00093 
00094 void TCPGenericCliAppBase::socketEstablished(int, void *)
00095 {
00096     // *redefine* to perform or schedule first sending
00097     EV << "connected\n";
00098     setStatusString("connected");
00099 }
00100 
00101 void TCPGenericCliAppBase::socketDataArrived(int, void *, cPacket *msg, bool)
00102 {
00103     // *redefine* to perform or schedule next sending
00104     packetsRcvd++;
00105     bytesRcvd+=msg->getByteLength();
00106 
00107     delete msg;
00108 }
00109 
00110 void TCPGenericCliAppBase::socketPeerClosed(int, void *)
00111 {
00112     // close the connection (if not already closed)
00113     if (socket.getState()==TCPSocket::PEER_CLOSED)
00114     {
00115         EV << "remote TCP closed, closing here as well\n";
00116         close();
00117     }
00118 }
00119 
00120 void TCPGenericCliAppBase::socketClosed(int, void *)
00121 {
00122     // *redefine* to start another session etc.
00123     EV << "connection closed\n";
00124     setStatusString("closed");
00125 }
00126 
00127 void TCPGenericCliAppBase::socketFailure(int, void *, int code)
00128 {
00129     // subclasses may override this function, and add code try to reconnect after a delay.
00130     EV << "connection broken\n";
00131     setStatusString("broken");
00132 
00133     numBroken++;
00134 }
00135 
00136 void TCPGenericCliAppBase::finish()
00137 {
00138     EV << getFullPath() << ": opened " << numSessions << " sessions\n";
00139     EV << getFullPath() << ": sent " << bytesSent << " bytes in " << packetsSent << " packets\n";
00140     EV << getFullPath() << ": received " << bytesRcvd << " bytes in " << packetsRcvd << " packets\n";
00141 
00142     recordScalar("number of sessions", numSessions);
00143     recordScalar("packets sent", packetsSent);
00144     recordScalar("packets rcvd", packetsRcvd);
00145     recordScalar("bytes sent", bytesSent);
00146     recordScalar("bytes rcvd", bytesRcvd);
00147 }
00148 
00149