#include <RealworldConnector.h>
Public Member Functions | |
Module_Class_Members (RealworldConnector, cSimpleModule, 0) | |
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 (cMessage *msg) |
Send a message to the (realworld) network. | |
virtual void | updateDisplayString () |
virtual char * | encapsulate (cMessage *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 cMessage * | 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 |
void RealworldConnector::transmitToNetwork | ( | cMessage * | msg | ) | [protected, virtual] |
Send a message to the (realworld) network.
msg | A pointer to the message to be send |
Referenced by handleMessage().
00114 { 00115 unsigned int length; 00116 sockaddr* addr = 0; 00117 socklen_t addrlen = 0; 00118 char* buf = encapsulate(msg, &length, &addr, &addrlen); 00119 if (buf) { 00120 numSent++; 00121 int nByte = scheduler->sendBytes(buf, length, addr, addrlen, isApp()); 00122 00123 if (nByte < 0) { 00124 EV << "[RealworldConnector::transmitToNetwork()]\n" 00125 << " Error sending Packet, sendBytes returned " << nByte 00126 << endl; 00127 00128 numSendError++; 00129 } else { 00130 EV << "[RealworldConnector::transmitToNetwork()]\n" 00131 << " Packet (size = " << nByte << ") sent" 00132 << endl; 00133 } 00134 } else { 00135 numSendError++; 00136 } 00137 00138 delete[] buf; 00139 delete addr; 00140 }
void RealworldConnector::updateDisplayString | ( | ) | [protected, virtual] |
Referenced by handleMessage(), and initialize().
00143 { 00144 char buf[80]; 00145 00146 if (ev.disabled()) { 00147 // speed up things 00148 displayString().setTagArg("t",0,""); 00149 } 00150 00151 sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent); 00152 00153 if (numRcvError>0) 00154 sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError, 00155 numSendError); 00156 00157 displayString().setTagArg("t",0,buf); 00158 }
virtual char* RealworldConnector::encapsulate | ( | cMessage * | 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().
virtual cMessage* 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 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.
Referenced by initialize(), and transmitToNetwork().
RealworldConnector::Module_Class_Members | ( | RealworldConnector | , | |
cSimpleModule | , | |||
0 | ||||
) |
virtual int RealworldConnector::numInitStages | ( | void | ) | const [inline, virtual] |
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.
Referenced by RealworldDevice::initialize().
00031 { 00032 if (stage==3) { 00033 // update display string when addresses have been autoconfigured etc. 00034 updateDisplayString(); 00035 return; 00036 } 00037 00038 // all initialization is done in the first stage 00039 if (stage!=0) 00040 return; 00041 00042 packetNotification = new cMessage("packetNotification"); 00043 mtu = par("mtu"); 00044 00045 scheduler = check_and_cast<RealtimeScheduler *>(simulation.scheduler()); 00046 scheduler->setInterfaceModule(this, packetNotification, &packetBuffer, 00047 mtu, isApp()); 00048 00049 if (!isApp() ) { 00050 parser = check_and_cast<PacketParser*>( 00051 parentModule()->submodule("packetParser")); 00052 } else { 00053 parser = check_and_cast<PacketParser*>( 00054 parentModule()->submodule("applicationParser")); 00055 } 00056 00057 numSent = numRcvdOK = numRcvError = numSendError = 0; 00058 WATCH(numSent); 00059 WATCH(numRcvdOK); 00060 WATCH(numRcvError); 00061 WATCH(numSendError); 00062 00063 if (!isApp()) { 00064 gateIndexNetwOut = gate("netwOut")->id(); 00065 } else { 00066 gateIndexNetwOut = gate("to_lowerTier")->id(); 00067 } 00068 00069 }
void RealworldConnector::handleMessage | ( | cMessage * | msg | ) | [virtual] |
The "main loop".
Every message that is received or send is handled by this method
00073 { 00074 // Packet from the real world... 00075 if (msg==packetNotification) { 00076 EV << "[RealworldConnector::handleMessage()]\n" 00077 << " Message from outside. Queue length = " 00078 << packetBuffer.size() << endl; 00079 00080 while( packetBuffer.size() > 0 ) { 00081 // get packet from buffer and parse it 00082 00083 RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin()); 00084 packetBuffer.pop_front(); 00085 char* buf = packet.data; 00086 uint32_t len = packet.length; 00087 sockaddr* addr = packet.addr; 00088 socklen_t addrlen = packet.addrlen; 00089 cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen); 00090 if (parsedPacket) { 00091 numRcvdOK++; 00092 send(parsedPacket, gateIndexNetwOut); 00093 } else { 00094 numRcvError++; 00095 } 00096 00097 } 00098 } else // arrived on gate "netwIn" 00099 { 00100 // Packet from inside, send to real word 00101 EV << "[RealworldConnector::handleMessage()]\n" 00102 << " Received " << msg << " for transmission" 00103 << endl; 00104 00105 transmitToNetwork(msg); 00106 } 00107 00108 if (ev.isGUI()) 00109 updateDisplayString(); 00110 00111 }
int RealworldConnector::gateIndexNetwOut [protected] |
Referenced by handleMessage(), and initialize().
unsigned int RealworldConnector::mtu [protected] |
long RealworldConnector::numSent [protected] |
Referenced by initialize(), transmitToNetwork(), and updateDisplayString().
long RealworldConnector::numSendError [protected] |
Referenced by initialize(), transmitToNetwork(), and updateDisplayString().
long RealworldConnector::numRcvdOK [protected] |
Referenced by handleMessage(), initialize(), and updateDisplayString().
long RealworldConnector::numRcvError [protected] |
Referenced by handleMessage(), initialize(), and updateDisplayString().
cMessage* RealworldConnector::packetNotification [protected] |
Referenced by handleMessage(), and initialize().
Referenced by handleMessage(), and initialize().
RealtimeScheduler* RealworldConnector::scheduler [protected] |
Referenced by initialize(), and transmitToNetwork().
PacketParser* RealworldConnector::parser [protected] |