PageRenderTime 14ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/run Torrent client/talkToJava.erl~

http://github.com/anonymous-admin/anonymous
Unknown | 161 lines | 136 code | 25 blank | 0 comment | 0 complexity | d5e0cc5625a3969fd708692bdefdf80c MD5 | raw file
  1-module(talkToJava).
  2-export([start_link/0, start_link/1, stop/0]).
  3-export([init/1, terminate/2, handle_cast/2, 
  4	 handle_info/2, code_change/3, handle_call/3]).
  5-behaviour(gen_server).
  6
  7-include("defs.hrl").
  8
  9start_link() ->
 10    start_link([]).
 11
 12start_link(_Args) ->
 13    ComputerName = net_adm:localhost(),
 14    NodeName = "javaNode@" ++ ComputerName,
 15    NodeAtom = list_to_atom(NodeName),
 16    gen_server:start_link({local, gui}, ?MODULE, [NodeAtom, dict:new()], []).
 17
 18stop() ->
 19    gen_server:cast(gui, stop).
 20
 21init([NodeAtom, TorrentIds]) ->
 22    timer:sleep(4000),
 23    {mailbox, NodeAtom} ! {self(), "startconnection"},
 24    gen_server:cast(msg_controller, {subscribe, gui, [{exit,-1}, {torrent_info,-1}, {torrent_status,-1}, 
 25                                                      {tracker_info,-1}, {seeders,-1}, {leechers,-1}, 
 26                                                      {uploaded,-1}, {downloaded,-1}, {left,-1},
 27						      {torrent_size,-1}, {pieces,-1},
 28                                                      {download_speed,-1}, {upload_speed,-1}, {filename,-1}, {default_path,-1}]}),
 29    {ok, {NodeAtom, TorrentIds}}.
 30
 31terminate(_Reason, {_NodeAtom, _TorrentIds}) ->
 32    gen_server:cast(gui, stop).
 33
 34handle_cast(stop, {_NodeAtom, _TorrentIds}) ->
 35    {stop, normal, {_NodeAtom, _TorrentIds}};
 36
 37handle_cast({notify, default_path, {-1, Value}}, {NodeAtom, TorrentIds}) ->
 38    Target ! {self(), TorrentId, 12, Value};
 39    {noreply, {NodeAtom, TorrentIds}};
 40
 41handle_cast({notify, _Tag, {-1, _Value}}, {NodeAtom, TorrentIds}) ->
 42    {noreply, {NodeAtom, TorrentIds}};
 43
 44handle_cast({notify, Tag, {Id, Value}}, {NodeAtom, TorrentIds}) ->
 45						%Check if the key already exists
 46    case lists:member(Id, dict:fetch_keys(TorrentIds)) of
 47	false -> NewDict = dict:append(Id, dict:size(TorrentIds)+1, TorrentIds);
 48	true  -> NewDict = TorrentIds
 49    end,
 50
 51    {ok, [TorrentId]} = dict:find(Id, NewDict),
 52
 53    Target = {mailbox2, NodeAtom},
 54
 55    %%receives from other erlang modules sends back to notice java
 56    case Tag of
 57    	torrent_info ->
 58	    Filename = Value#torrent.filename,
 59						%Seeders = Value#torrent.seeders,
 60						%Leechers = Value#torrent.leechers,
 61	    Size = Value#torrent.size,
 62	    Downloaded = Value#torrent.downloaded,
 63	    {Files,_} = Value#torrent.files,
 64						%Target ! {self(), TorrentId, 5, Seeders},
 65						%Target ! {self(), TorrentId, 6, Leechers},
 66	    Target ! {self(), TorrentId, 1, Size},
 67	    Target ! {self(), TorrentId, 7, Downloaded},
 68	    Target ! {self(), TorrentId, 0, Filename},
 69	    sendFiles(Files, NodeAtom, TorrentId);
 70
 71	error -> 
 72	    Target ! {self(), TorrentId, 11, Value};
 73
 74	filename ->
 75	    Target ! {self(), TorrentId, 0, Value};
 76
 77	tracker_info ->
 78	    Uploaded = Value#tracker_info.uploaded,
 79	    Target ! {self(), TorrentId, 8, Uploaded};
 80
 81	file_path ->
 82	    Files = Value#torrent.files,
 83	    Target ! {self(), TorrentId, 10, Files};
 84
 85	torrent_status ->
 86	    case Value of
 87		finished -> {mailbox2, NodeAtom} ! {self(), TorrentId, 9, Value};
 88	        _ -> ok
 89	    end;
 90
 91	torrent_size ->
 92	    Target ! {self(), TorrentId, 1, Value};
 93	download_speed ->
 94	    Target ! {self(), TorrentId, 3, Value};
 95	upload_speed ->
 96	    Target ! {self(), TorrentId, 4, Value};
 97	seeders ->
 98	    Target ! {self(), TorrentId, 5, Value};
 99	leechers ->
100	    Target ! {self(), TorrentId, 6, Value};
101	downloaded ->
102	    Target ! {self(), TorrentId, 7, Value};
103	uploaded ->
104	    Target ! {self(), TorrentId, 8, Value};
105	finished ->
106	    Target ! {self(), TorrentId, 9, "Torrent finished downloading"}
107    end,
108    {noreply, {NodeAtom, NewDict}}.
109
110handle_info(Info, {NodeAtom, TorrentIds}) ->
111    case Info of
112        %%receives from java
113	{_, connok} ->
114	    io:format("Message received: ~p~n", [connectionok]);   
115	{_, exit} ->
116	    io:format("Message received: ~p~n", [exited]),
117	    gen_server:cast(msg_controller, {notify, exit, exit});
118	{_, open,FileDir} ->
119	    io:format("Message received: ~p~n", [FileDir]),
120	    gen_server:cast(msg_controller, {notify, torrent_status, {-1, opened}}),
121	    gen_server:cast(msg_controller, {notify, torrent_filepath,{-1, FileDir}});
122	{_, Id, start} ->
123	    io:format("Message received: ~p~n", [resumed]),
124	    gen_server:cast(msg_controller, {notify, torrent_status,{getIdAtom(Id, TorrentIds), resume}});
125	{_, Id, stop} ->
126	    io:format("Message received: ~p~n", [stopped]),
127       	    gen_server:cast(msg_controller, {notify, torrent_status,{getIdAtom(Id, TorrentIds), stopped}});
128	{_, Id, pause} ->
129	    io:format("Message received: ~p~n", [paused]),
130	    gen_server:cast(msg_controller, {notify, torrent_status, {getIdAtom(Id, TorrentIds), paused}});
131	{_, Id, delete} ->
132	    io:format("Message received: ~p for id: ~p~n", [deleted, getIdAtom(Id, TorrentIds)]),
133	    gen_server:cast(msg_controller, {notify, torrent_status, {getIdAtom(Id, TorrentIds), deleted}});
134	{_, dir,DirList} ->
135	    io:format("directory: ~p~n", [DirList]),
136	    gen_server:cast(msg_controller, {notify, default_path,{-1, DirList}})
137    end,
138    {noreply, {NodeAtom, TorrentIds}}.
139
140sendFiles([],_,_) ->
141    ok;
142sendFiles([[ToSend,_]|T], NodeAtom, TorrentId) ->
143    {mailbox2, NodeAtom} ! {self(), TorrentId, 10, binary_to_list(hd(lists:reverse(ToSend)))},
144    sendFiles(T, NodeAtom, TorrentId).
145
146getIdAtom([], _Num, _TorrentIds) ->
147    {error, not_found};
148getIdAtom([H|T], Num, TorrentIds) ->
149    case {ok, [Num]} == dict:find(H, TorrentIds) of
150	true  -> H;
151	false -> getIdAtom(T, Num, TorrentIds)
152    end.
153
154getIdAtom(Num, TorrentIds) ->
155    getIdAtom(dict:fetch_keys(TorrentIds), Num, TorrentIds).
156
157code_change(_, _, _) ->
158    ok.
159
160handle_call(_, _, _) ->
161    ok.