forked from sacerdot/Emerging2223Project
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwellknown.erl
36 lines (33 loc) · 1.12 KB
/
wellknown.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
-module(wellknown).
-export([main/1, create_friends_list/2]).
main(Pids) ->
receive
% register new pid
{register_pid, {F, S}} ->
monitor(process, F),
main([{F, S}|Pids]);
% replace old pid with new pid
{replace_pid, OldPid, NewPid} ->
NewPids = lists:delete(OldPid, Pids),
main([NewPid|NewPids]);
% Send list of cars to another car
{getFriends, F, S, Ref} ->
% NewFriends -> car list permutation.
NewFriends = create_friends_list(Pids, {F, S}),
F ! {myFriends, NewFriends, Ref},
main(Pids);
{'DOWN', Ref, process, Pid, _Reason} ->
demonitor(Ref, [flush]),
Dead_Car = lists:keyfind(Pid, 1, Pids),
main(lists:delete(Dead_Car, Pids));
_ ->
main(Pids)
end.
% List permutation
permutation(X) ->
[ Y || {_,Y} <- lists:sort([ {rand:uniform(), N} || N <- X]) ].
% FS: {F, S} -> { Friendship, State }
% List: [{F, S}*]
create_friends_list(List, FS) ->
X = lists:delete(FS, permutation(List)),
lists:sublist(X, 5).