2 -behaviour(application
).
4 -include("etorrent_mnesia_table.hrl").
6 -export([db_initialize
/0, stop
/0, start
/0]).
7 -export([start
/2, stop
/1]).
8 -export([help
/0, h
/0, list/0, l
/0, show
/0, s
/0, show
/1, s
/1]).
11 ok
= application:start(crypto
),
12 ok
= application:start(inets
),
13 ok
= application:start(sasl
),
16 application:start(etorrent
).
19 start(_Type
, _Args
) ->
20 etorrent_sup:start_link().
23 ok
= application:stop(etorrent
),
30 ok
= mnesia:create_schema([node()]),
31 etorrent_mnesia_init:init().
33 %%--------------------------------------------------------------------
34 %% Function: list() -> io()
35 %% Description: List currently active torrents.
36 %%--------------------------------------------------------------------
38 {atomic
, A
} = etorrent_torrent:all(),
39 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
40 ["Id:", "total", "left", "uploaded", "downloaded",
42 lists:foreach(fun (R
) ->
43 io:format("~3.B ~11.B ~11.B ~11.B ~11.B ~3.B ~3.B ~7.3f% ~n",
54 %%--------------------------------------------------------------------
55 %% Function: show(Item) -> io()
56 %% Description: Show detailed information for Item
57 %%--------------------------------------------------------------------
59 io:format("You must supply a torrent Id number~n").
61 show(Item
) when is_integer(Item
) ->
62 %{atomic, Torrent} = etorrent_torrent:select(Item),
63 case etorrent_tracking_map:select(Item
) of
65 io:format("Id: ~3.B Name: ~s~n",
66 [R#tracking_map
.id
, R#tracking_map
.filename
]);
68 io:format("No such torrent Id~n")
71 io:format("Item supplied is not an integer~n").
73 %%--------------------------------------------------------------------
74 %% Function: help() -> io()
75 %% Description: Provide a simple help message for the commands supported.
76 %%--------------------------------------------------------------------
78 io:format("Available commands:~n", []),
80 Commands
= [{"help, h", "This help"},
81 {"list, l", "List torrents in system"},
82 {"show, s", "Show detailed information for a given torrent"},
83 {"stop", "Stop the system"}],
85 lists:foreach(fun({Command
, Desc
}) ->
86 io:format("~-12.s - ~s~n", [Command
, Desc
])
91 %%--------------------------------------------------------------------
93 %%--------------------------------------------------------------------
97 s(Item
) -> show(Item
).
99 %% --------------------------------------------------------------------
100 %% Internal functions
101 %% --------------------------------------------------------------------
102 percent_complete(R
) ->
103 %% left / complete * 100 = % done
104 (R#torrent
.total
- R#torrent
.left
) / R#torrent
.total
* 100.