wjzz avatar wjzz committed d8a0409 Draft

Automatic backup saving done.

Comments (0)

Files changed (2)

Erlang/pieces.erl

 -define(SERVER, ?MODULE).
 -record(state, {meta_data, 
                 file_name,
+                download_dir,
                 piece_dict=dict:new(),
                 finished
                }).
 
 %% API
 -export([
-         start_link/1,
+         start_link/2,
          downloaded_block/2,
          completed_pieces/0,
+         save_backup/0,
          status_report/0,
          stop/0
          ]).
 %% @end
 %%--------------------------------------------------------------------
 
-start_link(MetaData) ->
-    gen_server:start_link({local, ?SERVER}, ?MODULE, [MetaData], []).
+start_link(MetaData, DownloadDir) ->
+    gen_server:start_link({local, ?SERVER}, ?MODULE, [MetaData, DownloadDir], []).
 
 %%--------------------------------------------------------------------
 %% @doc Stores the block that has just been downloaded and checks if
     gen_server:call(?SERVER, completed_pieces).
 
 %%--------------------------------------------------------------------
+%% @doc Asks the piece server to write the backup file.
+%%
+%% @end
+%%--------------------------------------------------------------------
+
+save_backup() ->
+    gen_server:cast(?SERVER, save_backup).
+
+%%--------------------------------------------------------------------
 %% @doc Returns the list of downloaded pieces and blocks
 %%
 %% @spec status_report() -> ??? (TODO)
 %%%===================================================================
 
 
-init([MetaData]) ->
-    FileName = filename:join(["download", "temp.dat"]),
+init([MetaData, DownloadDir]) ->
+
+    FileName = filename:join([DownloadDir, "temp.dat"]),
     
     InitialFinished = try_read_backup(FileName),
     InitLen = ets:info(InitialFinished, size),
 
     {ok, #state{meta_data = MetaData,
                 file_name = FileName,
-                finished  = InitialFinished
+                finished  = InitialFinished,
+                download_dir = DownloadDir
                }}.
 
 handle_info(timeout, State) ->
         true -> 
             io:format("FINISHED!!!~n"),
             FinishedPieces = ets:tab2list(Finished),
-            create_downloaded_files("download", 
+            DownloadDir = State#state.download_dir,
+            create_downloaded_files(DownloadDir,
                                     metadata:file_infos(MetaData),
                                     FinishedPieces)                                    
     end,
 %% @end
 %%--------------------------------------------------------------------
 
+handle_cast(save_backup, State) ->
+    io:format("BACKUP SAVED.~n"),
+    FileName = State#state.file_name,
+    Finished = State#state.finished,
+
+    save_pieces_to_disk(FileName, Finished),
+    {noreply, State};
+
 handle_cast(stop, State) ->
     FileName = State#state.file_name,
     Finished = State#state.finished,
 start_test() ->
     stop(),
     MetaData = metadata:multi(),
-    {ok, _Pid} = start_link(MetaData).
+    {ok, _Pid} = start_link(MetaData, "tralala").
     
 all_parts_test() ->
     MetaData = metadata:multi(),

Erlang/torrent.erl

     start_worker(MetaInfo),
     peer_stats:start(MetaInfo),
 
-    pieces:start_link(MetaInfo),
+    DownloadDir = filename:join(["/home", "wjzz", "TextMining", "download"]),
+    pieces:start_link(MetaInfo, DownloadDir),
     CompletedPieces = pieces:completed_pieces(),
     scheduler:start_link(MetaInfo, CompletedPieces),
 
+    %% write a backup file every 5 minutes
+    {ok, _} = timer:apply_interval(5 * 60 * 1000, pieces, save_backup, []),
+
     %% TO BE ENABLED LATER
     %% 
     %%start_timer(5),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.