Commits

wjzz committed 380bd1c Draft

Added transfer rate calculation to monitor.

Comments (0)

Files changed (5)

Erlang/logging.erl

                end,       
     {ok, Response};
 
+router({"downloaded", _Params}) ->
+    DownloadedBytes = pieces:downloaded_bytes(),
+    Reply = io_lib:format("~p~n", [DownloadedBytes]),
+    {ok, Reply};
+
 router({"piece_completion", _Params}) ->
     {{pieces_num, PiecesNum},
      {blocks_num, BlocksNum},

Erlang/pieces.erl

 -record(state, {meta_data, 
                 file_name,
                 download_dir,
+                downloaded_bytes,
                 piece_dict=dict:new(),
                 finished
                }).
          completed_pieces/0,
          save_backup/0,
          status_report/0,
+         downloaded_bytes/0,
          stop/0
          ]).
 
     gen_server:call(?SERVER, completed_pieces).
 
 %%--------------------------------------------------------------------
+%% @doc Queries the server for the number of downloaded bytes 
+%%      in the current session.
+%%
+%% @spec downloaded_bytes() -> integer() 
+%% @end
+%%--------------------------------------------------------------------
+
+downloaded_bytes() ->
+    gen_server:call(?SERVER, downloaded_bytes).    
+
+%%--------------------------------------------------------------------
 %% @doc Asks the piece server to write the backup file.
 %%
 %% @end
     {ok, #state{meta_data = MetaData,
                 file_name = FileName,
                 finished  = InitialFinished,
-                download_dir = DownloadDir
+                download_dir = DownloadDir,
+                downloaded_bytes = 0
                }}.
 
 handle_info(timeout, State) ->
     PiecesNos = [ Key || {Key, _} <- ets:tab2list(Finished) ],
     {reply, PiecesNos, State};
 
+handle_call(downloaded_bytes, _From, State) ->
+    Reply = State#state.downloaded_bytes,
+    {reply, Reply, State};
+
 handle_call(status_report, _From, State) ->
     MetaData  = State#state.meta_data,
     PieceDict = State#state.piece_dict,
 %%     {noreply, State#state{piece_dict = UpdatedPieceDict}}.
 
 
-add_new_block(PeerPid, {PartNo, BlockNo, Data}, BlockList, State) ->
+add_new_block(PeerPid, {PartNo, BlockNo, Data}, BlockList, InitState) ->
+    GotBytes = InitState#state.downloaded_bytes,
+    State = InitState#state{downloaded_bytes = GotBytes + ?SIZE},
     PieceDict = State#state.piece_dict,
     %% did we just finish the piece?
     case all_parts(State#state.meta_data,

Erlang/torrent.erl

     start(9999).
 
 start(MonitorPortNo) ->
-    download_torrent(MonitorPortNo, metadata:multi()).
+    download_torrent(MonitorPortNo, metadata:single()).
 
 close() ->
     io:format("Stopping the download!~n"),

Erlang/www/index.html

     <div id="peer_info">
     </div>
 
+    <div id="transfer_info">
+    </div>
+
+
     <hr />
 
     <div id="piece_info">

Erlang/www/monitor.js

+var INTERVAL = 3 * 1000;
+var downloaded_prev = 0;
+
 function get_peers_completion(){
     $.get("/peers_completion", function(data){
         $('#peers').html("<pre>"+data+"</pre>");
     });
 }
 
+function pretty_size(rate){
+    var suffixes = ["b", "Kb", "Mb", "Gb"];
+    
+    var i = 0;
+    while (rate >= 1024.0){
+        rate /= 1024.0;
+        i++;
+    }
+    return (Math.round(rate) + ' ' + suffixes[i]);
+}
+
+function get_downloaded_bytes(){
+//    $('#transfer_info')
+    $.get("/downloaded", function(data){
+        var downloaded = parseInt(data, 10);
+        var delta = downloaded - downloaded_prev;
+        downloaded_prev = downloaded;
+        
+        var rate = delta / (INTERVAL / 1000);
+        var pretty_rate = pretty_size(rate);
+        $('#transfer_info').html("Download speed: <strong>"+pretty_rate+"/s</strong>.");
+    });
+
+}
+
 function interval_function(){
     get_peers_completion();
+    get_downloaded_bytes();
     get_piece_info();
 }   
 
 $(document).ready(function(){
-    setInterval(interval_function, 10000);    
+    setInterval(interval_function, INTERVAL);    
 });