/
etorrent_ctl.erl
196 lines (164 loc) · 5.84 KB
/
etorrent_ctl.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
%% @author Jesper Louis Andersen <jesper.louis.andersen@gmail.com>
%% @doc Control torrents globally
%% <p>This module is used to globally control torrents. You can start
%% a torrent by pointing to a file on disk, and you can stop or a
%% check a torrent.</p>
%% <p>As such, this module is <em>intended</em> to become an API for
%% torrent manipulation in the long run.</p>
%% @end
-module(etorrent_ctl).
-behaviour(gen_server).
-export([start_link/1,
start/1, start/2, stop/1,
check/1, pause/1, continue/1,
local_peer_id/0]).
-export([handle_cast/2, handle_call/3, init/1, terminate/2]).
-export([handle_info/2, code_change/3]).
-define(SERVER, ?MODULE).
-type bcode() :: etorrent_types:bcode().
-type peerid() :: <<_:160>>.
-record(state, {local_peer_id :: binary() }).
%% API
%% =======================================================================
% @doc Start a new etorrent_t_manager process
% @end
-spec start_link(binary()) -> {ok, pid()} | ignore | {error, term()}.
start_link(PeerId) when is_binary(PeerId) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [PeerId], []).
% @doc Ask the manager process to start a new torrent, given in File.
% @end
-spec start(string()) -> ok | {error, term()}.
start(File) ->
start(File, none).
%% @doc Ask the manager to start a new torrent, given in File
%% Upon completion the given CallBack function is executed in a separate
%% process.
%% @end
-spec start(string(), none | fun (() -> any())) -> ok | {error, term()}.
start(File, CallBack) ->
gen_server:call(?SERVER, {start, File, CallBack}, infinity).
% @doc Check a torrents contents
% @end
-spec check(integer()) -> ok.
check(Id) ->
gen_server:cast(?SERVER, {check, Id}).
% @doc Set the torrent on pause
% @end
-spec pause(integer()) -> ok.
pause(Id) ->
gen_server:cast(?SERVER, {pause, Id}).
% @doc Set the torrent on play :)
% @end
-spec continue(integer()) -> ok.
continue(Id) ->
gen_server:cast(?SERVER, {continue, Id}).
% @doc Ask the manager process to stop a torrent, identified by File.
% @end
-spec stop(string()) -> ok.
stop(File) ->
gen_server:cast(?SERVER, {stop, File}).
%% @doc Get a local peer id.
%%
%% Most of the code don't need this function, because the peer id is usually
%% passed as a parameter of the `start_link' function.
%%
%% This function can be used for debugging and inside tests.
-spec local_peer_id() -> peerid().
local_peer_id() ->
gen_server:call(?SERVER, local_peer_id).
%% =======================================================================
%% @private
init([PeerId]) ->
%% We trap exits to gracefully stop all torrents on death.
process_flag(trap_exit, true),
{ok, #state { local_peer_id = PeerId}}.
%% @private
handle_cast({check, Id}, S) ->
Child = etorrent_torrent_ctl:lookup_server(Id),
etorrent_torrent_ctl:check_torrent(Child),
{noreply, S};
handle_cast({pause, Id}, S) ->
Child = etorrent_torrent_ctl:lookup_server(Id),
etorrent_torrent_ctl:pause_torrent(Child),
{noreply, S};
handle_cast({continue, Id}, S) ->
Child = etorrent_torrent_ctl:lookup_server(Id),
etorrent_torrent_ctl:continue_torrent(Child),
{noreply, S};
handle_cast({stop, F}, S) ->
stop_torrent(F),
{noreply, S}.
%% @private
handle_call({start, F, CallBack}, _From, S) ->
lager:info("Starting torrent in file ~s", [F]),
case load_torrent(F) of
duplicate -> {reply, duplicate, S};
{ok, Torrent} ->
TorrentIH = etorrent_metainfo:get_infohash(Torrent),
case etorrent_torrent_pool:start_child(
{Torrent, F, TorrentIH},
S#state.local_peer_id,
etorrent_counters:next(torrent)) of
{ok, TorrentPid} ->
install_callback(TorrentPid, TorrentIH, CallBack),
{reply, ok, S};
{error, {already_started, _Pid}} = Err ->
{reply, Err, S}
end;
{error, Reason} ->
lager:info("Malformed torrent file ~s, error: ~p", [F, Reason]),
etorrent_event:notify({malformed_torrent_file, F}),
{reply, {error, Reason}, S}
end;
handle_call(stop_all, _From, S) ->
stop_all(),
{reply, ok, S};
handle_call(local_peer_id, _From, S) ->
{reply, S#state.local_peer_id, S}.
%% @private
handle_info(Info, State) ->
lager:error("Unknown handle_info event: ~p", [Info]),
{noreply, State}.
%% @private
terminate(_Event, _S) ->
stop_all(),
ok.
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% =======================================================================
stop_torrent(F) ->
lager:info("Stopping torrent in file ~s", [F]),
case etorrent_table:get_torrent({filename, F}) of
not_found -> ok; % Was already removed, it is ok.
{value, PL} ->
TorrentIH = proplists:get_value(info_hash, PL),
etorrent_torrent_pool:terminate_child(TorrentIH),
ok
end.
stop_all() ->
PLS = etorrent_table:all_torrents(),
[begin
F = proplists:get_value(filename, PL),
stop_torrent(F)
end || PL <- PLS].
-spec load_torrent(string()) -> duplicate
| {ok, bcode()}
| {error, _Reason}.
load_torrent(F) ->
case etorrent_table:get_torrent({filename, F}) of
not_found -> load_torrent_internal(F);
{value, PL} ->
case duplicate =:= proplists:get_value(state, PL) of
true -> duplicate;
false -> load_torrent_internal(F)
end
end.
load_torrent_internal(F) ->
Workdir = etorrent_config:work_dir(),
P = filename:join([Workdir, F]),
etorrent_bcoding:parse_file(P).
install_callback(_TorrentPid, _InfoHash, none) ->
ok;
install_callback(TorrentPid, InfoHash, Fun) ->
ok = etorrent_callback_handler:install_callback(TorrentPid, InfoHash, Fun).