RealworldConnector is a pseudo interface that allows communcation with the real world through the TunOutScheduler. More...
#include <RealworldConnector.h>
Public Member Functions | |
| RealworldConnector () | |
| virtual | ~RealworldConnector () |
| virtual int | numInitStages () const |
| virtual void | initialize (int stage) |
| Initialization of the module. | |
| virtual void | handleMessage (cMessage *msg) |
| The "main loop". | |
Protected Member Functions | |
| virtual void | transmitToNetwork (cPacket *msg) |
| Send a message to the (realworld) network. | |
| virtual void | updateDisplayString () |
| virtual char * | encapsulate (cPacket *msg, unsigned int *length, sockaddr **addr, socklen_t *addrlen)=0 |
| Converts an IP datagram to a data block for sending it to the (realworld) network. | |
| virtual cPacket * | decapsulate (char *buf, uint32_t length, sockaddr *addr, socklen_t addrlen)=0 |
| Parses data received from the (realworld) network and converts it into a cMessage. | |
| virtual bool | isApp () |
| If the Connector connects to an application, this method has to be overwritten to return "true". | |
Protected Attributes | |
| int | gateIndexNetwOut |
| unsigned int | mtu |
| long | numSent |
| long | numSendError |
| long | numRcvdOK |
| long | numRcvError |
| cMessage * | packetNotification |
| RealtimeScheduler::PacketBuffer | packetBuffer |
| RealtimeScheduler * | scheduler |
| PacketParser * | parser |
RealworldConnector is a pseudo interface that allows communcation with the real world through the TunOutScheduler.
Definition at line 66 of file RealworldConnector.h.
| RealworldConnector::RealworldConnector | ( | ) |
Definition at line 29 of file RealworldConnector.cc.
{
packetNotification = NULL;
}
| RealworldConnector::~RealworldConnector | ( | ) | [virtual] |
Definition at line 34 of file RealworldConnector.cc.
{
cancelAndDelete(packetNotification);
}
| virtual cPacket* RealworldConnector::decapsulate | ( | char * | buf, | |
| uint32_t | length, | |||
| sockaddr * | addr, | |||
| socklen_t | addrlen | |||
| ) | [protected, pure virtual] |
Parses data received from the (realworld) network and converts it into a cMessage.
| buf | A pointer to the data to be parsed | |
| length | The lenght of the buffer in bytes | |
| addr | If needed, the destination address | |
| addrlen | If needed, the length of the address |
Implemented in RealworldApp, TunOutDevice, and UdpOutDevice.
Referenced by handleMessage().
| virtual char* RealworldConnector::encapsulate | ( | cPacket * | msg, | |
| unsigned int * | length, | |||
| sockaddr ** | addr, | |||
| socklen_t * | addrlen | |||
| ) | [protected, pure virtual] |
Converts an IP datagram to a data block for sending it to the (realworld) network.
| msg | A pointer to the message to be converted | |
| length | A pointer to an int that will hold the length of the converted data | |
| addr | If needed, the destination address | |
| addrlen | If needed, the length of the address |
Implemented in RealworldApp, TunOutDevice, and UdpOutDevice.
Referenced by transmitToNetwork().
| void RealworldConnector::handleMessage | ( | cMessage * | msg | ) | [virtual] |
The "main loop".
Every message that is received or send is handled by this method
Definition at line 81 of file RealworldConnector.cc.
{
// Packet from the real world...
if (msg==packetNotification) {
EV << "[RealworldConnector::handleMessage()]\n"
<< " Message from outside. Queue length = "
<< packetBuffer.size() << endl;
while( packetBuffer.size() > 0 ) {
// get packet from buffer and parse it
RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin());
packetBuffer.pop_front();
char* buf = packet.data;
uint32_t len = packet.length;
sockaddr* addr = packet.addr;
socklen_t addrlen = packet.addrlen;
cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen);
if (parsedPacket) {
numRcvdOK++;
send(parsedPacket, gateIndexNetwOut);
} else {
numRcvError++;
}
}
} else // arrived on gate "netwIn"
{
// Packet from inside, send to real word
EV << "[RealworldConnector::handleMessage()]\n"
<< " Received " << msg << " for transmission"
<< endl;
transmitToNetwork(check_and_cast<cPacket*>(msg));
}
if (ev.isGUI())
updateDisplayString();
}
| void RealworldConnector::initialize | ( | int | stage | ) | [virtual] |
Initialization of the module.
Registers the device at the scheduler and searches for the appropriate payload-parser Will be called automatically at startup
Reimplemented in RealworldDevice.
Definition at line 39 of file RealworldConnector.cc.
{
if (stage==3) {
// update display string when addresses have been autoconfigured etc.
updateDisplayString();
return;
}
// all initialization is done in the first stage
if (stage!=0)
return;
packetNotification = new cMessage("packetNotification");
mtu = par("mtu");
scheduler = check_and_cast<RealtimeScheduler *>(simulation.getScheduler());
scheduler->setInterfaceModule(this, packetNotification, &packetBuffer,
mtu, isApp());
if (!isApp() ) {
parser = check_and_cast<PacketParser*>(
getParentModule()->getSubmodule("packetParser"));
} else {
parser = check_and_cast<PacketParser*>(
getParentModule()->getSubmodule("applicationParser"));
}
numSent = numRcvdOK = numRcvError = numSendError = 0;
WATCH(numSent);
WATCH(numRcvdOK);
WATCH(numRcvError);
WATCH(numSendError);
if (!isApp()) {
gateIndexNetwOut = gate("netwOut")->getId();
} else {
gateIndexNetwOut = gate("to_lowerTier")->getId();
}
}
| virtual bool RealworldConnector::isApp | ( | ) | [inline, protected, virtual] |
If the Connector connects to an application, this method has to be overwritten to return "true".
Reimplemented in RealworldApp.
Definition at line 124 of file RealworldConnector.h.
Referenced by initialize(), and transmitToNetwork().
{return false;}
| virtual int RealworldConnector::numInitStages | ( | void | ) | const [inline, virtual] |
Reimplemented in RealworldDevice.
Definition at line 130 of file RealworldConnector.h.
{
return 4;
}
| void RealworldConnector::transmitToNetwork | ( | cPacket * | msg | ) | [protected, virtual] |
Send a message to the (realworld) network.
| msg | A pointer to the message to be send |
Definition at line 122 of file RealworldConnector.cc.
Referenced by handleMessage().
{
unsigned int length;
sockaddr* addr = 0;
socklen_t addrlen = 0;
char* buf = encapsulate(msg, &length, &addr, &addrlen);
if (buf) {
numSent++;
int nByte = scheduler->sendBytes(buf, length, addr, addrlen, isApp());
if (nByte < 0) {
EV << "[RealworldConnector::transmitToNetwork()]\n"
<< " Error sending Packet, sendBytes returned " << nByte
<< endl;
numSendError++;
} else {
EV << "[RealworldConnector::transmitToNetwork()]\n"
<< " Packet (size = " << nByte << ") sent"
<< endl;
}
} else {
numSendError++;
}
delete[] buf;
delete addr;
}
| void RealworldConnector::updateDisplayString | ( | ) | [protected, virtual] |
Definition at line 151 of file RealworldConnector.cc.
Referenced by handleMessage(), and initialize().
{
char buf[80];
if (ev.isDisabled()) {
// speed up things
getDisplayString().setTagArg("t",0,"");
}
sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent);
if (numRcvError>0)
sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError,
numSendError);
getDisplayString().setTagArg("t",0,buf);
}
int RealworldConnector::gateIndexNetwOut [protected] |
Definition at line 70 of file RealworldConnector.h.
Referenced by handleMessage(), and initialize().
unsigned int RealworldConnector::mtu [protected] |
Definition at line 71 of file RealworldConnector.h.
Referenced by UdpOutDevice::encapsulate(), initialize(), and RealworldDevice::registerInterface().
long RealworldConnector::numRcvdOK [protected] |
Definition at line 77 of file RealworldConnector.h.
Referenced by handleMessage(), initialize(), and updateDisplayString().
long RealworldConnector::numRcvError [protected] |
Definition at line 78 of file RealworldConnector.h.
Referenced by handleMessage(), initialize(), and updateDisplayString().
long RealworldConnector::numSendError [protected] |
Definition at line 76 of file RealworldConnector.h.
Referenced by initialize(), transmitToNetwork(), and updateDisplayString().
long RealworldConnector::numSent [protected] |
Definition at line 75 of file RealworldConnector.h.
Referenced by initialize(), transmitToNetwork(), and updateDisplayString().
Definition at line 81 of file RealworldConnector.h.
Referenced by handleMessage(), and initialize().
cMessage* RealworldConnector::packetNotification [protected] |
Definition at line 80 of file RealworldConnector.h.
Referenced by handleMessage(), initialize(), RealworldConnector(), and ~RealworldConnector().
PacketParser* RealworldConnector::parser [protected] |
Definition at line 83 of file RealworldConnector.h.
Referenced by UdpOutDevice::decapsulate(), RealworldApp::decapsulate(), UdpOutDevice::encapsulate(), RealworldApp::encapsulate(), and initialize().
RealtimeScheduler* RealworldConnector::scheduler [protected] |
Definition at line 82 of file RealworldConnector.h.
Referenced by initialize(), and transmitToNetwork().
1.7.1