RINASim  October 2016
Documentation of framework for OMNeT++
RMTModuleAllocator.cc
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #include "RMTModuleAllocator.h"
24 
26 
28 {
29  qMonPolicy = getRINAModule<RMTQMonitorBase*>(this, 1, {MOD_POL_RMT_QMONITOR});
30 
31  portCount = 0;
32  interfacePort = nullptr;
33 
34  // TODO: purge this crap and think of something smarter
35  // port module coordinates
36  portXCoord = 55;
37  portYCoord = 180;
38 
39  WATCH(portCount);
40 }
41 
42 
43 RMTQueue* RMTModuleAllocator::addQueue(RMTQueueType type, RMTPort* port, const char* queueId)
44 {
45  cModule* portWrapper = port->getParentModule();
46 
47  // generate a name
48  std::ostringstream queueName;
49  const char* strType = (type == RMTQueue::INPUT ? "inQ_" : "outQ_");
50  queueName << strType << queueId;
51 
52  RMTQueue* queue = lookup(port, type, queueName.str().c_str());
53  if (queue != nullptr)
54  {
55  EV << "addQueue(): Queue with this ID already exists!";
56  return queue;
57  }
58 
59  // instantiate a new module
60  cModuleType *moduleType = cModuleType::get(MOD_RMT_QUEUE);
61  cModule *newModule = moduleType->createScheduleInit(queueName.str().c_str(), portWrapper);
62  queue = dynamic_cast<RMTQueue*>(newModule);
63 
64  // create bindings to other modules
65  cModule* rmt = getRINAModule<cModule*>(this, 1, {MOD_RMT});
66  std::ostringstream combinedQueueName;
67  combinedQueueName << portWrapper->getFullName() << queueName.str();
68  if (type == RMTQueue::OUTPUT)
69  {
70  // connect to RMT submodule
71  cGate* rmtOut = rmt->addGate(combinedQueueName.str().c_str(), cGate::OUTPUT, false);
72  cGate* portBorder = portWrapper->addGate(queueName.str().c_str(), cGate::INPUT, false);
73  rmtOut->connectTo(portBorder);
74  portBorder->connectTo(queue->getInputGate());
75 
76  queue->setRMTAccessGate(rmtOut);
77 
78  // connect to port
79  cGate* fromOutputQueue = port->addGate(queue->getFullName(), cGate::INPUT, false);
80  queue->getOutputGate()->connectTo(fromOutputQueue);
81 
82  port->registerOutputQueue(queue);
83  }
84  else if (type == RMTQueue::INPUT)
85  {
86  // connect to RMT submodule
87  cGate* rmtIn = rmt->addGate(combinedQueueName.str().c_str(), cGate::INPUT, false);
88  cGate* portBorder = portWrapper->addGate(queueName.str().c_str(), cGate::OUTPUT, false);
89 
90  queue->getOutputGate()->connectTo(portBorder);
91  portBorder->connectTo(rmtIn);
92 
93  queue->setRMTAccessGate(rmtIn);
94 
95  // connect to port
96  cGate* toInputQueue = port->addGate(queue->getFullName(), cGate::OUTPUT, false);
97  toInputQueue->connectTo(queue->getInputGate());
98 
99  port->registerInputQueue(queue);
100  }
101 
102  queue->setType(type);
103  queueToPort[queue] = port;
105 
106  return queue;
107 }
108 
110 {
111  std::ostringstream portName;
112  portName << "p" << portCount;
113  portCount++;
114 
115  // initialize a wrapper with port inside it
116  cModuleType* moduleType = cModuleType::get(MOD_RMT_PORTWRAPPER);
117  cModule* portWrapper = moduleType->createScheduleInit(portName.str().c_str(), getParentModule());
118  RMTPort* port = check_and_cast<RMTPort*>(portWrapper->getSubmodule(MOD_RMTPORT));
119 
120  port->setFlow(flow);
121 
122  // modify the position a little
123  cDisplayString& portDisp = portWrapper->getDisplayString();
124  portDisp.setTagArg("p", 0, portXCoord);
125  portDisp.setTagArg("p", 1, portYCoord);
126  portXCoord += 120;
127 
128  if (flow == nullptr)
129  {
130  interfacePort = port;
131  }
132  else if (flow->isManagementFlow())
133  {
134  portDisp.setTagArg("i2", 0, "status/execute");
135  }
136 
137  // initialize SDU protection
138  std::ostringstream sdupInstanceName, sdupPolicyType;
139  const char* sdupParam = getParentModule()->par("sdupPolicyName");
140  sdupPolicyType << "rina.policies.DIF.SDUProtection." << sdupParam << "." << sdupParam;
141  sdupInstanceName << "sdup" << portCount;
142  moduleType = cModuleType::get(sdupPolicyType.str().c_str());
143  cModule* sdup = check_and_cast<IntSDUProtection*>(
144  moduleType->createScheduleInit(sdupInstanceName.str().c_str(), portWrapper));
145 
146  // modify the position a little
147  cDisplayString& sdupDisp = sdup->getDisplayString();
148  sdupDisp.setTagArg("p", 0, 150);
149  sdupDisp.setTagArg("p", 1, 228);
150  sdupDisp.setTagArg("i", 0, "block/encrypt");
151  sdupDisp.setTagArg("is", 0, "s");
152 
153  interconnectModules(port, sdup, std::string("protect"), std::string("protect"));
154  interconnectModules(port, sdup, std::string("unprotect"), std::string("unprotect"));
155 
156  return port;
157 }
158 
160 {
161  cModule* rmt = getRINAModule<cModule*>(this, 1, {MOD_RMT});
162  RMTPort* port = queueToPort[queue];
163  cGate* rmtGate = queue->getRMTAccessGate();
164  cGate* qOutputGate = queue->getOutputGate();
165  cGate* qInputGate = queue->getInputGate();
166 
167  if (queue->getType() == RMTQueue::OUTPUT)
168  {
169  rmtGate->getNextGate()->disconnect();
170  rmtGate->disconnect();
171  qOutputGate->disconnect();
172  port->unregisterOutputQueue(queue);
173  }
174  else
175  {
176  qOutputGate->getNextGate()->disconnect();
177  qOutputGate->disconnect();
178  qInputGate->getPreviousGate()->disconnect();
179  port->unregisterInputQueue(queue);
180  }
181 
182  rmt->deleteGate(rmtGate->getFullName());
183  port->deleteGate(queue->getFullName());
184  port->getParentModule()->deleteGate(queue->getFullName());
185 
186  qMonPolicy->preQueueRemoval(queue);
187  queue->callFinish();
188  queue->deleteModule();
189 }
190 
192 {
193  while (!queues.empty())
194  {
195  removeQueue(queues.back());
196  queues.pop_back();
197  }
198 }
199 
200 RMTQueue* RMTModuleAllocator::lookup(RMTPort* port, RMTQueueType type, const char* queueName)
201 {
202  RMTQueues queues = (type == RMTQueue::OUTPUT ? port->getOutputQueues() : port->getInputQueues());
203 
204  for(auto const q : queues)
205  {
206  if (!opp_strcmp(q->getName(), queueName))
207  {
208  return q;
209  }
210  }
211  return nullptr;
212 }
213 
215 {
216  removeQueues(port->getOutputQueues());
217  removeQueues(port->getInputQueues());
218 
219  port->callFinish();
220  port->getParentModule()->deleteModule();
221 }
222 
224 {
225  return queueToPort[queue];
226 }
227 
229 {
230  return interfacePort;
231 }
232 
234 {
235  cModule* portWrapper = getParentModule()->getSubmodule(name);
236  if (portWrapper)
237  {
238  return dynamic_cast<RMTPort*>(portWrapper->getSubmodule(MOD_RMTPORT));
239  }
240 
241  return nullptr;
242 }
void removeQueues(RMTQueues &queues)
Class representing flow object with attributes from specs.
Definition: Flow.h:45
virtual void initialize()
queueType getType() const
Definition: RMTQueue.cc:241
cGate * getOutputGate() const
Definition: RMTQueue.cc:261
const char * MOD_RMT_PORTWRAPPER
Definition: ExternConsts.cc:87
virtual void postQueueCreation(RMTQueue *queue)
const char * MOD_RMTPORT
Definition: ExternConsts.cc:64
RMTQueue * lookup(RMTPort *port, RMTQueueType type, const char *queueName)
RMTQueues & getInputQueues()
Definition: RMTPort.cc:174
void setRMTAccessGate(cGate *gate)
Definition: RMTQueue.cc:256
RMTPort * getQueueToPortMapping(RMTQueue *queue)
const char * MOD_POL_RMT_QMONITOR
Definition: ExternConsts.cc:74
RMTPort * addPort(Flow *flow)
RMTPort * getPort(const char *name)
void interconnectModules(cModule *m1, cModule *m2, std::string n1, std::string n2)
Definition: Utils.cc:79
virtual void preQueueRemoval(RMTQueue *queue)
RMTQueue * addQueue(RMTQueueType type, RMTPort *port, const char *queueId="0")
bool isManagementFlow() const
Definition: Flow.cc:366
void removePort(RMTPort *port)
RMTQueues & getOutputQueues()
Definition: RMTPort.cc:179
void removeQueue(RMTQueue *queue)
void setType(queueType type)
Definition: RMTQueue.cc:246
void registerInputQueue(RMTQueue *queue)
Definition: RMTPort.cc:184
cGate * getInputGate() const
Definition: RMTQueue.cc:266
const char * MOD_RMT
Definition: ExternConsts.cc:62
std::map< RMTQueue *, RMTPort * > queueToPort
void setFlow(Flow *flow)
Definition: RMTPort.cc:374
Define_Module(RMTModuleAllocator)
void registerOutputQueue(RMTQueue *queue)
Definition: RMTPort.cc:190
RMTQMonitorBase * qMonPolicy
std::vector< RMTQueue * > RMTQueues
Definition: RMTQueue.h:180
cGate * getRMTAccessGate() const
Definition: RMTQueue.cc:251
const char * MOD_RMT_QUEUE
Definition: ExternConsts.cc:88