Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TCPSessionApp Class Reference

#include <TCPSessionApp.h>

List of all members.


Detailed Description

Single-connection TCP application.


Public Member Functions

 TCPSessionApp ()

Protected Types

typedef std::vector< CommandCommandVector

Protected Member Functions

void parseScript (const char *script)
void waitUntil (simtime_t t)
void count (cMessage *msg)
virtual void activity ()
virtual void finish ()

Protected Attributes

CommandVector commands
TCPSocket socket
int packetsRcvd
int bytesRcvd
int indicationsRcvd

Classes

struct  Command


Member Typedef Documentation

typedef std::vector<Command> TCPSessionApp::CommandVector [protected]
 


Constructor & Destructor Documentation

TCPSessionApp::TCPSessionApp  )  [inline]
 

00044 : cSimpleModule(32768) {}


Member Function Documentation

void TCPSessionApp::activity  )  [protected, virtual]
 

00087 {
00088     packetsRcvd = bytesRcvd = indicationsRcvd = 0;
00089     WATCH(packetsRcvd);
00090     WATCH(bytesRcvd);
00091     WATCH(indicationsRcvd);
00092 
00093     // parameters
00094     const char *address = par("address");
00095     int port = par("port");
00096     const char *connectAddress = par("connectAddress");
00097     int connectPort = par("connectPort");
00098 
00099     bool active = par("active");
00100     simtime_t tOpen = par("tOpen");
00101     simtime_t tSend = par("tSend");
00102     long sendBytes = par("sendBytes");
00103     simtime_t tClose = par("tClose");
00104 
00105     const char *script = par("sendScript");
00106     parseScript(script);
00107     if (sendBytes>0 && commands.size()>0)
00108         throw new cException("cannot use both sendScript and tSend+sendBytes");
00109 
00110     socket.setOutputGate(gate("tcpOut"));
00111 
00112     // open
00113     waitUntil(tOpen);
00114 
00115     socket.bind(*address ? IPvXAddress(address) : IPvXAddress(), port);
00116 
00117     EV << "issuing OPEN command\n";
00118     if (ev.isGUI()) displayString().setTagArg("t",0, active?"connecting":"listening");
00119 
00120     if (active)
00121         socket.connect(IPAddressResolver().resolve(connectAddress), connectPort);
00122     else
00123         socket.listen();
00124 
00125     // wait until connection gets established
00126     while (socket.state()!=TCPSocket::CONNECTED)
00127     {
00128         socket.processMessage(receive());
00129         if (socket.state()==TCPSocket::SOCKERROR)
00130             return;
00131     }
00132 
00133     EV << "connection established, starting sending\n";
00134     if (ev.isGUI()) displayString().setTagArg("t",0,"connected");
00135 
00136     // send
00137     if (sendBytes>0)
00138     {
00139         waitUntil(tSend);
00140         EV << "sending " << sendBytes << " bytes\n";
00141         cMessage *msg = new cMessage("data1");
00142         msg->setByteLength(sendBytes);
00143         socket.send(msg);
00144     }
00145     for (CommandVector::iterator i=commands.begin(); i!=commands.end(); ++i)
00146     {
00147         waitUntil(i->tSend);
00148         EV << "sending " << i->numBytes << " bytes\n";
00149         cMessage *msg = new cMessage("data1");
00150         msg->setByteLength(i->numBytes);
00151         socket.send(msg);
00152     }
00153 
00154     // close
00155     if (tClose>=0)
00156     {
00157         waitUntil(tClose);
00158         EV << "issuing CLOSE command\n";
00159         if (ev.isGUI()) displayString().setTagArg("t",0,"closing");
00160         socket.close();
00161     }
00162 
00163     // wait until peer closes too and all data arrive
00164     for (;;)
00165     {
00166         cMessage *msg = receive();
00167         count(msg);
00168         socket.processMessage(msg);
00169     }
00170 }

void TCPSessionApp::count cMessage *  msg  )  [protected]
 

00058 {
00059     if (msg->kind()==TCP_I_DATA || msg->kind()==TCP_I_URGENT_DATA)
00060     {
00061         packetsRcvd++;
00062         bytesRcvd+=msg->byteLength();
00063     }
00064     else
00065     {
00066         indicationsRcvd++;
00067     }
00068 }

void TCPSessionApp::finish  )  [protected, virtual]
 

00173 {
00174     EV << fullPath() << ": received " << bytesRcvd << " bytes in " << packetsRcvd << " packets\n";
00175 }

void TCPSessionApp::parseScript const char *  script  )  [protected]
 

00023 {
00024     const char *s = script;
00025     while (*s)
00026     {
00027         Command cmd;
00028 
00029         // parse time
00030         while (isspace(*s)) s++;
00031         if (!*s || *s==';') break;
00032         const char *s0 = s;
00033         cmd.tSend = strtod(s,&const_cast<char *&>(s));
00034         if (s==s0)
00035             throw new cException("syntax error in script: simulation time expected");
00036 
00037         // parse number of bytes
00038         while (isspace(*s)) s++;
00039         if (!isdigit(*s))
00040             throw new cException("syntax error in script: number of bytes expected");
00041         cmd.numBytes = atoi(s);
00042         while (isdigit(*s)) s++;
00043 
00044         // add command
00045         commands.push_back(cmd);
00046 
00047         // skip delimiter
00048         while (isspace(*s)) s++;
00049         if (!*s) break;
00050         if (*s!=';')
00051             throw new cException("syntax error in script: separator ';' missing");
00052         s++;
00053         while (isspace(*s)) s++;
00054     }
00055 }

void TCPSessionApp::waitUntil simtime_t  t  )  [protected]
 

00071 {
00072     if (simTime()>=t)
00073         return;
00074 
00075     cMessage *timeoutMsg = new cMessage("timeout");
00076     scheduleAt(t, timeoutMsg);
00077     cMessage *msg=NULL;
00078     while ((msg=receive())!=timeoutMsg)
00079     {
00080         count(msg);
00081         socket.processMessage(msg);
00082     }
00083     delete timeoutMsg;
00084 }


Member Data Documentation

int TCPSessionApp::bytesRcvd [protected]
 

CommandVector TCPSessionApp::commands [protected]
 

int TCPSessionApp::indicationsRcvd [protected]
 

int TCPSessionApp::packetsRcvd [protected]
 

TCPSocket TCPSessionApp::socket [protected]
 


The documentation for this class was generated from the following files:
Generated on Sat Apr 1 20:52:25 2006 for INET Framework for OMNeT++/OMNEST by  doxygen 1.4.1