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).
14 %% Start dependent applications
15 Fun
= fun(Application
) ->
16 case application:start(Application
) of
18 {error
, {already_started
, Application
}} -> ok
21 lists:foreach(Fun
, [crypto
, inets
, mnesia
, sasl
]),
23 etorrent_mnesia_init:wait(),
25 application:start(etorrent
).
27 start(_Type
, _Args
) ->
28 PeerId
= generate_peer_id(),
29 {ok
, Pid
} = etorrent_sup:start_link(PeerId
),
33 ok
= application:stop(etorrent
).
36 io:format("Shutting down etorrent~n").
42 ok
= mnesia:create_schema([node()]),
43 ok
= application:start(mnesia
),
44 etorrent_mnesia_init:init(),
48 %%--------------------------------------------------------------------
49 %% Function: list() -> io()
50 %% Description: List currently active torrents.
51 %%--------------------------------------------------------------------
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",
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",
71 percent_complete(R
)]),
72 io:format(" ~s~n", [FN
])
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 %%--------------------------------------------------------------------
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
89 io:format("Id: ~3.B Name: ~s~n",
90 [R#tracking_map
.id
, R#tracking_map
.filename
]);
92 io:format("No such torrent Id~n")
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 %%--------------------------------------------------------------------
102 etorrent_mgr:check(Id
).
104 %%--------------------------------------------------------------------
105 %% Function: help() -> io()
106 %% Description: Provide a simple help message for the commands supported.
107 %%--------------------------------------------------------------------
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
])
122 %%--------------------------------------------------------------------
124 %%--------------------------------------------------------------------
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
])).