A simple TCP example application. More...
#include <TCPExampleApp.h>
Public Member Functions | |
TCPExampleApp () | |
~TCPExampleApp () | |
Private Member Functions | |
void | initializeApp (int stage) |
initializes derived class-attributes | |
void | finishApp () |
collects statistical data of derived app | |
void | handleTimerEvent (cMessage *msg) |
void | handleDataReceived (TransportAddress address, cPacket *msg, bool urgent) |
Member function to handle incoming data. | |
void | handleConnectionEvent (EvCode code, TransportAddress address) |
Member function to handle passive connection events. | |
Private Attributes | |
simtime_t | sendPeriod |
int | numSent |
int | numReceived |
cMessage * | timerMsg |
A simple TCP example application.
This is a simple TCP example application. It periodically establishes TCP connections to random nodes, sends PING and receives PONG messages.
Definition at line 43 of file TCPExampleApp.h.
TCPExampleApp::TCPExampleApp | ( | ) |
Definition at line 38 of file TCPExampleApp.cc.
{ timerMsg = NULL; }
TCPExampleApp::~TCPExampleApp | ( | ) |
Definition at line 43 of file TCPExampleApp.cc.
{ cancelAndDelete(timerMsg); }
void TCPExampleApp::finishApp | ( | ) | [private, virtual] |
collects statistical data of derived app
Reimplemented from BaseApp.
Definition at line 78 of file TCPExampleApp.cc.
{ globalStatistics->addStdDev("TCPExampleApp: Sent packets", numSent); globalStatistics->addStdDev("TCPExampleApp: Received packets", numReceived); }
void TCPExampleApp::handleConnectionEvent | ( | EvCode | code, | |
TransportAddress | address | |||
) | [private, virtual] |
Member function to handle passive connection events.
code | event code of the current event | |
address | transport address of the remote host |
Reimplemented from BaseTcpSupport.
Definition at line 160 of file TCPExampleApp.cc.
{ if (code == PEER_CLOSED) { scheduleAt(simTime() + sendPeriod, timerMsg); } else { BaseTcpSupport::handleConnectionEvent(code, address); } }
void TCPExampleApp::handleDataReceived | ( | TransportAddress | address, | |
cPacket * | msg, | |||
bool | urgent | |||
) | [private, virtual] |
Member function to handle incoming data.
address | transport address of the remote host | |
msg | incoming data message | |
urgent | message urgency |
Reimplemented from BaseTcpSupport.
Definition at line 126 of file TCPExampleApp.cc.
{ // *redefine* to perform or schedule next sending TCPExampleMessage *TCPMsg = dynamic_cast<TCPExampleMessage*>(msg); RECORD_STATS(numReceived++); if (TCPMsg && TCPMsg->getType() == TCPEXMSG_PING){ // create PONG message TCPExampleMessage *respMsg = new TCPExampleMessage(); respMsg->setType(TCPEXMSG_PONG); // set the message type to PONG respMsg->setSenderAddress(thisNode); // set the sender address to our own respMsg->setByteLength(100); sendTcpData(respMsg, address); RECORD_STATS(numSent++); // user output EV << thisNode.getIp() << ": Got PING from " << TCPMsg->getSenderAddress().getIp() << ", sending PONG!" << std::endl; } else if (TCPMsg && TCPMsg->getType() == TCPEXMSG_PONG){ // user output EV << thisNode.getIp() << ": Got PONG reply! Closing connection." << std::endl; // dialog successfully transmitted, close connection closeTcpConnection(address); } delete msg; }
void TCPExampleApp::handleTimerEvent | ( | cMessage * | msg | ) | [private] |
Definition at line 85 of file TCPExampleApp.cc.
{ if (msg == timerMsg) { // if the simulator is still busy creating the network, // let's wait a bit longer if (underlayConfigurator->isInInitPhase()) { scheduleAt(simTime() + sendPeriod, timerMsg); return; } // do the same, if the node is the only one alive if (globalNodeList->getNumNodes() == 1){ scheduleAt(simTime() + sendPeriod, timerMsg); return; } // get the address of one of the other nodes TransportAddress* addr = globalNodeList->getRandomAliveNode(); while (thisNode.getIp().equals(addr->getIp())) { addr = globalNodeList->getRandomAliveNode(); } // create a PING message TCPExampleMessage *TCPMsg = new TCPExampleMessage(); TCPMsg->setType(TCPEXMSG_PING); // set the message type to PING TCPMsg->setSenderAddress(thisNode); // set the sender address to our own TCPMsg->setByteLength(100); // set the message length to 100 bytes RECORD_STATS(numSent++); // update statistics // connect and send message TransportAddress remoteAddress = TransportAddress(addr->getIp(), 24000); establishTcpConnection(remoteAddress); sendTcpData(TCPMsg, remoteAddress); // user output EV << thisNode.getIp() << ": Connecting to "<< addr->getIp() << " and sending PING."<< std::endl; } }
void TCPExampleApp::initializeApp | ( | int | stage | ) | [private, virtual] |
initializes derived class-attributes
stage | the init stage |
Reimplemented from BaseApp.
Definition at line 48 of file TCPExampleApp.cc.
{ if (stage != MIN_STAGE_APP) { return; } // copy the module parameter values to our own variables sendPeriod = par("sendPeriod"); // initialize our statistics variables numSent = 0; numReceived = 0; // tell the GUI to display our variables WATCH(numSent); WATCH(numReceived); // start our timer timerMsg = new cMessage("Periodic timer"); // set up and listen on tcp bindAndListenTcp(24000); // first node which was created starts with PING-PONG messaging if (globalNodeList->getNumNodes() == 1) { scheduleAt(simTime() + SimTime::parse("20s"), timerMsg); } }
int TCPExampleApp::numReceived [private] |
Definition at line 62 of file TCPExampleApp.h.
Referenced by finishApp(), handleDataReceived(), and initializeApp().
int TCPExampleApp::numSent [private] |
Definition at line 61 of file TCPExampleApp.h.
Referenced by finishApp(), handleDataReceived(), handleTimerEvent(), and initializeApp().
simtime_t TCPExampleApp::sendPeriod [private] |
Definition at line 58 of file TCPExampleApp.h.
Referenced by handleConnectionEvent(), handleTimerEvent(), and initializeApp().
cMessage* TCPExampleApp::timerMsg [private] |
Definition at line 65 of file TCPExampleApp.h.
Referenced by handleConnectionEvent(), handleTimerEvent(), initializeApp(), TCPExampleApp(), and ~TCPExampleApp().