forked from CSS-D/Net-Task2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkStateTable.cpp
More file actions
226 lines (220 loc) · 6.21 KB
/
Copy pathLinkStateTable.cpp
File metadata and controls
226 lines (220 loc) · 6.21 KB
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
#include "LinkStateTable.h"
bool LinkStateTable::update(Message message)
{
bool is_changed = false;
bool is_find = false;
if (message.cost == MAX_COST)
{
if (is_in_table(message.source_ip_addr, message.dest_ip_addr))
{
erase(message.source_ip_addr, message.dest_ip_addr);
return true;
}
return false;
}
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
if ((*it)[0].source_ip_addr == message.source_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == message.dest_ip_addr)
{
is_find = true;
if (j->cost != message.cost)
{
j->cost = message.cost;
is_changed = true;
break;
}
}
}
}
else if ((*it)[0].source_ip_addr == message.dest_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == message.source_ip_addr)
{
is_find = true;
if (j->cost != message.cost)
{
j->cost = message.cost;
is_changed = true;
break;
}
}
}
}
}
if (!is_find)
{
is_changed = true;
push(message.source_ip_addr, message.dest_ip_addr, message.cost);
}
if (is_changed)
{
print();
}
return is_changed;
}
void LinkStateTable::push(long source_ip_addr, long dest_ip_addr, int cost)
{
bool is_find_source = false;
bool is_find_dest = false;
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
if ((*it)[0].source_ip_addr == source_ip_addr)
{
is_find_source = true;
it->push_back(LinkState(source_ip_addr, dest_ip_addr, cost));
}
if ((*it)[0].source_ip_addr == dest_ip_addr)
{
is_find_dest = true;
it->push_back(LinkState(dest_ip_addr, source_ip_addr, cost));
}
}
if (!is_find_source)
{
vector<LinkState> source_table;
source_table.push_back(LinkState(source_ip_addr, dest_ip_addr, cost));
link_state_table_.push_back(source_table);
}
if (!is_find_dest)
{
vector<LinkState> dest_table;
dest_table.push_back(LinkState(dest_ip_addr, source_ip_addr, cost));
link_state_table_.push_back(dest_table);
}
}
void LinkStateTable::erase(long source_ip_addr, long dest_ip_addr)
{
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
if ((*it)[0].source_ip_addr == source_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == dest_ip_addr)
{
it->erase(j);
break;
}
}
}
else if ((*it)[0].source_ip_addr == dest_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == source_ip_addr)
{
it->erase(j);
break;
}
}
}
}
}
bool LinkStateTable::is_in_table(long source_ip_addr, long dest_ip_addr)
{
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
if ((*it)[0].source_ip_addr == source_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == dest_ip_addr)
{
return true;
}
}
}
else if ((*it)[0].source_ip_addr == dest_ip_addr)
{
for (auto j = it->begin(); j != it->end(); j++)
{
if (j->dest_ip_addr == source_ip_addr)
{
return true;
}
}
}
}
return false;
}
vector<LinkStateTable::LinkState> &LinkStateTable::operator[](int index)
{
if (index >= 0 && index < link_state_table_.size())
return link_state_table_[index];
cerr << "Link State Table : out of range" << endl;
return *(vector<LinkState> *)NULL;
}
void LinkStateTable::print()
{
cout << "Link State Table:" << endl;
cout << std::left << setw(LEN3) << "Source Ip Addr"
<< std::left << setw(LEN3) << "Dest Ip Addr"
<< std::left << setw(LEN3) << "Cost" << endl;
for (int i = 0; i < 3 * LEN3; i++)
{
cout << "-";
}
cout << endl;
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
for (auto j = it->begin(); j != it->end(); j++)
{
cout << std::left << setw(LEN3) << inet_ntoa(*((in_addr *)&j->source_ip_addr))
<< std::left << setw(LEN3) << inet_ntoa(*((in_addr *)&j->dest_ip_addr))
<< std::left << setw(LEN3) << j->cost << endl;
}
}
}
int LinkStateTable::get_index(long ip_addr)
{
for (int i = 0; i < link_state_table_.size(); i++)
{
if (!link_state_table_.empty() && link_state_table_[i][0].source_ip_addr == ip_addr)
{
return i;
}
}
return -1;
}
void LinkStateTable::delete_empty()
{
for (auto it = link_state_table_.begin(); it != link_state_table_.end(); it++)
{
if (it->empty())
{
link_state_table_.erase(it);
continue;
}
}
}