A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
qkd-location-register.cc
Go to the documentation of this file.
1/*
2 * Copyright(c) 2020 DOTFEESA www.tk.etf.unsa.ba
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 *
7 *
8 * Author: Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
9 * Miralem Mehic <miralem.mehic@ieee.org>
10 */
11#include "ns3/log.h"
12#include "ns3/address.h"
13#include "ns3/node.h"
14#include "ns3/nstime.h"
15#include "ns3/simulator.h"
16#include "ns3/uinteger.h"
17#include "ns3/trace-source-accessor.h"
18#include <iostream>
19#include <fstream>
20#include <string>
21
24
25namespace ns3 {
26
27 NS_LOG_COMPONENT_DEFINE("QKDLocationRegister");
28
29 NS_OBJECT_ENSURE_REGISTERED(QKDLocationRegister);
30
31 TypeId
33 {
34 static TypeId tid = TypeId("ns3::QKDLocationRegister")
36 .SetGroupName("QKDLocationRegister")
37 .AddConstructor<QKDLocationRegister>()
38 ;
39 return tid;
40 }
41
42 TypeId
44 {
45 return GetTypeId();
46 }
47
50
52 {
53 SetNode(n);
54
56 for(uint32_t i = 0; i < m_node->GetNApplications(); i++ ){
58 kms = app->GetObject<QKDKeyManagerSystemApplication>();
59 if(kms) break;
60 }
62 SetAddress(kms->GetAddress());
63
64 }
65
66 bool
68 {
69 NS_LOG_FUNCTION(this << m_id << rt.GetDestinationKmNodeId() << rt.GetNextHopKMNodeId());
70
71 if(m_id == rt.GetDestinationKmNodeId() && m_id == rt.GetNextHopKMNodeId())
72 return false;
73
74 std::pair<std::map<uint32_t, QKDLocationRegisterEntry>::iterator, bool> result = m_locationEntites.insert(
75 std::make_pair(rt.GetDestinationKmNodeId(),rt)
76 );
77 return result.second;
78 }
79
80 bool
82 {
83 NS_LOG_FUNCTION(this);
84 return (m_locationEntites.erase(dstSaeId) != 0);
85 }
86
87 bool
89 {
90 NS_LOG_FUNCTION(this << m_id << id << m_locationEntites.size());
91
92 if(m_locationEntites.empty())
93 return false;
94
95 for(auto i = m_locationEntites.begin(); i != m_locationEntites.end(); ++i)
96 NS_LOG_FUNCTION(this << "PRINT:" << m_id << i->first << id);
97
98 auto i = m_locationEntites.find(id);
99
100 if(i == m_locationEntites.end())
101 return false;
102
103 rt = i->second;
104 return true;
105 }
106
107 bool
109 {
111 for(auto i = m_locationEntites.begin(); i != m_locationEntites.end(); ++i)
112 {
113 if( i->second.GetDestinationKmsAddress() == dstKmsAddress ){
114 rt = i->second;
115 return true;
116 }
117 }
118 return false;
119
120 }
121
122 void
123 QKDLocationRegister::GetListOfAllEntries(std::map<uint32_t, QKDLocationRegisterEntry> & allRoutes)
124 {
125 for(auto i = m_locationEntites.begin(); i != m_locationEntites.end(); ++i)
126 {
127 allRoutes.insert(std::make_pair(i->first,i->second));
128 }
129 }
130
131 void
133 std::map<uint32_t, QKDLocationRegisterEntry> & unreachable)
134 {
135 unreachable.clear();
136 for(auto i = m_locationEntites.begin(); i
137 != m_locationEntites.end(); ++i)
138 {
139 if(i->second.GetNextHop() == nextHop)
140 {
141 unreachable.insert(std::make_pair(i->first,i->second));
142 }
143 }
144 }
145
148 {
149 return m_locationEntites.size();
150 }
151
152
153 // Given an Adjacency List, find all shortest paths from "start" to all other vertices.
154 std::vector< std::pair<uint32_t, uint32_t> >
156 {
157
158 NS_LOG_FUNCTION(this << m_id << "DijkstraSp on " << start << "m_adjList.size: " << m_adjList.size() );
159 std::vector<std::pair<uint32_t, uint32_t> > dist; // First int is dist, second is the previous node.
160
161 for(auto el : m_adjList){
162 NS_LOG_FUNCTION("**m_adjList:**" << el.first);
163 }
164
165 // Initialize all source->vertex as infinite.
166
167 uint32_t distSize = start > m_adjList.size() ? start+1 : m_adjList.size();
168 dist.resize(distSize);
169 dist[start] = std::make_pair(100, start);
170
171 for(auto const& el : m_adjList)
172 {
173 if(el.first > dist.size()){
174 dist.resize(el.first+1);
175 NS_LOG_FUNCTION("Dist size is too small! \t" << el.first << "\t" << dist.size());
176 }
177
178 if(el.first == m_id)
179 {
180 dist[m_id] = std::make_pair(0, m_id);
181 }else{
182 dist[el.first] =
183 std::make_pair(
184 uint32_t(1000000),
185 uint32_t(el.first)
186 ); // Define "infinity" as necessary by constraints.
187 }
188 //NS_LOG_FUNCTION(this << m_id << "save " << el.first << dist[el.first].first << dist[el.first].second );
189 }
190
191
192 for(auto i = dist.begin(); i != dist.end(); ++i)
193 NS_LOG_FUNCTION(this << "xxxx" << i->first << i->second);
194
195 NS_LOG_FUNCTION(this << "dist.size(): " << dist.size());
196 if(dist.size() > 1 )
197 {
198
199 NS_LOG_FUNCTION(this << 616);
200
201 // Create a PQ.
202 std::priority_queue<
203 std::pair<uint32_t, uint32_t>,
204 std::vector< std::pair<uint32_t, uint32_t> >,
205 std::greater<std::pair<uint32_t, uint32_t> >
206 > pq;
207
208 NS_LOG_FUNCTION(this << 625 << start);
209
210 // Add source to pq, where distance is 0.
212 std::pair<uint32_t, uint32_t> pZero = std::make_pair(start, startZero);
213
214 pq.push(pZero);
215
216 NS_LOG_FUNCTION(this << 630);
217
218 // While pq isn't empty...
219 while(!pq.empty())
220 {
221 NS_LOG_FUNCTION(this << m_id << "pq.size():" << pq.size());
222
223 // Get min distance vertex from pq.(Call it u.)
224 uint32_t u = pq.top().first;
225 pq.pop();
226 NS_LOG_FUNCTION(this << m_id << "u:" << u);
227
228 // Visit all of u's friends. For each one(called v)....
229 for(auto const& el : m_adjList)
230 {
231 NS_LOG_FUNCTION(this << m_id << "PPPPP\t u:" << u << "\t el.first: " << el.first << "m_adjList.size():" << m_adjList.size() );
232
233 uint32_t v = el.first;
234 uint32_t weight = el.second;
235
236 if(u == v || u == m_id || v == m_id || dist[u].second == dist[v].second)continue;
237
238 NS_LOG_FUNCTION(this << "u" << u);
239 NS_LOG_FUNCTION(this << "v" << v);
240 NS_LOG_FUNCTION(this << "weight" << weight);
241
242 NS_LOG_FUNCTION(this << "dist[u].first" << dist[u].first);
243 NS_LOG_FUNCTION(this << "dist[u].second" << dist[u].second);
244
245 NS_LOG_FUNCTION(this << "dist[v].first" << dist[v].first);
246 NS_LOG_FUNCTION(this << "dist[v].second" << dist[v].second);
247
248 // If the distance to v is shorter by going through u...
249 if(dist[v].first > dist[u].first + weight)
250 {
251 NS_LOG_FUNCTION(this << "536!!!!");
252 // Update the distance of v.
253 dist[v].first = dist[u].first + weight;
254 // Update the previous node of v.
255 dist[v].second = u;
256 // Insert v into the pq.
257 pq.emplace(v, dist[v].first);
258 }
259 NS_LOG_FUNCTION(this << "544!!!!");
260 }
261 }
262 }
263
264 return dist;
265 }
266
267 void
269 {
270 NS_LOG_FUNCTION(this << m_id);
271
272 for(auto const& el: m_controllers)
273 NS_LOG_FUNCTION("**controllers:**" << el.first);
274
275 for(auto const& el : m_controllers)
276 {
277 el.second->ClearRoutingTable();
278 uint32_t sourceNode = el.first;
279
280 std::vector<std::pair<uint32_t, uint32_t> > dist; // First int is dist, second is the previous node.
282
283 for(auto i = dist.begin(); i != dist.end(); ++i)
284 NS_LOG_FUNCTION(this << "oooo" << i->first << i->second);
285
286
287 NS_LOG_FUNCTION(this << sourceNode << dist.size() << el.first);
288
289 uint32_t counter = 0;
290 std::vector<uint32_t> path;
291 for(auto el2 : dist)
292 {
293 NS_LOG_FUNCTION(this << "el2.first:" << el2.first << "\t el2.second:" << el2.second);
294 path = {};
295
296 if(el2.first == 0 && el2.second == 0) {
297 }else{
298
299 /*
300 path is empty if the node is the node!
301 the first element in path is destination node!
302 the last element in path is the next hop!
303 el.first is the source node!
304 dist[i].first is number of hops
305 i is source node as well
306 */
307 uint32_t hops = el.first;
308 NS_LOG_FUNCTION(this << "nextHop:" << el2.second << "\thops:" << hops);
309
310 if(hops >= 1)
311 {
312 NS_LOG_FUNCTION(this << "716");
313 uint32_t tempVal(el2.second);
314
315 NS_LOG_FUNCTION(this << "720" << tempVal);
316 path.push_back(tempVal);
317
318 //Populate routing table
319 //uint32_t nextHop = ReverseColumn(path[path.size()-1]);
320 NS_LOG_FUNCTION(this << "path.size():" << path.size());
321
322 if(!path.empty()){
323
324 uint32_t nextHop = path[path.size()-1];
325 NS_LOG_FUNCTION(this << "nextHopIs:" << nextHop);
326
327 for(auto const& el : m_controllers){
328 NS_LOG_FUNCTION("****" << el.first);
329 }
330
331 if(m_controllers.find(nextHop) != m_controllers.end() )
332 {
333 Ipv4Address nextHopAddress = m_controllers.find(nextHop)->second->GetLocalKMAddress();
334 //uint32_t dstNodeId = ReverseColumn(uint32_t(i));
335 uint32_t dstNodeId = counter;
336 Ipv4Address dstNodeAddress = m_controllers.find(dstNodeId)->second->GetLocalKMAddress();
337 std::string dstKmId = m_controllers.find(dstNodeId)->second->GetLocalKMId();
339 nextHop,//nextHop KM node ID
340 nextHopAddress, //nextHop KM address
341 hops, // Dirrect p2p connection(number of hops)
342 dstNodeId, //Destination KM node ID
343 dstNodeAddress, //Destination KM address
344 dstKmId
345 );
347 //el.second->AddRouteEntry(newEntry);
348 NS_LOG_FUNCTION(this << "Node " << m_id << " FINAL_ROUTE\t" << "nextHop: " << nextHop << "(" << nextHopAddress << ")" << "\tdstNodeId: " << dstNodeId << "(" << dstNodeAddress << ")" << dstKmId);
349 NS_LOG_FUNCTION(this << m_id << newEntry.GetDestinationKmNodeId() << newEntry.GetNextHopKMNodeId());
350 }
351 }
352 }
353 }
354
355 counter++;
356
357 }
358
359
360
361 }
362 }
363
364} // namespace ns3
Ipv4 addresses are stored in host order in this class.
uint32_t GetNApplications() const
Definition node.cc:173
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Definition node.cc:164
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
QKDNetSim implements Key Management System(KMS) as an application that listens on TCP port 80.
Introspection did not find any typical Config paths.
QKDLocationRegister is a class used to keep details about distant QKD nodes and their connectivity.
void GetListOfDestinationWithNextHop(uint32_t nxtHp, std::map< uint32_t, QKDLocationRegisterEntry > &dstList)
Lookup list of addresses for which nxtHp is the next Hop address.
bool Lookup(uint32_t dstSaeId, QKDLocationRegisterEntry &rt)
Lookup location table entry with destination address dst.
std::map< uint32_t, QKDLocationRegisterEntry > m_locationEntites
std::map< uint32_t, Ptr< QKDControl > > m_controllers
void SetAddress(Ipv4Address addr)
bool AddEntry(QKDLocationRegisterEntry &r)
Add location table entry if it doesn't yet exist in location table.
TypeId GetInstanceTypeId() const override
Get the type ID for the instance.
bool DeleteEntry(uint32_t dst)
Delete location table entry with destination address dst, if it exists.
std::vector< std::pair< uint32_t, uint32_t > > DijkstraSP(uint32_t start)
std::vector< std::pair< uint32_t, uint32_t > > m_adjList
bool LookupByKms(Ipv4Address dstKmsId, QKDLocationRegisterEntry &rt)
Lookup location table entry with destination address dst KMS Id @Ipv4Address destination KMS Ipv4Addr...
static TypeId GetTypeId()
Get the type ID.
void GetListOfAllEntries(std::map< uint32_t, QKDLocationRegisterEntry > &allRoutes)
Lookup list of all addresses in the location table.
uint32_t GetSize()
Provides the number of routes present in that nodes location table.
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Definition first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.