Skip to content

Commit

Permalink
Add speed info into etorrent_console.
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Feb 14, 2013
1 parent ef15c7e commit d620247
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/etorrent_console.erl
Expand Up @@ -40,8 +40,9 @@
'uploaded' :: integer(),
'state' :: atom(),

'speed_in' = 0 :: integer(),
'speed_out' = 0 :: integer()
%% Byte per second
'speed_in' = 0.0 :: float(),
'speed_out' = 0.0 :: float()
}).

-record(state, {
Expand Down Expand Up @@ -85,7 +86,7 @@ handle_info(update, SD=#state{torrents=OldTorrents, tick=Timeout}) ->
UnsortedNewTorrents = lists:map(fun to_record/1, PLs),
NewTorrents = sort_records(UnsortedNewTorrents),
NewTorrents2 = calc_speed_records(OldTorrents, NewTorrents, Timeout),
map_records2(fun print_torent_info/2, OldTorrents, NewTorrents),
map_records2(fun print_torent_info/2, OldTorrents, NewTorrents2),
{noreply, SD#state{torrents=NewTorrents2}}.

terminate(_Reason, _SD) ->
Expand Down Expand Up @@ -129,19 +130,19 @@ sort_records(List) ->


calc_speed_records(Olds, News, Tick) ->
FU = fun(#torrent{uploaded=X, speed_out=0},
#torrent{uploaded=X, speed_out=0}=New) -> New;
FU = fun(#torrent{uploaded=X, speed_out=0.0},
#torrent{uploaded=X, speed_out=0.0}=New) -> New;
(#torrent{uploaded=X},
#torrent{uploaded=X}=New) -> New#torrent{speed_out=0};
#torrent{uploaded=X}=New) -> New#torrent{speed_out=0.0};
(#torrent{uploaded=O},
#torrent{uploaded=N}=New) ->
New#torrent{speed_out=calc_speed(O, N, Tick)}
end,

FD = fun(#torrent{downloaded=X, speed_in=0},
#torrent{downloaded=X, speed_in=0}=New) -> New;
FD = fun(#torrent{downloaded=X, speed_in=0.0},
#torrent{downloaded=X, speed_in=0.0}=New) -> New;
(#torrent{downloaded=X},
#torrent{downloaded=X}=New) -> New#torrent{speed_in=0};
#torrent{downloaded=X}=New) -> New#torrent{speed_in=0.0};
(#torrent{downloaded=O},
#torrent{downloaded=N}=New) ->
New#torrent{speed_in=calc_speed(O, N, Tick)}
Expand Down Expand Up @@ -201,10 +202,12 @@ print_torent_info(undefined, #torrent{id=Id}) ->
log("STARTED torrent #~p.", [Id]);
print_torent_info(#torrent{id=Id}, undefined) ->
log("STOPPED torrent #~p.", [Id]);
print_torent_info(_Old, #torrent{state=Status, id=Id, left=Left, total=Total}) ->
print_torent_info(_Old, #torrent{state=Status, id=Id, left=Left, total=Total,
speed_in=SpeedIn, speed_out=SpeedOut}) ->
DownloadedPercent = (Total-Left)/Total * 100,
log("~.10s #~p: ~6.2f%", [string:to_upper(atom_to_list(Status)),
Id, DownloadedPercent]).
log("~.10s #~p: ~6.2f% ~s in, ~s out",
[string:to_upper(atom_to_list(Status)), Id, DownloadedPercent,
pretty_speed(SpeedIn), pretty_speed(SpeedOut)]).

% 'leechers' :: integer(),
% 'seeders' :: integer(),
Expand All @@ -215,6 +218,15 @@ log(Pattern, Args) ->
io:format(user, Pattern ++ "~n", Args),
ok.

pretty_speed(BPS) when BPS < 1024 ->
io_lib:format("~7.2f B/s ", [BPS]);
pretty_speed(BPS) when BPS < 1024*1024 ->
io_lib:format("~7.2f KiB/s", [BPS / 1024]);
pretty_speed(BPS) ->
io_lib:format("~7.2f MiB/s", [BPS / (1024*1024)]).



-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

Expand Down
1 change: 1 addition & 0 deletions src/etorrent_torrent.erl
Expand Up @@ -31,6 +31,7 @@
-type(torrent_state() :: 'leeching' | 'seeding' | 'endgame' | 'paused' | 'unknown').

%% A single torrent is represented as the 'torrent' record
%% TODO: How many seeders/leechers are we connected to?
-record(torrent,
{ %% Unique identifier of torrent, monotonically increasing
id :: non_neg_integer(),
Expand Down

0 comments on commit d620247

Please sign in to comment.