OverSim
RealworldConnector Class Reference

RealworldConnector is a pseudo interface that allows communcation with the real world through the TunOutScheduler. More...

#include <RealworldConnector.h>

Inheritance diagram for RealworldConnector:
RealworldApp RealworldDevice TunOutDevice UdpOutDevice

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
PacketBuffer packetBuffer
RealtimeSchedulerscheduler
PacketParserparser

Detailed Description

RealworldConnector is a pseudo interface that allows communcation with the real world through the TunOutScheduler.

Definition at line 66 of file RealworldConnector.h.

Constructor & Destructor Documentation

RealworldConnector::RealworldConnector ( )

Definition at line 30 of file RealworldConnector.cc.

{
}
RealworldConnector::~RealworldConnector ( )
virtual

Definition at line 35 of file RealworldConnector.cc.

{
cancelAndDelete(packetNotification);
}

Member Function Documentation

virtual cPacket* RealworldConnector::decapsulate ( char *  buf,
uint32_t  length,
sockaddr *  addr,
socklen_t  addrlen 
)
protectedpure virtual

Parses data received from the (realworld) network and converts it into a cMessage.

Parameters
bufA pointer to the data to be parsed
lengthThe lenght of the buffer in bytes
addrIf needed, the destination address
addrlenIf needed, the length of the address
Returns
The parsed message

Implemented in TunOutDevice, UdpOutDevice, and RealworldApp.

Referenced by handleMessage().

virtual char* RealworldConnector::encapsulate ( cPacket *  msg,
unsigned int *  length,
sockaddr **  addr,
socklen_t *  addrlen 
)
protectedpure virtual

Converts an IP datagram to a data block for sending it to the (realworld) network.

Parameters
msgA pointer to the message to be converted
lengthA pointer to an int that will hold the length of the converted data
addrIf needed, the destination address
addrlenIf needed, the length of the address
Returns
A pointer to the converted data

Implemented in TunOutDevice, UdpOutDevice, and RealworldApp.

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 82 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
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) {
send(parsedPacket, gateIndexNetwOut);
} else {
}
}
} 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())
}
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 40 of file RealworldConnector.cc.

{
if (stage==3) {
// update display string when addresses have been autoconfigured etc.
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());
mtu, isApp());
if (!isApp() ) {
parser = check_and_cast<PacketParser*>(
getParentModule()->getSubmodule("packetParser"));
} else {
parser = check_and_cast<PacketParser*>(
getParentModule()->getSubmodule("applicationParser"));
}
WATCH(numSent);
WATCH(numRcvdOK);
WATCH(numRcvError);
WATCH(numSendError);
if (!isApp()) {
gateIndexNetwOut = gate("netwOut")->getId();
} else {
gateIndexNetwOut = gate("to_lowerTier")->getId();
}
}
virtual bool RealworldConnector::isApp ( )
inlineprotectedvirtual

If the Connector connects to an application, this method has to be overwritten to return "true".

Returns
false

Reimplemented in RealworldApp.

Definition at line 124 of file RealworldConnector.h.

Referenced by initialize(), and transmitToNetwork().

{return false;}
virtual int RealworldConnector::numInitStages ( void  ) const
inlinevirtual

Reimplemented in RealworldDevice.

Definition at line 130 of file RealworldConnector.h.

{
return 4;
}
void RealworldConnector::transmitToNetwork ( cPacket *  msg)
protectedvirtual

Send a message to the (realworld) network.

Parameters
msgA pointer to the message to be send

Definition at line 123 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) {
int nByte = scheduler->sendBytes(buf, length, addr, addrlen, isApp());
if (nByte < 0) {
EV << "[RealworldConnector::transmitToNetwork()]\n"
<< " Error sending Packet, sendBytes returned " << nByte
<< endl;
} else {
EV << "[RealworldConnector::transmitToNetwork()]\n"
<< " Packet (size = " << nByte << ") sent"
<< endl;
}
} else {
}
delete[] buf;
delete addr;
}
void RealworldConnector::updateDisplayString ( )
protectedvirtual

Definition at line 152 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,
getDisplayString().setTagArg("t",0,buf);
}

Member Data Documentation

int RealworldConnector::gateIndexNetwOut
protected

Definition at line 70 of file RealworldConnector.h.

Referenced by handleMessage(), and initialize().

unsigned int RealworldConnector::mtu
protected
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().

PacketBuffer RealworldConnector::packetBuffer
protected

Definition at line 81 of file RealworldConnector.h.

Referenced by handleMessage(), and initialize().

cMessage* RealworldConnector::packetNotification
protected
RealtimeScheduler* RealworldConnector::scheduler
protected

Definition at line 82 of file RealworldConnector.h.

Referenced by initialize(), and transmitToNetwork().


The documentation for this class was generated from the following files: