-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunchrouter.cc
371 lines (306 loc) · 9.28 KB
/
launchrouter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <click/config.h>
#include <elements/local/launch.hh>
#include <elements/local/launchrouter.hh>
#include <elements/local/launchrequester.hh>
#include <elements/local/launchlockrequester.hh>
#include <click/args.hh>
#include <clicknet/ether.h>
#include <click/error.hh>
#include <click/glue.hh>
#include <click/confparse.hh>
#include <unistd.h>
#include <math.h>
#define pi 3.14159265358979323846
CLICK_DECLS
LaunchRouter::LaunchRouter()
: _respone_waiting_timer(static_use_responses, this), _lock_waiting_timer(static_use_lock, this), _routing_table_entry_timer(static_make_routetable_expire, this)
{
_routingtable_available = false;
_channel_lock_positive = false;
_ready_for_another_packet = true;
can_use_11= true;
can_use_6 = true;
can_use_1 = true;
IPAddress n1 ("10.0.0.1");
LocationEntry n1_l(n1, 1, 1);
_ltable.insert(n1, n1_l);
IPAddress n2 ("10.0.0.2");
LocationEntry n2_l(n1, 2, 2);
_ltable.insert(n2, n2_l);
IPAddress n3 ("10.0.0.3");
LocationEntry n3_l(n1, 3, 3);
_ltable.insert(n3, n3_l);
IPAddress n4 ("10.0.0.4");
LocationEntry n4_l(n1, 4, 4);
_ltable.insert(n4, n4_l);
}
LaunchRouter::~LaunchRouter()
{
}
int
LaunchRouter::configure(Vector<String> &conf, ErrorHandler * errh)
{
if (Args(conf, this, errh)
.read_mp("ETH", _ether_address_eth)
.read_mp("PU", _pu_behavior)
.read_mp("RES_T", _repsonse_waiting_ms)
.read_mp("LOCK_T", _lock_waiting_ms)
.read_mp("RT_T", _routing_table_entry_ms)
.read_p("REQUESTER", reinterpret_cast<Element *&>(_requester))
.read_p("LOCK_REQUESTER", reinterpret_cast<Element *&>(_lock_requester))
.complete() < 0)
return -1;
memcpy(_eth ,_ether_address_eth.data(),sizeof(_eth));
return 0;
}
int
LaunchRouter::initialize(ErrorHandler *)
{
_respone_waiting_timer.initialize(this);
_lock_waiting_timer.initialize(this);
_routing_table_entry_timer.initialize(this);
}
Packet *
LaunchRouter::simple_action(Packet *p_in)
{
_holded_packet = p_in->uniqueify();
//if router has been initialized with a lock and a routing table
//the the packet is directly processed
if(_ready_for_another_packet)
{
//get dst ip from packet's annotations for later use
_dst_ip = _holded_packet->dst_ip_anno();
//Forward packet if routingtable is available and we have lock
//call launchlocrequester if routingtable is available and we don't have lock
//call launchctrlrequester to send REQ
if(_routingtable_available && _channel_lock_positive)
{
RouteEntry * current_best_neighbor = choose_bestneighbor(_dst_ip,_rtes);
IPAddress current_neighbor_ip = current_best_neighbor->neighbor_ip;
if(current_neighbor_ip == locked_neighbor_ip)
{
_holded_packet->set_dst_ip_anno(locked_neighbor_ip /*from calculating the metric*/);
return _holded_packet;
}
else
{
_lock_requester->send_lock_request(current_best_neighbor->channel/*channel selected*/, current_best_neighbor->neighbor_ip/*lock distantion ip*/, current_best_neighbor->neighbor_eth/*lock distantion eth*/,_eth);
_lock_waiting_timer.schedule_after_msec(_lock_waiting_ms);
packets_holded.push_back(_holded_packet);
}
}
//if only the routing table became available then send for
//a LOCK REQ again
else if(_routingtable_available)
{
RouteEntry * current_best_neighbor = choose_bestneighbor(_dst_ip,_rtes);
IPAddress current_neighbor_ip = current_best_neighbor->neighbor_ip;
_lock_requester->send_lock_request(current_best_neighbor->channel/*channel selected*/, current_best_neighbor->neighbor_ip/*lock distantion ip*/, current_best_neighbor->neighbor_eth/*lock distantion eth*/,_eth/*my ethernet*/);
_lock_waiting_timer.schedule_after_msec(_lock_waiting_ms);
packets_holded.push_back(_holded_packet);
}
//if neither the lock nor the routing table are available
//then send CTRL REQ to all neighbours
else
{
_requester->send_request();
_respone_waiting_timer.schedule_after_msec(_repsonse_waiting_ms);
_ready_for_another_packet = false;
packets_holded.push_back(_holded_packet);
}
}
else
{
packets_holded.push_back(_holded_packet);
}
return 0;
}
// Called by the _respone_waiting_timer when it times out
// to chech if the routingtable was loaded or not
void
LaunchRouter::use_responses()
{
if(_routingtable_available)
{
//lookup table and calculate the metric to choose next hop
//issue lock request
RouteEntry * best_neighbor = choose_bestneighbor(_dst_ip,_rtes);
locked_neighbor_ip = best_neighbor->neighbor_ip;
_lock_requester->send_lock_request(best_neighbor->channel/*channel selected*/, best_neighbor->neighbor_ip/*lock distantion ip*/, best_neighbor->neighbor_eth/*lock distantion eth*/,_eth);
_lock_waiting_timer.schedule_after_msec(_lock_waiting_ms);
}
else
{
_requester->send_request();
_respone_waiting_timer.schedule_after_msec(_repsonse_waiting_ms);
}
}
// Called by the _lock_waiting_timer when it times out to check if lock was obtained
void
LaunchRouter::use_lock()
{
//check whether lock response is positive or negative
//if positive push packet
//if negative wait again
if(_channel_lock_positive && _routingtable_available)
{
//annotate packet with distenation and output packet
//sleep(2);
while (packets_holded.size() > 0)
{
Packet * value = packets_holded.front();
RouteEntry * current_best_neighbor = choose_bestneighbor(value->dst_ip_anno(),_rtes);
IPAddress current_neighbor_ip = current_best_neighbor->neighbor_ip;
if(current_neighbor_ip == locked_neighbor_ip)
{
value->set_dst_ip_anno(locked_neighbor_ip /*from calculating the metric*/);
output(0).push(value);
packets_holded.pop_front();
}
else
{
_dst_ip = value->dst_ip_anno();
_lock_requester->send_lock_request(current_best_neighbor->channel/*channel selected*/, current_best_neighbor->neighbor_ip/*lock distantion ip*/, current_best_neighbor->neighbor_eth/*lock distantion eth*/,_eth);
_lock_waiting_timer.schedule_after_msec(_lock_waiting_ms);
return;
}
}
_ready_for_another_packet = true;
}
else
{
_respone_waiting_timer.schedule_after_msec(_repsonse_waiting_ms);
}
}
void
LaunchRouter::make_routetable_expire()
{
//for (RTIter iter = _rtes.begin(); iter.live(); iter++) {
// const RouteEntry &rte = iter.value();
// _rtes.remove(rte.neighbor_ip);
//}
//_routingtable_available = false;
}
//Should be called by LaunchCtrlResponseHandler
void
LaunchRouter::insert_route(const IPAddress &nip,
uint32_t nlat, uint32_t nlong,
uint8_t * ne, uint8_t chl,
uint32_t pub, uint32_t swt)
{
_routingtable_available = true;
_routing_table_entry_timer.reschedule_after_msec(_routing_table_entry_ms);
RouteEntry * temp = _rtes.findp(nip);
if(temp != NULL)
{
temp->neighbor_lat = nlat;
temp->neighbor_long = nlong;
temp->channel = chl;
temp->pu_behavior = pub;
temp->switching_time = swt;
}
else
{
RouteEntry r(nip, nlat, nlong, ne, chl, pub, swt);
_rtes.insert(nip, r);
}
}
void
LaunchRouter::update_route(const IPAddress &nip, uint8_t chl)
{
RouteEntry* temp = _rtes.findp(nip);
temp->channel = chl;
}
/*
// Function to pick the best neighbor in terms of the launch metric
RouteEntry
LaunchRouter::choose_bestneighbor(IPAddress _current_dst_addr)
{
if(_rtes.findp(_current_dst_addr) != 0)
return _rtes.findp(_current_dst_addr);
double last_metric = 10000;
uint8_t best_ip ;
double current_metric;
for (RTIter iter = _rtes.begin(); iter.live(); iter++) {
if(_current_dst_addr == iter.neighbor_ip)
{
best_ip = rte.neighbor_ip;
break;
}
RouteEntry rte = iter.value();
LocationEntry lentry = _ltable.find(rte.neighbor_ip);
current_metric = calculate_metric(rte, lentry);
if(current_metric < last_metric)
{
last_metric = current_metric;
best_ip = rte.neighbor_ip;
}
}
return _rtes.findp(best_ip);
}
*/
//Function to calculate the metric for certain neighbor in the table
double
LaunchRouter::calculate_metric(RouteEntry r, LocationEntry l)
{
uint8_t distance_value = distance(((double)l.neighbor_lat/1000.00),((double)l.neighbor_long/1000.00),((double)r.neighbor_lat/1000.00), ((double)r.neighbor_long/1000.00),'M');
return ((distance_value/(3*(10^6))) + r.switching_time)/(1- r.pu_behavior*_pu_behavior);
}
//Function to calculate the distance between two nodes
double
LaunchRouter::distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta, dist;
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
switch(unit) {
case 'M':
break;
case 'K':
dist = dist * 1.609344;
break;
case 'N':
dist = dist * 0.8684;
break;
}
return (dist);
}
double
LaunchRouter::deg2rad(double deg) {
return (deg * pi / 180);
}
double
LaunchRouter::rad2deg(double rad) {
return (rad * 180 / pi);
}
void
LaunchRouter::set_channel_loc_positive(){
_channel_lock_positive = true;
}
void
LaunchRouter::set_channel_loc_negative()
{
_channel_lock_positive = false;
}
void
LaunchRouter::cant_use_channel(int channel_number)
{
switch(channel_number)
{
case 1:
can_use_1 =false;
break;
case 6:
can_use_6 =false;
break;
case 11:
can_use_11 =false;
break;
}
if(channel_used == channel_number)
set_channel_loc_negative();
}
CLICK_ENDDECLS
EXPORT_ELEMENT(LaunchRouter)