-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicRoutingEntry.h
103 lines (95 loc) · 2.38 KB
/
DynamicRoutingEntry.h
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
#ifndef DYNAMIC_ROUTING_ENTRY_H
#define DYNAMIC_ROUTING_ENTRY_H
#include "Config.h"
#include "Common.h"
#include "DynamicRoutingTriple.h"
class DynamicRoutingEntry
{
public:
char* prefix;
Digest digest;
int height;
DynamicRoutingTriple* triples;
DynamicRoutingEntry* parent;
DynamicRoutingEntry* lchild;
DynamicRoutingEntry* rchild;
void reset()
{
prefix = NULL;
digest = 0;
height = 1;
triples = NULL;
parent = lchild = rchild = NULL;
}
DynamicRoutingEntry()
{
reset();
}
void setPrefix(char* prefix)
{
Common* common = Common::getCommon();
common->copystring(this->prefix, prefix);
this->digest = common->computeDigest(prefix);
}
void addInfo(int face, int distance)
{
DynamicRoutingTriple* triple = triples;
while(NULL != triple)
{
if(triple->face == face) break;
triple = triple->next;
}
if(NULL == triple)
{
triple = new DynamicRoutingTriple();
triple->face = face;
triple->distance = (double)distance;
triple->numOfPackets = 1;
triple->next = triples;
if(NULL != triples) triples->previous = triple;
triples = triple;
}
else
{
triple->distance = (triple->distance*triple->numOfPackets + distance)/(triple->numOfPackets + 1);
++triple->numOfPackets;
}
}
void removeInfo(int face, int distance)
{
DynamicRoutingTriple* triple = triples;
while(NULL != triple)
{
if(triple->face == face) break;
triple = triple->next;
}
if(NULL != triple)
{
triple->distance = (triple->distance*triple->numOfPackets - distance)/(triple->numOfPackets - 1);
--triple->numOfPackets;
if(triple->distance <= 0 || triple->numOfPackets <= 0)
{
if(NULL != triple->next) triple->next->previous = triple->previous;
if(NULL != triple->previous) triple->previous->next = triple->next;
if(triple == triples) triples = triple->next;
free(triple);
}
}
}
void swap(DynamicRoutingEntry* entry)
{
char* tempPrefix = this->prefix;
this->prefix = entry->prefix;
entry->prefix = tempPrefix;
Digest tempDigest = this->digest;
this->digest = entry->digest;
entry->digest = tempDigest;
int tempHeight = this->height;
this->height = entry->height;
entry->height = tempHeight;
DynamicRoutingTriple* tempTriple = this->triples;
this->triples = entry->triples;
entry->triples = tempTriple;
}
};
#endif