Update launch code to correctly handle the case where the schema is already created.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent.erl
blobff552953049d1d358984e2d469c154976ba594cc
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 = application:start(mnesia),
15 db_initialize(),
16 application:start(etorrent).
18 start(_Type, _Args) ->
19 etorrent_sup:start_link().
21 stop() ->
22 ok = application:stop(etorrent),
23 halt().
25 stop(_State) ->
26 ok.
28 db_initialize() ->
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])
33 end,
34 etorrent_mnesia_init:init().
36 %%--------------------------------------------------------------------
37 %% Function: list() -> io()
38 %% Description: List currently active torrents.
39 %%--------------------------------------------------------------------
40 list() ->
41 {atomic, A} = etorrent_torrent:all(),
42 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
43 ["Id:", "total", "left", "uploaded", "downloaded",
44 "I", "C", "Comp."]),
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",
47 [R#torrent.id,
48 R#torrent.total,
49 R#torrent.left,
50 R#torrent.uploaded,
51 R#torrent.downloaded,
52 R#torrent.leechers,
53 R#torrent.seeders,
54 percent_complete(R)])
55 end, A).
57 %%--------------------------------------------------------------------
58 %% Function: show(Item) -> io()
59 %% Description: Show detailed information for Item
60 %%--------------------------------------------------------------------
61 show() ->
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
67 {atomic, [R]} ->
68 io:format("Id: ~3.B Name: ~s~n",
69 [R#tracking_map.id, R#tracking_map.filename]);
70 {atomic, []} ->
71 io:format("No such torrent Id~n")
72 end;
73 show(_) ->
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 %%--------------------------------------------------------------------
80 help() ->
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])
90 end,
91 Commands),
92 ok.
94 %%--------------------------------------------------------------------
95 %% Abbreviations
96 %%--------------------------------------------------------------------
97 h() -> help().
98 l() -> list().
99 s() -> show().
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.