Test class for the application layer. More...
#include <SensorApplLayer.h>
Inherits BaseLayer.
Public Types | |
enum | APPL_MSG_TYPES { SEND_DATA_TIMER, STOP_SIM_TIMER, DATA_MESSAGE } |
enum | TRAFFIC_TYPES { UNKNOWN = 0, PERIODIC, UNIFORM, EXPONENTIAL, NB_DISTRIBUTIONS } |
Public Member Functions | |
virtual void | initialize (int) |
Initialization of the module and some variables. | |
virtual void | finish () |
Called when the simulation has finished. | |
Protected Member Functions | |
virtual void | handleSelfMsg (cMessage *) |
Handle self messages such as timer... | |
virtual void | handleLowerMsg (cMessage *) |
Handle messages from lower layer. | |
virtual void | handleLowerControl (cMessage *msg) |
Handle control messages from lower layer. | |
virtual void | handleUpperMsg (cMessage *m) |
Handle messages from upper layer. | |
virtual void | handleUpperControl (cMessage *m) |
Handle control messages from upper layer. | |
virtual void | sendData () |
send a data packet to the next hop | |
virtual void | initializeDistribution (const char *) |
Recognize distribution name. Redefine this method to add your own distribution. | |
virtual void | scheduleNextPacket () |
calculate time to wait before sending next packet, if required. You can redefine this method in a subclass to add your own distribution. | |
cStdDev & | hostsLatency (int hostAddress) |
Returns the latency statistics variable for the passed host address. | |
Protected Attributes | |
cMessage * | delayTimer |
int | myAppAddr |
int | destAddr |
int | sentPackets |
simtime_t | initializationTime |
simtime_t | firstPacketGeneration |
simtime_t | lastPacketReception |
int | trafficType |
double | trafficParam |
int | nbPackets |
long | nbPacketsSent |
long | nbPacketsReceived |
bool | stats |
bool | trace |
bool | debug |
Debug switch for all other modules. | |
bool | broadcastPackets |
map< int, cStdDev > | latencies |
cStdDev | latency |
cOutVector | latenciesRaw |
Packet | packet |
int | catPacket |
int | hostID |
int | headerLength |
BaseWorldUtility * | world |
int | dataOut |
int | dataIn |
int | ctrlOut |
int | ctrlIn |
Test class for the application layer.
All nodes periodically generate a packet addressed to a sink. This class takes three arguments:
Definition at line 54 of file SensorApplLayer.h.
void SensorApplLayer::handleLowerMsg | ( | cMessage * | msg | ) | [protected, virtual] |
Handle messages from lower layer.
Handling of messages arrived to destination.
Implements BaseLayer.
Definition at line 165 of file SensorApplLayer.cc.
References hostsLatency(), and Blackboard::publishBBItem().
{ ApplPkt *m; switch (msg->getKind()) { case DATA_MESSAGE: m = static_cast<ApplPkt *> (msg); nbPacketsReceived++; packet.setPacketSent(false); packet.setNbPacketsSent(0); packet.setNbPacketsReceived(1); packet.setHost(myAppAddr); world->publishBBItem(catPacket, &packet, hostID); if (stats) { simtime_t theLatency = m->getArrivalTime() - m->getCreationTime(); if(trace) { hostsLatency(m->getSrcAddr()).collect(theLatency); latenciesRaw.record(theLatency.dbl()); } latency.collect(theLatency); if (firstPacketGeneration < 0) firstPacketGeneration = m->getCreationTime(); lastPacketReception = m->getArrivalTime(); if(trace) { debugEV<< "Received a data packet from host[" << m->getSrcAddr() << "], latency=" << theLatency << ", collected " << hostsLatency(m->getSrcAddr()). getCount() << "mean is now: " << hostsLatency(m->getSrcAddr()). getMean() << endl; } else { debugEV<< "Received a data packet from host[" << m->getSrcAddr() << "], latency=" << theLatency << endl; } } delete msg; // sendReply(m); break; default: EV << "Error! got packet with unknown kind: " << msg->getKind() << endl; delete msg; } }
void SensorApplLayer::handleSelfMsg | ( | cMessage * | msg | ) | [protected, virtual] |
Handle self messages such as timer...
A timer with kind = SEND_DATA_TIMER indicates that a new data has to be send (sendData).
There are no other timers implemented for this module.
Implements BaseLayer.
Definition at line 216 of file SensorApplLayer.cc.
References sendData().
{ switch (msg->getKind()) { case SEND_DATA_TIMER: sendData(); //delete msg; break; default: EV<< "Unkown selfmessage! -> delete, kind: " << msg->getKind() << endl; delete msg; } }
virtual void SensorApplLayer::handleUpperMsg | ( | cMessage * | msg | ) | [inline, protected, virtual] |
Handle messages from upper layer.
This function is pure virtual here, because there is no reasonable guess what to do with it by default.
Implements BaseLayer.
Definition at line 125 of file SensorApplLayer.h.
{ delete m; }
cStdDev & SensorApplLayer::hostsLatency | ( | int | hostAddress | ) | [protected] |
Returns the latency statistics variable for the passed host address.
hostAddress | the address of the host to return the statistics for. |
Definition at line 103 of file SensorApplLayer.cc.
Referenced by handleLowerMsg().
{ if(latencies.count(hostAddress) == 0) { std::ostringstream oss; oss << hostAddress; cStdDev aLatency(oss.str().c_str()); latencies.insert(pair<int, cStdDev>(hostAddress, aLatency)); } return latencies[hostAddress]; }
void SensorApplLayer::initialize | ( | int | stage | ) | [virtual] |
Initialization of the module and some variables.
First we have to initialize the module from which we derived ours, in this case BasicModule.
Then we will set a timer to indicate the first time we will send a message
Reimplemented from BaseLayer.
Definition at line 35 of file SensorApplLayer.cc.
References debug, BaseModule::findHost(), Blackboard::getCategory(), initializeDistribution(), and AddressingInterface::myNetwAddr().
{ BaseLayer::initialize(stage); if (stage == 0) { debugEV<< "in initialize() stage 0..."; debug = par("debug"); stats = par("stats"); trace = par("trace"); nbPackets = par("nbPackets"); trafficParam = par("trafficParam"); initializationTime = par("initializationTime"); broadcastPackets = par("broadcastPackets"); headerLength = par("headerLength"); // application configuration const char *traffic = par("trafficType"); destAddr = par("destAddr"); nbPacketsSent = 0; nbPacketsReceived = 0; firstPacketGeneration = -1; lastPacketReception = -2; initializeDistribution(traffic); delayTimer = new cMessage("appDelay", SEND_DATA_TIMER); // Blackboard stuff: hostID = getParentModule()->getId(); // get pointer to the world module world = FindModule<BaseWorldUtility*>::findGlobalModule(); } else if (stage == 1) { debugEV << "in initialize() stage 1..."; // Application address configuration: equals to host address cModule *netw = FindModule<BaseNetwLayer*>::findSubModule(findHost()); if(!netw) { netw = findHost()->getSubmodule("netw"); if(!netw) { opp_error("Could not find network layer module. This means " "either no network layer module is present or the " "used network layer module does not subclass from " "BaseNetworkLayer."); } } AddressingInterface* addrScheme = FindModule<AddressingInterface*> ::findSubModule(findHost()); if(addrScheme) { myAppAddr = addrScheme->myNetwAddr(netw); } else { myAppAddr = netw->getId(); } sentPackets = 0; catPacket = world->getCategory(&packet); // first packet generation time is always chosen uniformly // to avoid systematic collisions if(nbPackets> 0) scheduleAt(simTime() +uniform(initializationTime, initializationTime + trafficParam), delayTimer); if (stats) { latenciesRaw.setName("rawLatencies"); latenciesRaw.setUnit("s"); latency.setName("latency"); } } }
void SensorApplLayer::sendData | ( | ) | [protected, virtual] |
send a data packet to the next hop
This function creates a new data message and sends it down to the network layer.
Definition at line 236 of file SensorApplLayer.cc.
References Blackboard::publishBBItem(), scheduleNextPacket(), and BaseLayer::sendDown().
Referenced by handleSelfMsg().
{ ApplPkt *pkt = new ApplPkt("Data", DATA_MESSAGE); if(broadcastPackets) { pkt->setDestAddr(L3BROADCAST); } else { pkt->setDestAddr(destAddr); } pkt->setSrcAddr(myAppAddr); pkt->setByteLength(headerLength); // set the control info to tell the network layer the layer 3 address pkt->setControlInfo(new NetwControlInfo(pkt->getDestAddr())); debugEV<< "Sending data packet!\n"; sendDown(pkt); nbPacketsSent++; packet.setPacketSent(true); packet.setNbPacketsSent(1); packet.setNbPacketsReceived(0); packet.setHost(myAppAddr); world->publishBBItem(catPacket, &packet, hostID); sentPackets++; scheduleNextPacket(); }