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.
00030 { 00031 packetNotification = NULL; 00032 }
RealworldConnector::~RealworldConnector | ( | ) | [virtual] |
Definition at line 34 of file RealworldConnector.cc.
00035 { 00036 cancelAndDelete(packetNotification); 00037 }
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.
00082 { 00083 // Packet from the real world... 00084 if (msg==packetNotification) { 00085 EV << "[RealworldConnector::handleMessage()]\n" 00086 << " Message from outside. Queue length = " 00087 << packetBuffer.size() << endl; 00088 00089 while( packetBuffer.size() > 0 ) { 00090 // get packet from buffer and parse it 00091 00092 RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin()); 00093 packetBuffer.pop_front(); 00094 char* buf = packet.data; 00095 uint32_t len = packet.length; 00096 sockaddr* addr = packet.addr; 00097 socklen_t addrlen = packet.addrlen; 00098 cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen); 00099 if (parsedPacket) { 00100 numRcvdOK++; 00101 send(parsedPacket, gateIndexNetwOut); 00102 } else { 00103 numRcvError++; 00104 } 00105 00106 } 00107 } else // arrived on gate "netwIn" 00108 { 00109 // Packet from inside, send to real word 00110 EV << "[RealworldConnector::handleMessage()]\n" 00111 << " Received " << msg << " for transmission" 00112 << endl; 00113 00114 transmitToNetwork(check_and_cast<cPacket*>(msg)); 00115 } 00116 00117 if (ev.isGUI()) 00118 updateDisplayString(); 00119 00120 }
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.
00040 { 00041 if (stage==3) { 00042 // update display string when addresses have been autoconfigured etc. 00043 updateDisplayString(); 00044 return; 00045 } 00046 00047 // all initialization is done in the first stage 00048 if (stage!=0) 00049 return; 00050 00051 packetNotification = new cMessage("packetNotification"); 00052 mtu = par("mtu"); 00053 00054 scheduler = check_and_cast<RealtimeScheduler *>(simulation.getScheduler()); 00055 scheduler->setInterfaceModule(this, packetNotification, &packetBuffer, 00056 mtu, isApp()); 00057 00058 if (!isApp() ) { 00059 parser = check_and_cast<PacketParser*>( 00060 getParentModule()->getSubmodule("packetParser")); 00061 } else { 00062 parser = check_and_cast<PacketParser*>( 00063 getParentModule()->getSubmodule("applicationParser")); 00064 } 00065 00066 numSent = numRcvdOK = numRcvError = numSendError = 0; 00067 WATCH(numSent); 00068 WATCH(numRcvdOK); 00069 WATCH(numRcvError); 00070 WATCH(numSendError); 00071 00072 if (!isApp()) { 00073 gateIndexNetwOut = gate("netwOut")->getId(); 00074 } else { 00075 gateIndexNetwOut = gate("to_lowerTier")->getId(); 00076 } 00077 00078 }
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().
virtual int RealworldConnector::numInitStages | ( | void | ) | const [inline, virtual] |
Reimplemented in RealworldDevice.
Definition at line 130 of file RealworldConnector.h.
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().
00123 { 00124 unsigned int length; 00125 sockaddr* addr = 0; 00126 socklen_t addrlen = 0; 00127 char* buf = encapsulate(msg, &length, &addr, &addrlen); 00128 if (buf) { 00129 numSent++; 00130 int nByte = scheduler->sendBytes(buf, length, addr, addrlen, isApp()); 00131 00132 if (nByte < 0) { 00133 EV << "[RealworldConnector::transmitToNetwork()]\n" 00134 << " Error sending Packet, sendBytes returned " << nByte 00135 << endl; 00136 00137 numSendError++; 00138 } else { 00139 EV << "[RealworldConnector::transmitToNetwork()]\n" 00140 << " Packet (size = " << nByte << ") sent" 00141 << endl; 00142 } 00143 } else { 00144 numSendError++; 00145 } 00146 00147 delete[] buf; 00148 delete addr; 00149 }
void RealworldConnector::updateDisplayString | ( | ) | [protected, virtual] |
Definition at line 151 of file RealworldConnector.cc.
Referenced by handleMessage(), and initialize().
00152 { 00153 char buf[80]; 00154 00155 if (ev.isDisabled()) { 00156 // speed up things 00157 getDisplayString().setTagArg("t",0,""); 00158 } 00159 00160 sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent); 00161 00162 if (numRcvError>0) 00163 sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError, 00164 numSendError); 00165 00166 getDisplayString().setTagArg("t",0,buf); 00167 }
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().