-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_handler.erl
69 lines (59 loc) · 2.54 KB
/
message_handler.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
%%%---------------------------------------------------------------------
%%% Created by: Bruce Yinhe
%%% Creation date: 2011-10-18
%%%---------------------------------------------------------------------
%%% Description module message_handler
%%%---------------------------------------------------------------------
%%% This is the supervisor for message_receiver and message_sender
%%%---------------------------------------------------------------------
%%% Exports
%%%---------------------------------------------------------------------
%%% start(Requester_pid, Socket, Peer_id, Peer_mutex_pid, Piece_mutex_pid,
%%% File_storage_pid)
%%% starts the process and returns its processID
%%%---------------------------------------------------------------------
%%% init(Requester_pid, Socket, Peer_id, Peer_mutex_pid, Piece_mutex_pid,
%%% File_s%%% torage_pid)
%%% initialize the process, starts the children and enters the loop
%%%---------------------------------------------------------------------
%%% send(Pid, Type, Msg)
%%% sends a sending request to msg_sender
%%%---------------------------------------------------------------------
%%% loop(Socket, Peer_id, Msg_recver_pid, Msg_sender_pid)
%%% the loop
%%%---------------------------------------------------------------------
-module(message_handler).
-export([start/6, send/3]).
-export([loop/4, init/6]).
start(Requester_pid, Socket, Peer_id,
Peer_mutex_pid, Piece_mutex_pid, File_storage_pid) ->
spawn(?MODULE, init,
[Requester_pid, Socket, Peer_id,
Peer_mutex_pid, Piece_mutex_pid, File_storage_pid]).
send(Pid, Type, Msg) ->
Pid ! {start_sending, Type, Msg}.
init(Requester_pid, Socket, Peer_id, Peer_mutex_pid, Piece_mutex_pid,
File_storage_pid) ->
process_flag(trap_exit, true),
receive
{uploader, Uploader_pid} ->
ok
end,
Msg_recver_pid = message_receiver:start(Requester_pid, Uploader_pid,
Peer_mutex_pid,
Piece_mutex_pid,
File_storage_pid,
Socket, Peer_id),
link(Msg_recver_pid),
Msg_sender_pid = message_sender:start(Socket),
link(Msg_sender_pid),
loop(Socket, Peer_id, Msg_recver_pid, Msg_sender_pid).
loop(Socket, Peer_id, Msg_recver_pid, Msg_sender_pid) ->
receive
{start_sending, Type, Msg} ->
ok = message_sender:send(Msg_sender_pid, Type, Msg),
loop(Socket, Peer_id, Msg_recver_pid, Msg_sender_pid);
{'EXIT', _Pid, _Reason} ->
gen_tcp:close(Socket),
exit(self(), kill)
end.