Allow dependent applications to already be started.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent.erl
blobafb35ea6388d678c54e9ddf9fbedf8aeae3269b5
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").
38 stop(_State) ->
39 ok.
41 db_create_schema() ->
42 ok = mnesia:create_schema([node()]),
43 ok = application:start(mnesia),
44 etorrent_mnesia_init:init(),
45 mnesia:info(),
46 halt().
48 %%--------------------------------------------------------------------
49 %% Function: list() -> io()
50 %% Description: List currently active torrents.
51 %%--------------------------------------------------------------------
52 list() ->
53 {atomic, A} = etorrent_torrent:all(),
54 {DownloadRate, UploadRate} = etorrent_rate_mgr:global_rate(),
55 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
56 ["Id:", "total", "left", "uploaded", "downloaded",
57 "I", "C", "Comp."]),
59 lists:foreach(
60 fun (R) ->
61 {atomic, [#tracking_map { filename = FN, _=_}]} =
62 etorrent_tracking_map:select(R#torrent.id),
63 io:format("~3.B ~11.B ~11.B ~11.B ~11.B ~3.B ~3.B ~7.3f% ~n",
64 [R#torrent.id,
65 R#torrent.total,
66 R#torrent.left,
67 R#torrent.uploaded,
68 R#torrent.downloaded,
69 R#torrent.leechers,
70 R#torrent.seeders,
71 percent_complete(R)]),
72 io:format(" ~s~n", [FN])
73 end, A),
74 %io:format("Rate Up/Down: ~e / ~e~n", [UploadRate, DownloadRate]).
75 io:format("Rate Up/Down: ~8.2f / ~8.2f~n", [UploadRate / 1024.0,
76 DownloadRate / 1024.0]).
78 %%--------------------------------------------------------------------
79 %% Function: show(Item) -> io()
80 %% Description: Show detailed information for Item
81 %%--------------------------------------------------------------------
82 show() ->
83 io:format("You must supply a torrent Id number~n").
85 show(Item) when is_integer(Item) ->
86 %{atomic, Torrent} = etorrent_torrent:select(Item),
87 case etorrent_tracking_map:select(Item) of
88 {atomic, [R]} ->
89 io:format("Id: ~3.B Name: ~s~n",
90 [R#tracking_map.id, R#tracking_map.filename]);
91 {atomic, []} ->
92 io:format("No such torrent Id~n")
93 end;
94 show(_) ->
95 io:format("Item supplied is not an integer~n").
97 %%--------------------------------------------------------------------
98 %% Function: check(Item) -> io()
99 %% Description: Check a torrents contents. For debugging.
100 %%--------------------------------------------------------------------
101 check(Id) ->
102 etorrent_t_manager:check_torrent(Id).
104 %%--------------------------------------------------------------------
105 %% Function: help() -> io()
106 %% Description: Provide a simple help message for the commands supported.
107 %%--------------------------------------------------------------------
108 help() ->
109 io:format("Available commands:~n", []),
111 Commands = [{"help, h", "This help"},
112 {"list, l", "List torrents in system"},
113 {"show, s", "Show detailed information for a given torrent"},
114 {"stop", "Stop the system"}],
116 lists:foreach(fun({Command, Desc}) ->
117 io:format("~-12.s - ~s~n", [Command, Desc])
118 end,
119 Commands),
122 %%--------------------------------------------------------------------
123 %% Abbreviations
124 %%--------------------------------------------------------------------
125 h() -> help().
126 l() -> list().
127 s() -> show().
128 s(Item) -> show(Item).
130 %% --------------------------------------------------------------------
131 %% Internal functions
132 %% --------------------------------------------------------------------
133 percent_complete(R) ->
134 %% left / complete * 100 = % done
135 (R#torrent.total - R#torrent.left) / R#torrent.total * 100.
137 generate_peer_id() ->
138 Number = crypto:rand_uniform(0, ?RANDOM_MAX_SIZE),
139 Rand = io_lib:fwrite("~B----------", [Number]),
140 lists:flatten(io_lib:format("-ET~s-~12s", [?VERSION, Rand])).