RINASim  October 2016
Documentation of framework for OMNeT++
LatGenerator.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 "APN.h"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 
30 namespace LatGenerator {
31 
33 
34 using namespace std;
35 
36 portMetric::portMetric(RMTPort* p, unsigned short m) :
37  port(p), metric(m){}
38 
39 bool portMetric::operator < (const portMetric &other) const {
40  return port < other.port;
41 }
42 
43 // A new flow has been inserted/or removed
44 void LatGenerator::insertedFlow(const Address &addr, const QoSCube &qos, RMTPort * port){
45  std::string dst = addr.getIpcAddress().getName();
46 
47  unsigned short metric = qos.getDelay()/par("redLinkCost").longValue();
48 
49  if(metric < par("minLinkCost").longValue()) {
50  metric = par("minLinkCost").longValue();
51  } else if(metric > par("maxLinkCost").longValue()) {
52  metric = par("maxLinkCost").longValue();
53  }
54 
55  if(qos.getQosId() == qos.MANAGEMENT.getQosId() || qos.getQosId() == VAL_UNDEF_QOSID) {
56  metric = par("maxLinkCost").longValue();
57  }
58 
59  neighbours[dst].insert(portMetric(port, metric));
60  if(neighbours[dst].size() == 1){
61  rt->insertFlow(addr, dst, "", metric);
62  routingUpdated();
63  } else {
64  bool lower = true;
65  for(portMetric mt : neighbours[dst]) {
66  if(metric >= mt.metric && port != mt.port) { lower = false; }
67  }
68  if(lower) {
69  rt->insertFlow(addr, dst, "", metric);
70  routingUpdated();
71  }
72  }
73 }
74 void LatGenerator::removedFlow(const Address &addr, const QoSCube& qos, RMTPort * port){
75  std::string dst = addr.getIpcAddress().getName();
76  neighbours[dst].erase(portMetric(port, 0));
77  if(neighbours[dst].size() <= 0){
78  rt->removeFlow(addr, dst, "");
79  neighbours.erase(dst);
80  routingUpdated();
81  } else {
82  unsigned short min = par("maxLinkCost").longValue();
83  for(portMetric mt : neighbours[dst]) {
84  if(min >= mt.metric) { min = mt.metric; }
85  }
86  rt->insertFlow(addr, dst, "", min);
87  routingUpdated();
88  }
89 }
90 
91 //Routing has processes a routing update
92 void LatGenerator::routingUpdated(){
93  entries2Next changes = rt->getChanges();
94 
95  for(entries2NextIt it = changes.begin(); it!= changes.end(); it++){
96  qosPaddr dst = it->first;
97  std::string nextHop = "";
98  if(!it->second.nh.empty()){
99  nextHop = *(it->second.nh.begin());
100  }
101  RMTPort * p = NULL;
102  if(nextHop != "") {
103  NTableIt n = neighbours.find(nextHop);
104  if(n != neighbours.end()){
105  if(!n->second.empty()) {
106  p = n->second.begin()->port;
107  }
108  }
109  }
110  if(p == NULL) {
111  fwd->remove(dst.second);
112  } else {
113  fwd->insert(dst.second, p);
114  }
115  }
116 }
117 
118 // Called after initialize
119 void LatGenerator::onPolicyInit(){
120  //Set Forwarding policy
121  fwd = getRINAModule<MiniTable::MiniTable *>(this, 2, {MOD_RELAYANDMUX, MOD_POL_RMT_PDUFWD});
122  rt = getRINAModule<IntSimpleRouting *>(this, 2, {MOD_POL_ROUTING});
123 }
124 
125 }
static const QoSCube MANAGEMENT
Definition: QoSCube.h:201
NTable::iterator NTableIt
Definition: LatGenerator.h:49
const char * MOD_POL_ROUTING
Definition: ExternConsts.cc:77
const APN & getIpcAddress() const
Getter of IPC Process address which should be unambiguous within DIF.
Definition: Address.cc:83
bool operator<(const portMetric &other) const
Definition: LatGenerator.cc:39
portMetric(RMTPort *p, unsigned short m)
Definition: LatGenerator.cc:36
const std::string VAL_UNDEF_QOSID
Definition: QoSCube.cc:36
std::map< qosPaddr, std::string > entries2Next
const char * MOD_RELAYANDMUX
Definition: ExternConsts.cc:57
Class representing QoSCube with all its properties that is primarily used by FA, RMT and RA Specifica...
Definition: QoSCube.h:57
std::string getQosId() const
Gets QoSCube identifier.
Definition: QoSCube.cc:364
entries2Next::iterator entries2NextIt
std::pair< std::string, std::string > qosPaddr
int getDelay() const
Gets Delay parameter.
Definition: QoSCube.cc:284
const std::string & getName() const
Gets APN string name representation.
Definition: APN.cc:40
Register_Class(LatGenerator)
const char * MOD_POL_RMT_PDUFWD
Definition: ExternConsts.cc:73
Address class holds IPC Process identification.
Definition: Address.h:42