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"),
37 ok
= etorrent_fast_resume:stop(),
44 ok
= mnesia:create_schema([node()]),
45 ok
= application:start(mnesia
),
46 etorrent_mnesia_init:init(),
50 %%--------------------------------------------------------------------
51 %% Function: list() -> io()
52 %% Description: List currently active torrents.
53 %%--------------------------------------------------------------------
55 {atomic
, A
} = etorrent_torrent:all(),
56 {DownloadRate
, UploadRate
} = etorrent_rate_mgr:global_rate(),
57 io:format("~3s ~11s ~11s ~11s ~11s ~3s ~3s ~7s~n",
58 ["Id:", "total", "left", "uploaded", "downloaded",
63 {atomic
, [#tracking_map
{ filename
= FN
, _
=_
}]} =
64 etorrent_tracking_map:select(R#torrent
.id
),
65 io:format("~3.B ~11.B ~11.B ~11.B ~11.B ~3.B ~3.B ~7.3f% ~n",
73 percent_complete(R
)]),
74 io:format(" ~s~n", [FN
])
76 %io:format("Rate Up/Down: ~e / ~e~n", [UploadRate, DownloadRate]).
77 io:format("Rate Up/Down: ~8.2f / ~8.2f~n", [UploadRate
/ 1024.0,
78 DownloadRate
/ 1024.0]).
80 %%--------------------------------------------------------------------
81 %% Function: show(Item) -> io()
82 %% Description: Show detailed information for Item
83 %%--------------------------------------------------------------------
85 io:format("You must supply a torrent Id number~n").
87 show(Item
) when is_integer(Item
) ->
88 %{atomic, Torrent} = etorrent_torrent:select(Item),
89 case etorrent_tracking_map:select(Item
) of
91 io:format("Id: ~3.B Name: ~s~n",
92 [R#tracking_map
.id
, R#tracking_map
.filename
]);
94 io:format("No such torrent Id~n")
97 io:format("Item supplied is not an integer~n").
99 %%--------------------------------------------------------------------
100 %% Function: check(Item) -> io()
101 %% Description: Check a torrents contents. For debugging.
102 %%--------------------------------------------------------------------
104 etorrent_mgr:check(Id
).
106 %%--------------------------------------------------------------------
107 %% Function: help() -> io()
108 %% Description: Provide a simple help message for the commands supported.
109 %%--------------------------------------------------------------------
111 io:format("Available commands:~n", []),
113 Commands
= [{"help, h", "This help"},
114 {"list, l", "List torrents in system"},
115 {"show, s", "Show detailed information for a given torrent"},
116 {"stop", "Stop the system"}],
118 lists:foreach(fun({Command
, Desc
}) ->
119 io:format("~-12.s - ~s~n", [Command
, Desc
])
124 %%--------------------------------------------------------------------
126 %%--------------------------------------------------------------------
130 s(Item
) -> show(Item
).
132 %% --------------------------------------------------------------------
133 %% Internal functions
134 %% --------------------------------------------------------------------
135 percent_complete(R
) ->
136 %% left / complete * 100 = % done
137 (R#torrent
.total
- R#torrent
.left
) / R#torrent
.total
* 100.
139 generate_peer_id() ->
140 Number
= crypto:rand_uniform(0, ?RANDOM_MAX_SIZE
),
141 Rand
= io_lib:fwrite("~B----------", [Number
]),
142 lists:flatten(io_lib:format("-ET~s-~12s", [?VERSION
, Rand
])).