Be strict, all applications must start when we call etorrent:start/0. While here...
[etorrent.git] / lib / etorrent-1.0 / src / etorrent.erl
bloba12ff2b1bac3c995fea509e1c5c1450de6711799
1 -module(etorrent).
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]).
10 start() ->
11 ok = application:start(crypto),
12 ok = application:start(inets),
13 ok = application:start(sasl),
14 ok = mnesia:start(),
15 db_initialize(),
16 application:start(etorrent).
19 start(_Type, _Args) ->
20 etorrent_sup:start_link().
22 stop() ->
23 ok = application:stop(etorrent),
24 halt().
26 stop(_State) ->
27 ok.
29 db_initialize() ->
30 ok = mnesia:create_schema([node()]),
31 etorrent_mnesia_init:init().
33 %%--------------------------------------------------------------------
34 %% Function: list() -> io()
35 %% Description: List currently active torrents.
36 %%--------------------------------------------------------------------
37 list() ->
38 {atomic, A} = etorrent_torrent:all(),
39 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
40 ["Id:", "total", "left", "uploaded", "downloaded",
41 "I", "C", "Comp."]),
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",
44 [R#torrent.id,
45 R#torrent.total,
46 R#torrent.left,
47 R#torrent.uploaded,
48 R#torrent.downloaded,
49 R#torrent.leechers,
50 R#torrent.seeders,
51 percent_complete(R)])
52 end, A).
54 %%--------------------------------------------------------------------
55 %% Function: show(Item) -> io()
56 %% Description: Show detailed information for Item
57 %%--------------------------------------------------------------------
58 show() ->
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
64 {atomic, [R]} ->
65 io:format("Id: ~3.B Name: ~s~n",
66 [R#tracking_map.id, R#tracking_map.filename]);
67 {atomic, []} ->
68 io:format("No such torrent Id~n")
69 end;
70 show(_) ->
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 %%--------------------------------------------------------------------
77 help() ->
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])
87 end,
88 Commands),
89 ok.
91 %%--------------------------------------------------------------------
92 %% Abbreviations
93 %%--------------------------------------------------------------------
94 h() -> help().
95 l() -> list().
96 s() -> show().
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.