-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.erl
352 lines (293 loc) · 10.3 KB
/
node.erl
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
-module(node).
-export([start/1]).
-include_lib("datastructures.hrl").
-import(util, [log/2]).
-define(INFINITY, 999999999999).
start(State) ->
yes = register_node(State#state.name, self()),
loop(State).
loop(State) ->
log_state(State),
timer:sleep(100),
receive
wakeup ->
log("~p received wakeup", [State#state.name]),
NewState = wakeup(State),
loop(NewState);
{initiate,Level,FragName,NodeState,Edge} ->
log_receive(initiate, State, Edge),
NewState = handle_initiate(State, Level, FragName, NodeState, Edge),
loop(NewState);
{test,Level,FragName,Edge} ->
log_receive(test, State, Edge),
NewState = handle_test(State, Level, FragName, Edge),
loop(NewState);
{accept,Edge} ->
log_receive(accept, State, Edge),
NewState = handle_accept(State, Edge),
loop(NewState);
{reject,Edge} ->
log_receive(reject, State, Edge),
NewState = handle_reject(State, Edge),
loop(NewState);
{report,Weight,Edge} ->
log_receive(report, State, Edge),
NewState = handle_report(State, Weight, Edge),
loop(NewState);
{changeroot,Edge} ->
log_receive(changeroot, State, Edge),
NewState = handle_changeroot(State),
loop(NewState);
{connect,Level,Edge} ->
log("~p received connect on edge ~p", [State#state.name, Edge]),
NewState = handle_connect(State, Level, Edge),
loop(NewState)
end.
get_target_pid(Edge) ->
global:whereis_name(Edge#edge.node_2).
edge_to_tuple(Edge) ->
{ Edge#edge.weight, Edge#edge.node_1, Edge#edge.node_2 }.
register_node(NodeName, Pid) ->
global:register_name(NodeName, Pid).
wakeup(State) ->
BestEdge = util:get_best_edge_from_basic_edges(State#state.edges),
NewEdgeList = util:replace_edge(
State#state.edges,
BestEdge,
BestEdge#edge { type = branch }),
send_connect(State, 0, BestEdge),
State#state {
edges = NewEdgeList,
fragment_level = 0,
status = found,
find_count = 0
}.
handle_connect(State, Level, NeighbourEdge) ->
NewState = case State#state.status == sleeping of
true -> wakeup(State);
_ -> State
end,
Edge = util:get_edge_by_neighbour_edge(NewState#state.edges, NeighbourEdge),
if
Level < NewState#state.fragment_level ->
Branch = Edge#edge { type = branch },
AktState = State#state { edges = util:replace_edge(State#state.edges, Edge, Branch)},
send_initiate(AktState,
AktState#state.fragment_level,
AktState#state.fragment_name,
AktState#state.status,
Branch),
case AktState#state.status of
find ->
AktState#state { find_count = AktState#state.find_count + 1 };
_ ->
AktState
end;
true ->
case Edge#edge.type of
basic ->
resend(State, {connect, Level, NeighbourEdge}),
NewState;
_ ->
send_initiate(NewState, NewState#state.fragment_level + 1, Edge#edge.weight, find, Edge),
NewState
end
end.
handle_initiate(State, Level, FragName, NodeStatus, NeighbourEdge) ->
Edge = util:get_edge_by_neighbour_edge(State#state.edges, NeighbourEdge),
BranchList = lists:filter(
fun(Elem) -> (not util:are_edges_equal(Elem, Edge)) and (Elem#edge.type == branch) end,
State#state.edges
),
NewFindCount = case NodeStatus of
find -> State#state.find_count + length(BranchList);
_ -> State#state.find_count
end,
lists:foreach(
fun(EdgeElem) -> send_initiate(State, Level, FragName, NodeStatus, EdgeElem) end,
BranchList
),
NewState = State#state {
status = NodeStatus,
fragment_level = Level,
fragment_name = FragName,
in_branch = Edge,
best_edge = undefined,
best_weight = ?INFINITY,
find_count = NewFindCount
},
case NodeStatus of
find -> test(NewState);
_ -> NewState
end.
test(State) ->
BasicEdges = lists:filter(
fun(Edge) -> Edge#edge.type == basic end,
State#state.edges),
case length(BasicEdges) > 0 of
true ->
TestEdge = util:get_best_edge_from_basic_edges(BasicEdges),
send_test(State, State#state.fragment_level, State#state.fragment_name, TestEdge),
State#state { test_edge = TestEdge };
false ->
report(State#state { test_edge = undefined })
end.
handle_test(InState, Level, FragName, NeighbourEdge) ->
State = case InState#state.status of
sleeping -> wakeup(InState);
_ -> InState
end,
if
Level > State#state.fragment_level ->
resend(State, {test, Level, FragName, NeighbourEdge}),
State;
true ->
Edge = util:get_edge_by_neighbour_edge(State#state.edges, NeighbourEdge),
if FragName /= State#state.fragment_name ->
send_accept(State, Edge),
State;
true ->
NewState = case Edge#edge.type of
basic ->
Rejected = Edge#edge { type = rejected },
State#state { edges = util:replace_edge(State#state.edges, Edge, Rejected) };
_ -> State
end,
case not util:are_edges_equal(NewState#state.test_edge, Edge) of
true ->
send_reject(NewState, Edge),
NewState;
false ->
test(NewState)
end
end
end.
handle_accept(State, NeighbourEdge) ->
Edge = util:get_edge_by_neighbour_edge(State#state.edges, NeighbourEdge),
{NewBestWeight, NewBestEdge} = case Edge#edge.weight < State#state.best_weight of
true -> {Edge#edge.weight, Edge};
false -> {State#state.best_weight, State#state.best_edge}
end,
NewState = State#state {
test_edge = undefined ,
best_weight = NewBestWeight,
best_edge = NewBestEdge
},
report(NewState).
handle_reject(State, NeighbourEdge) ->
Edge = util:get_edge_by_neighbour_edge(State#state.edges, NeighbourEdge),
NewState = case Edge#edge.type of
basic ->
NewEdge = Edge#edge{ type = rejected },
State#state { edges = util:replace_edge(State#state.edges, Edge, NewEdge) };
_ -> State
end,
test(NewState).
report(State) ->
case {State#state.find_count, State#state.test_edge} of
{0, undefined} ->
NewState = State#state { status = found },
send_report(State, State#state.best_weight, State#state.in_branch),
NewState;
_ -> State
end.
handle_report(State, Weight, NeighbourEdge) ->
Edge = util:get_edge_by_neighbour_edge(State#state.edges, NeighbourEdge),
case util:are_edges_equal(State#state.in_branch, Edge) of
false ->
{NewBestWeight, NewBestEdge} = case Weight < State#state.best_weight of
true -> {Weight, Edge};
false -> {State#state.best_weight, State#state.best_edge}
end,
NewState = State#state {
find_count = State#state.find_count - 1,
best_weight = NewBestWeight,
best_edge = NewBestEdge
},
report(NewState);
true ->
case State#state.status of
find ->
resend(State, {report, Weight, NeighbourEdge}),
State;
_ ->
case Weight > State#state.best_weight of
true ->
change_root(State);
false -> case {State#state.best_weight, Weight} of
{?INFINITY, ?INFINITY} ->
log("~p ending, MST found. i guess :)", [State#state.name]),
exit(normal);
_ -> State
end
end
end
end.
change_root(State) ->
BestEdge = State#state.best_edge,
case BestEdge#edge.type of
branch ->
send_changeroot(State, BestEdge),
State;
_ ->
send_connect(State, State#state.fragment_level, BestEdge),
NewEdgeList = util:replace_edge(State#state.edges, BestEdge, BestEdge#edge { type = branch }),
State#state { edges = NewEdgeList }
end.
handle_changeroot(State) ->
change_root(State).
% send methods
send_connect(State, Level, Edge) ->
log_send(connect, State, Edge),
get_target_pid(Edge) ! { connect, Level, edge_to_tuple(Edge) }.
send_initiate(State, FragmentLevel, FragmentName, NodeStatus, Edge) ->
log_send(initiate, State, Edge),
get_target_pid(Edge) ! {initiate, FragmentLevel, FragmentName, NodeStatus, edge_to_tuple(Edge)}.
send_test(State, FragmentLevel, FragmentName, Edge) ->
log_send(test, State, Edge),
get_target_pid(Edge) ! { test, FragmentLevel, FragmentName, edge_to_tuple(Edge) }.
send_accept(State, Edge) ->
log_send(accept, State, Edge),
get_target_pid(Edge) ! {accept, edge_to_tuple(Edge)}.
send_reject(State, Edge) ->
log_send(reject, State, Edge),
get_target_pid(Edge) ! {reject, edge_to_tuple(Edge)}.
send_report(State, BestWeight, Edge) ->
log_send(report, State, Edge),
get_target_pid(Edge) ! { report, BestWeight, edge_to_tuple(Edge) }.
send_changeroot(State, Edge) ->
log_send(changeroot, State, Edge),
get_target_pid(Edge) ! { changeroot, edge_to_tuple(Edge) }.
resend(State, Payload) ->
[Message | _] = tuple_to_list(Payload),
log("~p relaying ~p to itself", [State#state.name, Message]),
timer:sleep(300),
self() ! Payload.
log_send(Message, State, Edge) ->
log("~p (~p) sending ~p to ~p",
[State#state.name, State#state.fragment_name, Message, Edge#edge.node_2]).
log_receive(Message, State, Edge) ->
log("~p (~p) received ~p on edge ~p",
[State#state.name, State#state.fragment_name, Message, Edge]).
log_state(State) ->
log(
"~n~p
~p (~p) ~p
Fragment Name ~p
Edges ~p
Best Edge ~p
Best Weight ~p
In Branch ~p
Test Edge ~p~n",
[
calendar:local_time(),
State#state.name,
State#state.fragment_name,
State#state.status,
State#state.fragment_level,
State#state.edges,
State#state.best_edge,
State#state.best_weight,
State#state.in_branch,
State#state.test_edge
]).