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
),
14 ok
= application:start(mnesia
),
16 application:start(etorrent
).
18 start(_Type
, _Args
) ->
19 etorrent_sup:start_link().
22 ok
= application:stop(etorrent
),
29 %% May already exist, we do not care at the moment.
30 case mnesia:create_schema([]) of
31 ok
-> error_logger:info_report([schema
, created
]);
32 E
-> error_logger:info_report([schema
, E
])
34 etorrent_mnesia_init:init().
36 %%--------------------------------------------------------------------
37 %% Function: list() -> io()
38 %% Description: List currently active torrents.
39 %%--------------------------------------------------------------------
41 {atomic
, A
} = etorrent_torrent:all(),
42 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
43 ["Id:", "total", "left", "uploaded", "downloaded",
45 lists:foreach(fun (R
) ->
46 io:format("~3.B ~11.B ~11.B ~11.B ~11.B ~3.B ~3.B ~7.3f% ~n",
57 %%--------------------------------------------------------------------
58 %% Function: show(Item) -> io()
59 %% Description: Show detailed information for Item
60 %%--------------------------------------------------------------------
62 io:format("You must supply a torrent Id number~n").
64 show(Item
) when is_integer(Item
) ->
65 %{atomic, Torrent} = etorrent_torrent:select(Item),
66 case etorrent_tracking_map:select(Item
) of
68 io:format("Id: ~3.B Name: ~s~n",
69 [R#tracking_map
.id
, R#tracking_map
.filename
]);
71 io:format("No such torrent Id~n")
74 io:format("Item supplied is not an integer~n").
76 %%--------------------------------------------------------------------
77 %% Function: help() -> io()
78 %% Description: Provide a simple help message for the commands supported.
79 %%--------------------------------------------------------------------
81 io:format("Available commands:~n", []),
83 Commands
= [{"help, h", "This help"},
84 {"list, l", "List torrents in system"},
85 {"show, s", "Show detailed information for a given torrent"},
86 {"stop", "Stop the system"}],
88 lists:foreach(fun({Command
, Desc
}) ->
89 io:format("~-12.s - ~s~n", [Command
, Desc
])
94 %%--------------------------------------------------------------------
96 %%--------------------------------------------------------------------
100 s(Item
) -> show(Item
).
102 %% --------------------------------------------------------------------
103 %% Internal functions
104 %% --------------------------------------------------------------------
105 percent_complete(R
) ->
106 %% left / complete * 100 = % done
107 (R#torrent
.total
- R#torrent
.left
) / R#torrent
.total
* 100.