-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoordinator.erl
283 lines (250 loc) · 11.4 KB
/
coordinator.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
-module(coordinator).
-behaviour(gen_server).
%% API
-export([start/1]).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-define (FRAMES_TO_SKIP, 3).
-record(state, {team_number,
station_number,
current_slot,
receiver_pid :: pid(),
sender_pid :: pid(),
datasink_pid :: pid(),
slot_wishes,
used_slots,
needs_new_slot,
frame_timer}).
start([ReceivingPortAtom, TeamNumberAtom, StationNumberAtom, MulticastIPAtom, LocalIPAtom]) ->
SendingPort = 14000 + atom_to_integer(TeamNumberAtom),
gen_server:start(?MODULE,
[atom_to_integer(ReceivingPortAtom),
SendingPort,
atom_to_list(TeamNumberAtom),
atom_to_list(StationNumberAtom),
MulticastIPAtom,
LocalIPAtom],
[]);
start([ReceivingPortAtom, SendingPortAtom, TeamNumberAtom, StationNumberAtom, MulticastIPAtom, LocalIPAtom]) ->
gen_server:start(?MODULE,
[atom_to_integer(ReceivingPortAtom),
atom_to_integer(SendingPortAtom),
atom_to_list(TeamNumberAtom),
atom_to_list(StationNumberAtom),
MulticastIPAtom,
LocalIPAtom],
[]).
init([ReceivingPort, SendingPort, TeamNumber, StationNumber, MulticastIP, LocalIP]) ->
%%% seed process' random number generator
random:seed(now()),
%%% parse IPs into usable format
{ok, ParsedMulticastIP} = inet_parse:address(atom_to_list(MulticastIP)),
{ok, ParsedLocalIP} = inet_parse:address(atom_to_list(LocalIP)),
%%% Open Multicast sockets
{ok, ReceivingSocket} = gen_udp:open(ReceivingPort,
[binary,
{active, true},
{multicast_if, ParsedLocalIP},
inet,
{reuseaddr, true}, % reuse open port on local machine
{multicast_loop, true},
{add_membership, {ParsedMulticastIP,
ParsedLocalIP}}
]),
utility:log("receiving socket ~p~n",[inet:port(ReceivingSocket)]),
utility:log("multicast ip ~p~n",[ParsedMulticastIP]),
utility:log("local ip ~p~n",[ParsedLocalIP]),
{ok, SendingSocket} = gen_udp:open(SendingPort,
[binary,
{active, true},
{multicast_if, ParsedLocalIP},
inet,
{reuseaddr, true}, % reuse open port on local machine
{multicast_loop, true},
{ip, ParsedLocalIP}
]),
utility:log("sending socket ~p~n",[inet:port(SendingSocket)]),
%%% start the receiver and sender processes
{ok, ReceiverPID} = receiver:start(self(), ReceivingSocket, LocalIP, SendingPort),
{ok, SenderPID} = sender:start(self(),
SendingSocket,
ParsedMulticastIP,
ReceivingPort),
gen_udp:controlling_process(ReceivingSocket,ReceiverPID),
gen_udp:controlling_process(SendingSocket,SenderPID),
{ok, DataSinkPID} = datasink:start(TeamNumber, StationNumber),
%%% start timer for first sending round
Timer = create_msg_timer(2000, first_frame),
{ok, #state{team_number = TeamNumber, %
station_number = StationNumber, % HOSTNAME##lab
receiver_pid = ReceiverPID, % PID of the receiver gen_server
sender_pid = SenderPID, % PID of the sender gen_server
datasink_pid = DataSinkPID, % PID of the datasink gen_server
slot_wishes = dict:new(), % [{SlotNumber,[Station1, Station2]}, ...]
used_slots = [], % list of all slots in use. determined by seen packets
needs_new_slot = false, %
frame_timer = Timer
}}.
%%% async incoming messages
handle_cast({received, Slot, TimestampReceived, Packet}, State) ->
utility:log("received something"),
NewSlotwishes = register_slotwishes(Packet, State#state.slot_wishes),
case check_for_packet_collision(Slot, State) of
true ->
utility:log("collision detected"),
% check if the Slot of the packet is the same that we are sending in
case Slot == State#state.current_slot of
true ->
utility:log("collision with own packet"),
% Add slot to Slotwishes again so that it will be sorted out in the
% next round when we are checking for a free slot
NewTimer = case State#state.needs_new_slot of
true ->
State#state.frame_timer;
false ->
%skip up to 2 frames to avoid to be stuck in collisions on same slots repeatedly
FramesToSkip = random:uniform(?FRAMES_TO_SKIP),
restart_msg_timer(1000 * FramesToSkip, new_frame, State#state.frame_timer)
end,
{noreply, State#state{needs_new_slot = true,
slot_wishes = NewSlotwishes,
frame_timer = NewTimer}};
false ->
{noreply, State#state{slot_wishes = NewSlotwishes}}
end;
false -> % no collision at all
{StationIdentifier, StationNumber, NextSlot, Payload, Timestamp} = parse_message(Packet),
gen_server:cast(State#state.datasink_pid, {write_data, StationIdentifier, StationNumber, NextSlot, Payload, Timestamp, TimestampReceived}),
UsedSlots = [Slot | State#state.used_slots],
{noreply, State#state{slot_wishes = NewSlotwishes,
used_slots = UsedSlots}}
end;
handle_cast(needs_new_slot, State) ->
utility:log("needs new slot"),
{noreply, State#state{needs_new_slot = true}};
handle_cast(kill, State) ->
{stop, normal, State};
handle_cast(_UnknownMessage, State) ->
utility:log("unknown msg"),
{noreply, State}.
%%% do everything required for a clean shutdown
terminate(_Reason, State) ->
exit(State#state.datasink_pid, kill),
gen_server:cast(State#state.receiver_pid, kill),
gen_fsm:send_event(State#state.sender_pid, kill),
ok.
%%%%% Helpers
atom_to_integer(Atom) ->
list_to_integer(atom_to_list(Atom)).
create_msg_timer(Time, Msg) ->
utility:log("creating timer"),
erlang:send_after(Time - (utility:current_timestamp() rem 1000),self(),Msg).
restart_msg_timer(Time, Msg, OldTimerRef) ->
erlang:cancel_timer(OldTimerRef),
create_msg_timer(Time, Msg).
calculate_free_slot(Slotwishes) ->
utility:log("calc free slot"),
AvailableSlots = lists:subtract(lists:seq(0,19), dict:fetch_keys(Slotwishes)),
case length(AvailableSlots) == 0 of
true ->
no_free_slot;
false ->
RandomElementIndex = random:uniform(length(AvailableSlots)),
lists:nth(RandomElementIndex, AvailableSlots)
end.
%%% returns true when the Slot collided else false.
-spec check_for_packet_collision(Slot :: integer(), #state{}) -> true | false.
check_for_packet_collision(Slot, State) ->
lists:member(Slot, State#state.used_slots) orelse Slot == State#state.current_slot.
%%% adds the slot a packet was sent in to the list of used slots
-spec register_slotwishes(Packet :: binary(), Slotwishes :: dict()) -> dict().
register_slotwishes(Packet, Slotwishes) ->
{_, StationNumber, NextSlot, _, _} = parse_message(Packet),
dict:store(NextSlot, StationNumber, Slotwishes).
%%% TODO handle malformed packets
parse_message(Packet) ->
<<StationIdentifierBin:8/binary,
StationNumberBin:2/binary,
PayloadBin:14/binary,
Slot:8/integer-big,
Timestamp:64/integer-big
>> = Packet,
StationIdentifier = binary_to_list(StationIdentifierBin),
StationNumber = list_to_integer(binary_to_list(StationNumberBin)),
Payload = binary_to_list(PayloadBin),
{StationIdentifier, StationNumber, Slot, Payload, Timestamp}.
handle_info(first_frame, State) ->
utility:log("lets start a new round"),
case calculate_free_slot(State#state.slot_wishes) of
no_free_slot ->
utility:log("no free slot. skipping this frame"),
NewTimer = create_msg_timer(1000, first_frame),
{noreply, State#state{slot_wishes = dict:new(), used_slots=[], needs_new_slot = false, frame_timer = NewTimer}};
Slot ->
utility:log("found free slot. starting first frame"),
NewTimer = create_msg_timer(1000, new_frame),
gen_fsm:send_event(State#state.sender_pid, {slot, Slot}),
{noreply, State#state{current_slot = Slot, slot_wishes = dict:new(), used_slots=[], needs_new_slot = false, frame_timer = NewTimer}}
end;
handle_info(new_frame, State) ->
%start timer for next sending round
utility:log("lets start a new round"),
NewTimer = create_msg_timer(1000, new_frame),
NewCurrentSlot = case State#state.needs_new_slot of
true ->
utility:log("own packet collided - calculating new slot"),
case calculate_free_slot(State#state.slot_wishes) of
no_free_slot ->
utility:log("send_message: no free slot. skipping this frame!"),
no_free_slot;
FreeSlot ->
gen_fsm:send_event(State#state.sender_pid, {slot, FreeSlot}),
FreeSlot
end;
false ->
gen_fsm:send_event(State#state.sender_pid, {slot, State#state.current_slot}),
State#state.current_slot
end,
NewState = case NewCurrentSlot of
no_free_slot ->
%resetting wishes and used_slots for next frame, keep current slot but get a new one next round
State#state{slot_wishes = dict:new(), used_slots=[], needs_new_slot = true, frame_timer = NewTimer};
_ ->
%resetting wishes and used_slots for next frame and set the next slot
State#state{current_slot = NewCurrentSlot, slot_wishes = dict:new(), used_slots=[], needs_new_slot = false, frame_timer = NewTimer}
end,
{noreply, NewState};
handle_info({revise_next_slot, CurrentNextSlot}, State) ->
utility:log("coordinator: revising current next slot"),
case dict:is_key(CurrentNextSlot, State#state.slot_wishes) of
true -> %wish for slot, choose another
utility:log("coordinator: invalid - calculating new slot"),
NextSlot = case calculate_free_slot(State#state.slot_wishes) of
no_free_slot ->
gen_fsm:send_event(State#state.sender_pid, no_free_slot),
CurrentNextSlot;
Slot ->
gen_fsm:send_event(State#state.sender_pid, {next_slot, Slot}),
Slot
end,
utility:log("station: ~p~n before: ~p~n after: ~p~n wishes: ~p~n",[State#state.station_number,CurrentNextSlot,NextSlot,dict:to_list(State#state.slot_wishes)]),
{noreply, State#state{current_slot = NextSlot}};
false -> %still free, keep it
utility:log("coordinator: valid - just go on"),
gen_fsm:send_event(State#state.sender_pid, {next_slot, CurrentNextSlot}),
{noreply, State}
end;
%%% OTP gen_server boilerplate - ignore this
handle_info(Info, State) ->
utility:log("coordinator: how about sending gen server a msg in a proper way: ~p~n",[Info]),
{noreply, State}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.