Handle HAVE_ALL and HAVE_NONE. Cleanup the BITFIELD message.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent.erl
blobb28dbb034ae12d6b05da54027cff4b14905e0d29
1 -module(etorrent).
2 -behaviour(application).
4 -include("etorrent_version.hrl").
5 -include("etorrent_mnesia_table.hrl").
7 -export([stop/0, start/0, db_create_schema/0]).
8 -export([start/2, stop/1, prep_stop/1]).
9 -export([help/0, h/0, list/0, l/0, show/0, s/0, show/1, s/1, check/1]).
11 -define(RANDOM_MAX_SIZE, 999999999999).
13 start() ->
14 %% Start dependent applications
15 Fun = fun(Application) ->
16 case application:start(Application) of
17 ok -> ok;
18 {error, {already_started, Application}} -> ok
19 end
20 end,
21 lists:foreach(Fun, [crypto, inets, mnesia, sasl]),
22 %% DB
23 etorrent_mnesia_init:wait(),
24 %% Etorrent
25 application:start(etorrent).
27 start(_Type, _Args) ->
28 PeerId = generate_peer_id(),
29 {ok, Pid} = etorrent_sup:start_link(PeerId),
30 {ok, Pid}.
32 stop() ->
33 ok = application:stop(etorrent).
35 prep_stop(_S) ->
36 io:format("Shutting down etorrent~n"),
37 ok = etorrent_fast_resume:stop(),
38 ok.
40 stop(_State) ->
41 ok.
43 db_create_schema() ->
44 ok = mnesia:create_schema([node()]),
45 ok = application:start(mnesia),
46 etorrent_mnesia_init:init(),
47 mnesia:info(),
48 halt().
50 %%--------------------------------------------------------------------
51 %% Function: list() -> io()
52 %% Description: List currently active torrents.
53 %%--------------------------------------------------------------------
54 list() ->
55 {atomic, A} = etorrent_torrent:all(),
56 {DownloadRate, UploadRate} = etorrent_rate_mgr:global_rate(),
57 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
58 ["Id:", "total", "left", "uploaded", "downloaded",
59 "I", "C", "Comp."]),
61 lists:foreach(
62 fun (R) ->
63 {atomic, [#tracking_map { filename = FN, _=_}]} =
64 etorrent_tracking_map:select(R#torrent.id),
65 io:format("~3.B ~11.B ~11.B ~11.B ~11.B ~3.B ~3.B ~7.3f% ~n",
66 [R#torrent.id,
67 R#torrent.total,
68 R#torrent.left,
69 R#torrent.uploaded,
70 R#torrent.downloaded,
71 R#torrent.leechers,
72 R#torrent.seeders,
73 percent_complete(R)]),
74 io:format(" ~s~n", [FN])
75 end, A),
76 %io:format("Rate Up/Down: ~e / ~e~n", [UploadRate, DownloadRate]).
77 io:format("Rate Up/Down: ~8.2f / ~8.2f~n", [UploadRate / 1024.0,
78 DownloadRate / 1024.0]).
80 %%--------------------------------------------------------------------
81 %% Function: show(Item) -> io()
82 %% Description: Show detailed information for Item
83 %%--------------------------------------------------------------------
84 show() ->
85 io:format("You must supply a torrent Id number~n").
87 show(Item) when is_integer(Item) ->
88 %{atomic, Torrent} = etorrent_torrent:select(Item),
89 case etorrent_tracking_map:select(Item) of
90 {atomic, [R]} ->
91 io:format("Id: ~3.B Name: ~s~n",
92 [R#tracking_map.id, R#tracking_map.filename]);
93 {atomic, []} ->
94 io:format("No such torrent Id~n")
95 end;
96 show(_) ->
97 io:format("Item supplied is not an integer~n").
99 %%--------------------------------------------------------------------
100 %% Function: check(Item) -> io()
101 %% Description: Check a torrents contents. For debugging.
102 %%--------------------------------------------------------------------
103 check(Id) ->
104 etorrent_mgr:check(Id).
106 %%--------------------------------------------------------------------
107 %% Function: help() -> io()
108 %% Description: Provide a simple help message for the commands supported.
109 %%--------------------------------------------------------------------
110 help() ->
111 io:format("Available commands:~n", []),
113 Commands = [{"help, h", "This help"},
114 {"list, l", "List torrents in system"},
115 {"show, s", "Show detailed information for a given torrent"},
116 {"stop", "Stop the system"}],
118 lists:foreach(fun({Command, Desc}) ->
119 io:format("~-12.s - ~s~n", [Command, Desc])
120 end,
121 Commands),
124 %%--------------------------------------------------------------------
125 %% Abbreviations
126 %%--------------------------------------------------------------------
127 h() -> help().
128 l() -> list().
129 s() -> show().
130 s(Item) -> show(Item).
132 %% --------------------------------------------------------------------
133 %% Internal functions
134 %% --------------------------------------------------------------------
135 percent_complete(R) ->
136 %% left / complete * 100 = % done
137 (R#torrent.total - R#torrent.left) / R#torrent.total * 100.
139 generate_peer_id() ->
140 Number = crypto:rand_uniform(0, ?RANDOM_MAX_SIZE),
141 Rand = io_lib:fwrite("~B----------", [Number]),
142 lists:flatten(io_lib:format("-ET~s-~12s", [?VERSION, Rand])).