PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/run Torrent client/tracker_interactor.erl~

http://github.com/anonymous-admin/anonymous
Unknown | 97 lines | 79 code | 18 blank | 0 comment | 0 complexity | ad3337342623966999bf872c498dea9a MD5 | raw file
 1%%Author: Massih
 2%%Creation Date: 2-Nov-2011
 3-module(tracker_interactor).
 4-export([init/1,start_link/1,handle_cast/2,terminate/2]).
 5-export([handle_call/3, handle_info/2, code_change/3]).
 6-export([create_record/0, make_url/1, send_request/1, response_handler/1]).
 7-behaviour(gen_server).
 8-include("defs.hrl").
 9
10
11init([Tracker, Timeout, Torrent])->
12    {ok, {Tracker, Torrent}, Timeout}.
13
14terminate(_Reason,_Data)->
15    gen_server:cast(self(), stop).
16
17start_link([Tracker, Timeout, Torrent])->
18    gen_server:start_link(?MODULE,[Tracker, Timeout, Torrent],[]).
19
20send_request(URL)->
21    inets:start(),
22    httpc:request(URL).
23
24make_url(T)->
25    io:format("Creating URL: ~p~n", [T]),
26    T#tracker_info.url++"?info_hash="++T#tracker_info.info_hash++"&peer_id="++T#tracker_info.peer_id++"&port="++T#tracker_info.port++"&downloaded="++T#tracker_info.downloaded++"&left="++T#tracker_info.left++"&event="++T#tracker_info.event++"&numwant="++T#tracker_info.num_want++"&uploaded="++T#tracker_info.uploaded++"&compact=1".
27
28
29response_handler(Body)->
30    Decoded_Body = parser:decode(list_to_binary(Body)),
31    [Interval,Seeds,Leechers,Peers]=interpreter:get_tracker_response_info(Decoded_Body),
32    io:format("after response info~n"),
33    %%io:format("seeds:~p  leechers:~p  interval :~p\n",[S,L,NewInterval]),
34    [Seeds,Leechers,Interval,Peers].
35
36handle_cast(stop, _Data)->
37    {stop, normal, _Data};
38
39handle_cast(tracker_request_info,{Tracker, Torrent})->
40    URL = make_url(Tracker),
41    io:format("The url is ~p~n", [URL]),
42    case send_request(URL) of
43	{ok,{Status_line, _Headers, Body}} ->
44	    case Status_line of
45		{"HTTP/1.0",200,"OK"} ->    
46		    [Seeders,Leechers,Interval,Peers] = response_handler(Body),
47		    %spawn_peers(Peers, Torrent),
48	            NewTracker = Tracker#tracker_info{interval=integer_to_list(Interval)},
49		    %SEND BACK INFO TO TORRENT
50	            gen_server:cast(msg_controller, {notify, seeders, {Torrent#torrent.id, integer_to_list(Seeders)}}),
51	            {noreply, {{NewTracker,Torrent},Interval}};
52		_ -> {stop, normal, []}		
53	    end;
54	{error,_Reason} ->
55	    %%gen_server:cast(msg_controller,{tracker_request_error,Reason})
56	    io:format("Request Error"),
57	    {stop, normal, []}
58    end.
59
60handle_info(timeout, {Tracker, Torrent}) ->
61    case whereis(Torrent#torrent.id) of
62	undefined -> 
63	    NewTracker = Tracker,
64	    {error, dead},
65	    {stop, normal, {Tracker, Torrent}}; 
66        _         ->
67            {Downloaded, Left, Event, Max_peers} = gen_server:call(Torrent#torrent.id, get_dynamics),
68            NewTracker = Tracker#tracker_info{downloaded=Downloaded, left=Left, event=Event, num_want=Max_peers},
69            gen_server:cast(self(), tracker_request_info),
70	    {noreply, {NewTracker,Torrent},NewTracker#tracker_info.interval} 
71    end.
72    
73
74create_record() ->
75    T = #tracker_info{url = "http://torrent.fedoraproject.org:6969/announce",
76		      info_hash = "%16%71%26%41%9E%F1%84%B4%EC%8F%B3%CA%46%5A%B7%FE%D1%97%51%9A",
77		      peer_id = "edocIT00855481937666",
78		      port = "6881"},
79    T.
80
81spawn_peers([], _) ->
82    ok;
83spawn_peers([H|T], Torrent) ->
84    [Ip,Port] = H,
85    case whereis(list_to_atom(Ip)) of
86	undefined -> dynamic_supervisor:start_peer(Torrent, Ip, Port);
87	_         -> ok
88    end,
89    spawn_peers(T, Torrent).
90
91handle_call(_,_,_) ->
92    ok.
93
94code_change(_,_,_) ->
95    ok.
96    
97