-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwarding-information-base.cpp
124 lines (107 loc) · 2.87 KB
/
forwarding-information-base.cpp
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
/**
* Copyright (c) 2018 Amar Abane (a_abane@hotmail.fr). All rights reserved.
*
* This file is part of NDN802.15.4.
*
* NDN802.15.4 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NDN802.15.4 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "forwarding-interest-base.h"
int8_t ForwardingInformationBase::add(Name* prefix, float heardCost)
{
if ( fib_size_ == MAX_FIB_ENTRIES )
return -1;
int8_t entryIndex = 0;
if ( find(prefix, &entryIndex) )
return entryIndex;
FibEntry* e = new FibEntry(getNextFibEntryId(), prefix, heardCost);
entries_[fib_size_] = e;
fib_size_++;
return (fib_size_ - 1);
}
int8_t ForwardingInformationBase::updateCost(NdnPacket* data)
{
int8_t entryIndex = -1;
if ( match(data, &entryIndex) )
{
entries_[entryIndex]->updateCost(data->getCost());
return entryIndex;
}
if ( fib_size_ == MAX_FIB_ENTRIES )
return -1;
Name* prefix = new Name(data->getPrefix(), data->getPrefixLength());
FibEntry* e = new FibEntry(getNextFibEntryId(), prefix, data->getCost());
entries_[fib_size_] = e;
fib_size_++;
return (fib_size_ - 1);
}
float ForwardingInformationBase::findEntry(NdnPacket* interest, uint8_t* id)
{
for ( int8_t i = 0; i < fib_size_; i++ )
if ( entries_[i]->getPrefix()->match(interest) )
{
*id = entries_[i]->getId();
return entries_[i]->getCost();
}
if ( fib_size_ == MAX_FIB_ENTRIES )
return -1;
return 0;
}
bool ForwardingInformationBase::match(NdnPacket* data, int8_t* index)
{
for ( int8_t i = 0; i < fib_size_; i++ )
if ( entries_[i]->getPrefix()->match(data) )
{
*index = i;
return true;
}
return false;
}
bool ForwardingInformationBase::find(Name* prefix, int8_t* index)
{
for ( int8_t i = 0; i < fib_size_; i++ )
if ( entries_[i]->getPrefix()->equals(prefix) )
{
*index = i;
return true;
}
return false;
}
float ForwardingInformationBase::computeNa()
{
if (Id == 0)
return TH;
float Na = (float) Du / (float) Id;
if (Na > 1)
Na = 1;
return Na;
}
FibEntry* ForwardingInformationBase::operator[](uint8_t i)
{
return entries_[i];
}
/* 0 never used. Start from 1 */
uint8_t ForwardingInformationBase::getNextFibEntryId()
{
nextFibEntryId_++;
if (nextFibEntryId_ >= 255)
nextFibEntryId_ = 1;
return nextFibEntryId_;
}
void ForwardingInformationBase::resetEntry(uint8_t entryId)
{
for ( int8_t i = 0; i < fib_size_; i++ )
if ( entries_[i]->getId() == entryId )
{
entries_[i]->reset();
return;
}
}