OverSim
RealworldConnector.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include "RealworldConnector.h"
25 
26 #include <string.h>
27 #include <omnetpp.h>
28 
29 
31 {
32  packetNotification = NULL;
33 }
34 
36 {
37  cancelAndDelete(packetNotification);
38 }
39 
41 {
42  if (stage==3) {
43  // update display string when addresses have been autoconfigured etc.
45  return;
46  }
47 
48  // all initialization is done in the first stage
49  if (stage!=0)
50  return;
51 
52  packetNotification = new cMessage("packetNotification");
53  mtu = par("mtu");
54 
55  scheduler = check_and_cast<RealtimeScheduler *>(simulation.getScheduler());
57  mtu, isApp());
58 
59  if (!isApp() ) {
60  parser = check_and_cast<PacketParser*>(
61  getParentModule()->getSubmodule("packetParser"));
62  } else {
63  parser = check_and_cast<PacketParser*>(
64  getParentModule()->getSubmodule("applicationParser"));
65  }
66 
68  WATCH(numSent);
69  WATCH(numRcvdOK);
70  WATCH(numRcvError);
71  WATCH(numSendError);
72 
73  if (!isApp()) {
74  gateIndexNetwOut = gate("netwOut")->getId();
75  } else {
76  gateIndexNetwOut = gate("to_lowerTier")->getId();
77  }
78 
79 }
80 
81 
83 {
84  // Packet from the real world...
85  if (msg==packetNotification) {
86  EV << "[RealworldConnector::handleMessage()]\n"
87  << " Message from outside. Queue length = "
88  << packetBuffer.size() << endl;
89 
90  while( packetBuffer.size() > 0 ) {
91  // get packet from buffer and parse it
92 
93  PacketBufferEntry packet = *(packetBuffer.begin());
94  packetBuffer.pop_front();
95  char* buf = packet.data;
96  uint32_t len = packet.length;
97  sockaddr* addr = packet.addr;
98  socklen_t addrlen = packet.addrlen;
99  cMessage *parsedPacket = decapsulate(buf, len, addr, addrlen);
100  if (parsedPacket) {
101  numRcvdOK++;
102  send(parsedPacket, gateIndexNetwOut);
103  } else {
104  numRcvError++;
105  }
106 
107  }
108  } else // arrived on gate "netwIn"
109  {
110  // Packet from inside, send to real word
111  EV << "[RealworldConnector::handleMessage()]\n"
112  << " Received " << msg << " for transmission"
113  << endl;
114 
115  transmitToNetwork(check_and_cast<cPacket*>(msg));
116  }
117 
118  if (ev.isGUI())
120 
121 }
122 
124 {
125  unsigned int length;
126  sockaddr* addr = 0;
127  socklen_t addrlen = 0;
128  char* buf = encapsulate(msg, &length, &addr, &addrlen);
129  if (buf) {
130  numSent++;
131  int nByte = scheduler->sendBytes(buf, length, addr, addrlen, isApp());
132 
133  if (nByte < 0) {
134  EV << "[RealworldConnector::transmitToNetwork()]\n"
135  << " Error sending Packet, sendBytes returned " << nByte
136  << endl;
137 
138  numSendError++;
139  } else {
140  EV << "[RealworldConnector::transmitToNetwork()]\n"
141  << " Packet (size = " << nByte << ") sent"
142  << endl;
143  }
144  } else {
145  numSendError++;
146  }
147 
148  delete[] buf;
149  delete addr;
150 }
151 
153 {
154  char buf[80];
155 
156  if (ev.isDisabled()) {
157  // speed up things
158  getDisplayString().setTagArg("t",0,"");
159  }
160 
161  sprintf(buf, "rcv:%ld snt:%ld", numRcvdOK, numSent);
162 
163  if (numRcvError>0)
164  sprintf(buf+strlen(buf), "\nerrin:%ld errout:%ld", numRcvError,
165  numSendError);
166 
167  getDisplayString().setTagArg("t",0,buf);
168 }
169