-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.erl
88 lines (74 loc) · 2.38 KB
/
temp.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
-module(pingserver).
-export([
rpc_handler/2,
start/1,
ping_server/1,
ping/1,
pong/0
]).
ping(Node) ->
{N, Status} = rpc:call(Node, pingserver, pong, []),
if
Status =:= up ->
{Status, N};
Status =:= nodedown ->
{down, Node}
end.
pong() ->
{node(), up}.
ping_server(List) ->
Result = [ ping(Node) || Node <- List ],
[[ {Status, Node} || {Status, Node} <- Result ,
Status =:= up ],
[ {Status, Node} || {Status, Node} <- Result ,
Status =:= down ]
].
start(List) ->
Pid = register(ping_server_pid, spawn(mapreduce, ping_server, [List])),
io:format("~nstart: ping_server_pid registered to: ~p ~n",[Pid]).
server(List) ->
io:format("ping_server: Task_tracker status-list: ~p~n",[List]),
receive
{up, T_tracker, _, _} ->
io:format("~nping_server: received message {up, ~p}",[T_tracker]),
ping_server( update(List, {up,T_tracker}) );
{down, T_tracker, _} ->
io:format(" ping_server: received message {down, ~p}~n",[T_tracker]),
ping_server( update(List, {down,T_tracker}) );
{die } ->
io:format(" Ping server exiting... ~n")
after 10000 ->
lists:map( fun(X) ->
spawn(mapreduce, rpc_ping, [X]) end,
List ),
ping_server(List)
end.
rpc1_ping(T_tracker) ->
io:format("rpc_ping: Pinging T_tracker -> ~p~n",[T_tracker]),
{_,Timestamp1} = erlang:localtime(),
Result = rpc:call(T_tracker, mapreduce, rpc_pong, [node(), self()]),
io:format("rpc_ping: Result from calling rpc_pong : ~p~n",[Result]),
case Result of
{ping, _} ->
receive
{ping, T_tracker} ->
{_, Timestamp2} = erlang:localtime(),
io:format("Received pong from ~p at ~p, ping sent at ~p~n",
[T_tracker, Timestamp2, Timestamp1]),
ping_server_pid ! {up, T_tracker, Timestamp2, Timestamp1}
after 10000 ->
ping_server_pid ! {down, T_tracker, Timestamp1}
end;
{badrpc, Reason} ->
io:format("rpc_ping: T_tracker at ~p is down due to ~p~n",
[T_tracker, Reason]),
ping_server_pid ! {down, T_tracker, Timestamp1};
true ->
ping_server_pid ! {nodedown, T_tracker}
end.
rpc1_pong(Origin, Server_pid) ->
io:format("~nReceived ping from ~p ",[Origin]),
rpc:call(Origin, mapreduce, rpc_handler,
[Server_pid, {ping, node()}]).
rpc_handler(Pid, Request) ->
Pid ! Request .