RINASim  October 2016
Documentation of framework for OMNeT++
LS_Module.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "common/Routing_Alg.h"
4 #include "common/GraphCL.h"
5 
6 namespace common_LSModule {
7 
8 using namespace std;
9 using namespace common_Routing;
10 using namespace common_GraphCL;
11 
12 template<class T>
13 class LS_Module : public Routing_Alg<T> {
14  //Vars and classes
15 public:
16 protected:
17  int secId;
18 
19  struct linksU{
20  int sId;
21  map<string, T> links;
22  linksU() : sId(-1){}
23  linksU(const int &_sId) : sId(_sId){}
24  };
25 
26  class LS_Update : public Routing_Update {
27  public:
29  map<string, linksU> entries;
30  void addEntry(const string & _addr, linksU _neig) { entries[_addr] = _neig; }
31  };
32 
33  map<string, linksU> netState;
34  set<string> changed;
35  set<string> newNeigh;
36 
37  map<string, nhLMetric<T> > currentTable;
38 
39  //Functions
40 public:
41  LS_Module() : secId(-1){}
42  LS_Module(Routing_Module * p, const Address &_nAddr, const string &_addr, T inf) :
43  Routing_Alg<T>(p, _nAddr, _addr, inf), secId(1){}
44 
45  virtual void addSynonym(const string syn) {
47 
48  secId++;
49  changed.insert(Routing_Alg<T>::myAddr);
50  netState[Routing_Alg<T>::myAddr].sId = secId;
51  netState[Routing_Alg<T>::myAddr].links[syn] = T();
52 
54  }
55  virtual void removeSynonym(const string syn) {
57 
58  secId++;
59  changed.insert(Routing_Alg<T>::myAddr);
60  netState[Routing_Alg<T>::myAddr].sId = secId;
61  netState[Routing_Alg<T>::myAddr].links.erase(syn);
62 
64  }
65 
66  virtual void addFlow(const Address &_nAddr, const string &_addr, const T &_metric){
67  Routing_Alg<T>::addFlow(_nAddr, _addr, _metric);
68 
69  changed.insert(Routing_Alg<T>::myAddr);
70  secId++;
71  netState[Routing_Alg<T>::myAddr].sId = secId;
72 
73  if(netState[Routing_Alg<T>::myAddr].links[_addr] == 0) {
74  newNeigh.insert(_addr);
75  }
76  netState[Routing_Alg<T>::myAddr].links[_addr] = _metric;
77 
79  }
80 
81  virtual void removeFlow(const Address &_nAddr, const string &_addr){
82  Routing_Alg<T>::removeFlow(_nAddr, _addr);
83 
84  changed.insert(Routing_Alg<T>::myAddr);
85  secId++;
86  netState[Routing_Alg<T>::myAddr].sId = secId;
87  netState[Routing_Alg<T>::myAddr].links.erase(_addr);
88 
90  }
91 
92  virtual bool processUpdate(Routing_Update * update) {
93  if(LS_Update * up = dynamic_cast<LS_Update*>(update)) {
94  //cout<<"I'm "<< Routing_Alg<T>::myNAddr <<"<"<< Routing_Alg<T>::descriptor << "> receiving update" << endl;
95  for(auto & entry : up->entries){
96  if(entry.first == Routing_Alg<T>::myAddr) { continue; }
97  if(netState[entry.first].sId < entry.second.sId){
98  netState[entry.first] = entry.second;
99  changed.insert(entry.first);
100  // cout << "\t"<<entry.first<< " updated to sId : "<<netState[entry.first].sId<<endl;
101  }
102  }
103  if(!changed.empty()) {
105  // cout << "--Schedule a new update" << endl;
106  return true;
107  }
108  }
109 
110  return false;
111  };
112 
113  virtual void sendUpdate() {
114 
116 
117  map<string, linksU> entries;
118  for(string entry : changed){
119  entries[entry] = netState[entry];
120  }
121 
122  for(auto neig : Routing_Alg<T>::invNeigTable){
123  LS_Update * update = new LS_Update();
124  if(newNeigh.find(neig.second) == newNeigh.end()){
125  update->entries = entries;
126  // cout<<"I'm "<< Routing_Alg<T>::myNAddr << " sending partial to "<< neig.first << endl;
127  } else {
128  update->entries = netState;
129  // cout<<"I'm "<< Routing_Alg<T>::myNAddr << " sending full to "<< neig.first << endl;
130  }
131  Routing_Alg<T>::parent->chSendUpdate(update, neig.first, this);
132  }
133  changed.clear();
134  newNeigh.clear();
135 
136  }
137 
138  virtual map<string, nhLMetric<T> > getChanges() {
139  GraphCL<T> g;
140 
141  for(const auto &entry : netState){
142  for(const auto & link : entry.second.links){
143  g.addLink(entry.first, Link<T>(link.first, link.second));
144  }
145  }
146  map<string, nhLMetric<T> > t = g.getNextHops(Routing_Alg<T>::myAddr, Routing_Alg<T>::infinite);
147  map<string, nhLMetric<T> > ret = t;
148 
149  for(const auto & entry : currentTable){
150  if(entry.second.metric == ret[entry.first].metric || entry.second.nh == ret[entry.first].nh) {
151  ret.erase(entry.first);
152  }
153 
154  }
155 
156  currentTable = t;
157  return ret;
158  };
159 
160  virtual map<string, nhLMetric<T> > getAll() {
161  GraphCL<T> g;
162 
163  for(auto entry : netState){
164  for(auto link : entry.second.links){
165  g.addLink(entry.first, Link<T>(link.first, link.second));
166  }
167  }
169  return currentTable;
170  };
171 
172 
173  virtual void print() {
174  GraphCL<T> g;
175  for(auto entry : netState){
176  for(auto link : entry.second.links){
177  g.addLink(entry.first, Link<T>(link.first, link.second));
178  }
179  }
181  }
182 };
183 
184 } /* namespace common_LSModule */
map< string, nhLMetric< T > > getNextHops(const string &root, const T &infMetric)
Definition: GraphCL.h:50
map< string, linksU > netState
Definition: LS_Module.h:33
virtual bool processUpdate(Routing_Update *update)
Definition: LS_Module.h:92
map< string, linksU > entries
Definition: LS_Module.h:29
LS_Module(Routing_Module *p, const Address &_nAddr, const string &_addr, T inf)
Definition: LS_Module.h:42
virtual map< string, nhLMetric< T > > getAll()
Definition: LS_Module.h:160
virtual void addSynonym(const string syn)
Definition: Routing_Alg.h:40
virtual void addSynonym(const string syn)
Definition: LS_Module.h:45
virtual void removeFlow(const Address &_nAddr, const string &_addr)
Definition: LS_Module.h:81
virtual void removeSynonym(const string syn)
Definition: Routing_Alg.h:41
void print(const string &root, const T &infMetric)
Definition: GraphCL.h:64
virtual void print()
Definition: LS_Module.h:173
void addEntry(const string &_addr, linksU _neig)
Definition: LS_Module.h:30
set< string > changed
Definition: LS_Module.h:34
virtual void sendUpdate()
Definition: LS_Module.h:113
virtual void addFlow(const Address &_nAddr, const string &_addr, const T &_metric)
Definition: LS_Module.h:66
void addLink(const string &n, const Link< T > &l)
Definition: GraphCL.h:46
map< string, nhLMetric< T > > currentTable
Definition: LS_Module.h:37
virtual void removeSynonym(const string syn)
Definition: LS_Module.h:55
virtual void removeFlow(const Address &_nAddr, const string &_addr)
Definition: Routing_Alg.h:49
set< string > newNeigh
Definition: LS_Module.h:35
Address class holds IPC Process identification.
Definition: Address.h:42
virtual map< string, nhLMetric< T > > getChanges()
Definition: LS_Module.h:138
virtual void sendUpdate()
Definition: Routing_Alg.h:68
virtual void addFlow(const Address &_nAddr, const string &_addr, const T &_metric)
Definition: Routing_Alg.h:43