#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 |
00111 { 00112 unsigned int length; 00113 sockaddr* addr = 0; 00114 socklen_t addrlen = 0; 00115 char* buf = encapsulate(msg, &length, &addr, &addrlen); 00116 if( buf ) { 00117 numSent++; 00118 int nByte = scheduler->sendBytes( buf, length, addr, addrlen, isApp() ); 00119 if (nByte < 0) { 00120 EV << "[RealworldConnector::transmitToNetwork()]\n" 00121 << " Error sending Packet, sendBytes returned " << nByte 00122 << endl; 00123 numSendError++; 00124 } else { 00125 EV << "[RealworldConnector::transmitToNetwork()]\n" 00126 << " Packet (size = " << nByte << ") sent" 00127 << endl; 00128 } 00129 } else { 00130 numSendError++; 00131 } 00132 delete buf; 00133 delete addr; 00134 }
void RealworldConnector::updateDisplayString | ( | ) | [protected, virtual] |
00137 { 00138 char buf[80]; 00139 if (ev.disabled()) { 00140 // speed up things 00141 displayString().setTagArg("t",0,""); 00142 } 00143 sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent); 00144 00145 if (numRcvError>0) 00146 sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError, numSendError); 00147 00148 displayString().setTagArg("t",0,buf); 00149 }
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.
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.
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.
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.
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, mtu, isApp()); 00047 00048 // parser = check_and_cast<PacketParser*>(parentModule()->submodule("packetParser")); 00049 if (!isApp() ) { 00050 parser = check_and_cast<PacketParser*>(parentModule()->submodule("packetParser")); 00051 } else { 00052 parser = check_and_cast<PacketParser*>(parentModule()->submodule("applicationParser")); 00053 } 00054 00055 numSent = numRcvdOK = numRcvError = numSendError = 0; 00056 WATCH(numSent); 00057 WATCH(numRcvdOK); 00058 WATCH(numRcvError); 00059 WATCH(numSendError); 00060 00061 if (!isApp() ) { 00062 gateIndexNetwOut = gate("netwOut")->id(); 00063 } else { 00064 gateIndexNetwOut = gate("to_lowerTier")->id(); 00065 } 00066 00067 }
void RealworldConnector::handleMessage | ( | cMessage * | msg | ) | [virtual] |
The "main loop".
Every message that is received or send is handled by this method
00071 { 00072 // Packet from the real world... 00073 if (msg==packetNotification) { 00074 EV << "[RealworldConnector::handleMessage()]\n" 00075 << " Message from outside. Queue length = " << packetBuffer.size() 00076 << endl; 00077 00078 while( packetBuffer.size() > 0 ) { 00079 // get packet from buffer and parse it 00080 00081 RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin()); 00082 packetBuffer.pop_front(); 00083 char* buf = packet.data; 00084 uint32_t len = packet.length; 00085 sockaddr* addr = packet.addr; 00086 socklen_t addrlen = packet.addrlen; 00087 cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen); 00088 if (parsedPacket) { 00089 numRcvdOK++; 00090 send(parsedPacket, gateIndexNetwOut); 00091 } else { 00092 numRcvError++; 00093 } 00094 00095 } 00096 } else // arrived on gate "netwIn" 00097 { 00098 // Packet from inside, send to real word 00099 EV << "[RealworldConnector::handleMessage()]\n" 00100 << " Received " << msg << " for transmission" 00101 << endl; 00102 transmitToNetwork(msg); 00103 } 00104 00105 if (ev.isGUI()) 00106 updateDisplayString(); 00107 00108 }
int RealworldConnector::gateIndexNetwOut [protected] |
unsigned int RealworldConnector::mtu [protected] |
long RealworldConnector::numSent [protected] |
long RealworldConnector::numSendError [protected] |
long RealworldConnector::numRcvdOK [protected] |
long RealworldConnector::numRcvError [protected] |
cMessage* RealworldConnector::packetNotification [protected] |
RealtimeScheduler* RealworldConnector::scheduler [protected] |
PacketParser* RealworldConnector::parser [protected] |