Public Member Functions | Protected Member Functions | Protected Attributes

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

List of all members.

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
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 29 of file RealworldConnector.cc.

{
    packetNotification = NULL;
}

RealworldConnector::~RealworldConnector (  )  [virtual]

Definition at line 34 of file RealworldConnector.cc.

{
    cancelAndDelete(packetNotification);
}


Member Function Documentation

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.

Parameters:
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
Returns:
The parsed message

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.

Parameters:
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
Returns:
A pointer to the converted data

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".

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 [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.

Parameters:
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);
}


Member Data Documentation

Definition at line 70 of file RealworldConnector.h.

Referenced by handleMessage(), and initialize().

unsigned int RealworldConnector::mtu [protected]

Definition at line 77 of file RealworldConnector.h.

Referenced by handleMessage(), initialize(), and updateDisplayString().

Definition at line 78 of file RealworldConnector.h.

Referenced by handleMessage(), initialize(), and updateDisplayString().

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 82 of file RealworldConnector.h.

Referenced by initialize(), and transmitToNetwork().


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