#include <RealworldConnector.h>
Inheritance diagram for RealworldConnector:
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 |
00107 { 00108 unsigned int length; 00109 sockaddr* addr = 0; 00110 socklen_t addrlen = 0; 00111 char* buf = encapsulate(msg, &length, &addr, &addrlen); 00112 if( buf ) { 00113 numSent++; 00114 int nByte = scheduler->sendBytes( buf, length, addr, addrlen, isApp() ); 00115 if (nByte < 0) { 00116 EV << "Error sending Packet, sendBytes returned " << nByte << "\n"; 00117 numSendError++; 00118 } else { 00119 EV << "Packet (size = " << nByte << ") send\n"; 00120 } 00121 } else { 00122 numSendError++; 00123 } 00124 delete buf; 00125 delete addr; 00126 }
void RealworldConnector::updateDisplayString | ( | ) | [protected, virtual] |
00129 { 00130 char buf[80]; 00131 if (ev.disabled()) { 00132 // speed up things 00133 displayString().setTagArg("t",0,""); 00134 } 00135 sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent); 00136 00137 if (numRcvError>0) 00138 sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError, numSendError); 00139 00140 displayString().setTagArg("t",0,buf); 00141 }
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: Message from outside. Queue length = " << packetBuffer.size() << ".\n"; 00075 while( packetBuffer.size() > 0 ) { 00076 // get packet from buffer and parse it 00077 00078 RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin()); 00079 packetBuffer.pop_front(); 00080 char* buf = packet.data; 00081 uint32_t len = packet.length; 00082 sockaddr* addr = packet.addr; 00083 socklen_t addrlen = packet.addrlen; 00084 cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen); 00085 if (parsedPacket) { 00086 numRcvdOK++; 00087 send(parsedPacket, gateIndexNetwOut); 00088 } else { 00089 numRcvError++; 00090 } 00091 00092 } 00093 } else // arrived on gate "netwIn" 00094 { 00095 // Packet from inside, send to real word 00096 EV << "Received " << msg << " for transmission\n"; 00097 00098 transmitToNetwork(msg); 00099 } 00100 00101 if (ev.isGUI()) 00102 updateDisplayString(); 00103 00104 }
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] |