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