RINASim  October 2016
Documentation of framework for OMNeT++
IMultiQoSTable.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 
24 #include "InfectionSignals.h"
25 
26 
28 
29 namespace IMultiQoSTable {
30 
31 using namespace std;
32 
33 #include <sstream>
34 
35 
36 FlowIdent::FlowIdent(const string & _qosId, const string & _srcAddr, const string & _dstAddr, const int & _srcCepId, const int & _dstCepId) :
37  qosId(_qosId), srcAddr(_srcAddr), dstAddr(_dstAddr), srcCepId(_srcCepId), dstCepId(_dstCepId){}
38 
39 bool FlowIdent::operator<( const FlowIdent & o ) const {
40  if(srcCepId < o.srcCepId) { return true; }
41  if(srcCepId > o.srcCepId) { return false; }
42 
43  if(dstCepId < o.dstCepId) { return true; }
44  if(dstCepId > o.dstCepId) { return false; }
45 
46  if(qosId < o.qosId) { return true; }
47  if(qosId > o.qosId) { return false; }
48 
49  if(dstAddr < o.dstAddr) { return true; }
50  if(dstAddr > o.dstAddr) { return false; }
51 
52  return srcAddr < o.srcAddr;
53 }
54 
55 CacheData::CacheData(RMTPort * _next, const simtime_t & _expiration) :
56  next(_next), expiration(_expiration) {}
58  next(nullptr), expiration(0.0) {}
59 
60 // Lookup function, return a list of RMTPorts to forward a PDU/Address+qos.
61 vector<RMTPort * > IMultiQoSTable::lookup(const PDU * pdu){
62 
63  string dstAddr = pdu->getDstAddr().getIpcAddress().getName();
64  string srcAddr = pdu->getSrcAddr().getIpcAddress().getName();
65 
66  const ConnectionId & cId = pdu->getConnId();
67  FlowIdent fId = FlowIdent(cId.getQoSId(), srcAddr, dstAddr, cId.getSrcCepId(), cId.getDstCepId());
68 
69  CacheData & cd = cache[fId];
70 
71  simtime_t now = simTime();
72  simtime_t ex = now + exTime;
73 
74  if(cd.next == nullptr || cd.expiration < now) {
75  cd.next = search(dstAddr, fId.qosId);
76  }
77 
78  vector<RMTPort * > ret;
79  if(cd.next != nullptr) {
80  ret.push_back(cd.next);
81  cd.expiration = ex;
82 
83  if(const InfectedDataTransferPDU * cipdu = dynamic_cast<const InfectedDataTransferPDU*>(pdu)) {
84  InfectedDataTransferPDU * ipdu = const_cast<InfectedDataTransferPDU *>(cipdu);
85  ipdu->pathDelay += portDelay[cd.next];
86  }
87  } else {
88  cache.erase(fId);
89  }
90 
91  return ret;
92 }
93 vector<RMTPort * > IMultiQoSTable::lookup(const Address &dst, const std::string& qos){
94 
95  string dstAddr = dst.getIpcAddress().getName();
96 
97  vector<RMTPort* > ret;
98 
99  RMTPort * next = search(dstAddr, qos);
100 
101  if(next != nullptr) {
102  ret.push_back(next);
103  }
104 
105  return ret;
106 }
107 
108 // Returns a representation of the Forwarding Knowledge
109 string IMultiQoSTable::toString(){
110  std::ostringstream os;
111 
112  os << this->getName()<<endl;
113  for(const auto &qosTable : table) {
114  os << "\tQoS :" << qosTable.first << endl;
115  for(const auto & entry : qosTable.second) {
116  os << "\t\tQoS :" << entry.first << " -> ";
117  for(RMTPort * p : entry.second){
118  os << p->getParentModule()->getName() << " ";
119 
120  }
121  os << endl;
122  }
123  }
124 
125 
126  return os.str();
127 }
128 
129 
130 RMTPort * IMultiQoSTable::search(const string & dst, const string & qos) {
131 
132  if(qos != QoSCube::MANAGEMENT.getQosId()) {
133  vector<RMTPort*> & vR = table[qos][dst];
134  int pS = vR.size();
135  if(pS <= 0) { return nullptr; }
136  if(pS == 1) { return vR.front(); }
137 
138  int k = intuniform(0, pS-1);
139  return vR[k];
140  } else {
141  return search(dst, MA2QoS);
142  }
143 }
144 
145 //Insert/Remove an entry
146 void IMultiQoSTable::addReplace(const std::string &addr, const std::string &qosId, std::vector<RMTPort * > ports) {
147  vector<RMTPort*> old;
148 
149  for(RMTPort * p : table[qosId][addr]) {
150  bool found = false;
151  for(RMTPort * p2 : ports) {
152  if(p == p2) { found = true; break; }
153  }
154  if(!found) { old.push_back(p); }
155  }
156 
157  if(ports.empty()){
158  table[qosId].erase(addr);
159  } else {
160  table[qosId][addr] = ports;
161  }
162 
163  for(RMTPort * p : old) {
164  for(auto it = cache.begin(); it != cache.end();) {
165  auto itB = it++;
166  if(it->second.next == p && it->first.dstAddr == addr && it->first.qosId == qosId) {
167  cache.erase(itB);
168  }
169  }
170  }
171 }
172 
173 void IMultiQoSTable::setPortDelay(RMTPort* port, double delay) {
174  portDelay[port] = delay;
175 }
176 
177 // Called after initialize
178 void IMultiQoSTable::onPolicyInit(){
179  MA2QoS = par("MA2QoS").stdstringValue();
180  if(MA2QoS == "") { error("Management to QoS must be set."); }
181  exTime = par("exTime").doubleValue();
182 }
183 
184 void IMultiQoSTable::finish(){
185  if(par("printAtEnd").boolValue() || par("printCacheAtEnd").boolValue()){
186  EV << "-----------------" << endl;
187  EV << this->getFullPath() << endl;
188  if(par("printAtEnd").boolValue()) {
189  EV << "Forwarding table::" << endl;
190  EV << toString() <<endl;
191  }
192  if(par("printCacheAtEnd").boolValue()) {
193  EV << "\tCache : "<<endl;
194  for(const auto &ent : cache) {
195  EV << "\t\t :";
196  EV << "("<< ent.first.srcAddr << "."<< ent.first.srcCepId << ")";
197  EV << " -> ";
198  EV << "("<< ent.first.dstAddr << "."<< ent.first.dstCepId << ")";
199  EV << " ["<< ent.first.qosId << "]";
200  EV << " >> "<<endl;
201  EV << "\t\t\t" << ent.second.next->getParentModule()->getName();
202  EV << "\t\t\tExpires " << ent.second.expiration;
203  EV << endl;
204  }
205  }
206 
207  EV << "-----------------" << endl;
208  }
209 }
210 
211 }
static const QoSCube MANAGEMENT
Definition: QoSCube.h:201
bool operator<(const FlowIdent &n) const
std::string getQoSId() const
Getter of selected QoS-cube identifier.
Definition: ConnectionId.cc:44
const APN & getIpcAddress() const
Getter of IPC Process address which should be unambiguous within DIF.
Definition: Address.cc:83
Connection identifier as defined in specifications.
Definition: ConnectionId.h:42
int getSrcCepId() const
Getter of source Connection-Endpoint identifier.
Definition: ConnectionId.cc:54
virtual ConnectionId & getConnId()
Definition: PDU_m.cc:336
Definition: PDU.h:42
virtual Address & getDstAddr()
Definition: PDU_m.cc:306
int getDstCepId() const
Getter of destination Connection-Endpoint identifier.
Definition: ConnectionId.cc:34
virtual Address & getSrcAddr()
Definition: PDU_m.cc:296
Register_Class(IMultiQoSTable::IMultiQoSTable)
FlowIdent(const string &_qosId, const string &_srcAddr, const string &_dstAddr, const int &_srcCepId, const int &_dstCepId)
const std::string & getName() const
Gets APN string name representation.
Definition: APN.cc:40
Address class holds IPC Process identification.
Definition: Address.h:42