Handle HAVE_ALL and HAVE_NONE. Cleanup the BITFIELD message.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent_t_sup.erl
blob9c571483382ec3df8880c9b0ce4b9a4a2e4aafed
1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_t_sup.erl
3 %%% Author : Jesper Louis Andersen <jlouis@succubus.local.domain>
4 %%% License : See COPYING
5 %%% Description : Supervision of torrent modules.
6 %%%
7 %%% Created : 13 Jul 2007 by
8 %%% Jesper Louis Andersen <jlouis@succubus.local.domain>
9 %%%-------------------------------------------------------------------
10 -module(etorrent_t_sup).
12 -behaviour(supervisor).
14 %% API
15 -export([start_link/3, add_tracker/5, get_pid/2,
16 add_peer/5]).
18 %% Supervisor callbacks
19 -export([init/1]).
21 %%====================================================================
22 %% API functions
23 %%====================================================================
24 start_link(File, Local_PeerId, Id) ->
25 supervisor:start_link(?MODULE, [File, Local_PeerId, Id]).
27 %%--------------------------------------------------------------------
28 %% Func: get_pid/2
29 %% Args: Pid ::= pid() - Pid of the supervisor
30 %% Name ::= atom() - the atom the pid is identified by
31 %% Description: Return the Pid of the peer group process.
32 %%--------------------------------------------------------------------
33 get_pid(Pid, Name) ->
34 {value, {_, Child, _, _}} =
35 lists:keysearch(Name, 1, supervisor:which_children(Pid)),
36 Child.
38 %%--------------------------------------------------------------------
39 %% Func: add_filesystem/3
40 %% Description: Add a filesystem process to the torrent.
41 %%--------------------------------------------------------------------
43 %%--------------------------------------------------------------------
44 %% Func: add_file_system_pool/1
45 %% Description: Add a filesystem process to the torrent.
46 %%--------------------------------------------------------------------
47 add_tracker(Pid, URL, InfoHash, Local_Peer_Id, TorrentId) ->
48 Tracker = {tracker_communication,
49 {etorrent_tracker_communication, start_link,
50 [self(), URL, InfoHash, Local_Peer_Id, TorrentId]},
51 permanent, 15000, worker, [etorrent_tracker_communication]},
52 supervisor:start_child(Pid, Tracker).
54 add_peer(Pid, PeerId, InfoHash, TorrentId, {IP, Port}) ->
55 FSPid = get_pid(Pid, fs),
56 GroupPid = get_pid(Pid, peer_pool_sup),
57 etorrent_t_peer_pool_sup:add_peer(GroupPid,
58 PeerId,
59 InfoHash,
60 FSPid,
61 TorrentId,
62 {IP, Port}).
64 %%====================================================================
65 %% Supervisor callbacks
66 %%====================================================================
67 %%--------------------------------------------------------------------
68 %% Func: init(Args) -> {ok, {SupFlags, [ChildSpec]}} |
69 %% ignore |
70 %% {error, Reason}
71 %% Description: Whenever a supervisor is started using
72 %% supervisor:start_link/[2,3], this function is called by the new process
73 %% to find out about restart strategy, maximum restart frequency and child
74 %% specifications.
75 %%--------------------------------------------------------------------
76 init([Path, PeerId, Id]) ->
77 FSPool = {fs_pool,
78 {etorrent_fs_pool_sup, start_link, []},
79 transient, infinity, supervisor, [etorrent_fs_pool_sup]},
80 FS = {fs,
81 {etorrent_fs, start_link, [Id, self()]},
82 permanent, 2000, worker, [etorrent_fs]},
83 Control = {control,
84 {etorrent_t_control, start_link, [Id, Path, PeerId]},
85 permanent, 20000, worker, [etorrent_t_control]},
86 PeerPool = {peer_pool_sup,
87 {etorrent_t_peer_pool_sup, start_link, []},
88 transient, infinity, supervisor, [etorrent_t_peer_pool_sup]},
89 {ok, {{one_for_all, 1, 60}, [FSPool, FS, Control, PeerPool]}}.
91 %%====================================================================
92 %% Internal functions
93 %%====================================================================