Skip to content

Commit

Permalink
Use gproc to simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
krestenkrab committed Nov 19, 2013
1 parent fb654fe commit 9fbf4b0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
5 changes: 3 additions & 2 deletions torrent_client/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ This directory holds a simple torrent client in Erlang.

In building this, the major challenge is to keep the project under 500
lines, since we're building it almost from first principles. Right
now it is ~750 lines (of which ~150 are blank or comments); I'll
now it is ~690 lines (of which ~140 are blank or comments); I'll
rework the code and the requirements to fit.

The easiest way to get Erlang, is to download it from www.erlang-solutions.org.
Don't use the Erlang distributed with Ubuntu.

compile:
./rebar get-deps
./rebar compile

run:
erl -pa ebin
erl -pa ebin -pa deps/gproc/ebin
1> torrent_file:download("../path/to.torrent")

6 changes: 6 additions & 0 deletions torrent_client/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


{deps, [
% {nat_upnp, ".*", {git, "git://github.com/benoitc/nat_upnp", {branch, "master"}}},
{gproc, ".*", {git, "git://github.com/uwiger/gproc", {branch, "master"}}}
]}.
6 changes: 3 additions & 3 deletions torrent_client/src/torrent_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ start() ->
application:start(torrent_client).

start_link() ->
ok = application:ensure_started(inets),
ok = application:ensure_started(crypto),
ok = application:ensure_started(inets),
ok = application:ensure_started(crypto),
ok = application:ensure_started(gproc),
proc_lib:start_link(?MODULE, init, [self()]).

init(Caller) ->
erlang:register(torrent_client, self()),

ets:new(torrent_owners, [public,named_table]),
ets:new(torrent_stats, [public,named_table]),

Rand = crypto:rand_bytes(12),
Expand Down
17 changes: 5 additions & 12 deletions torrent_client/src/torrent_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

-behavior(gen_server).
-export([init/1,terminate/2,handle_call/3,handle_cast/2,handle_info/2]).
-export([download/1,downloaded/2,find/1,piece_offset/2,piece_sha/2,piece_length/2]).
-export([download/1,downloaded/2,piece_offset/2,piece_sha/2,piece_length/2]).
-export([verify_piece/3]).

-record(state, { info :: #info{},
Expand All @@ -21,26 +21,20 @@
download(TorrentFile) ->
ok = application:ensure_started(torrent_client),
Info = read_torrent_file(TorrentFile),
case torrent_file:find(Info#info.info_hash) of
{ok, PID} ->
case gproc:lookup_pids({n,l,Info#info.info_hash}) of
[PID] ->
{ok, PID};
false ->
[] ->
gen_server:start_link(?MODULE, [Info], [])
end.

find(InfoHash) ->
case ets:lookup(torrent_owners, InfoHash) of
[{InfoHash,PID}] -> {ok, PID};
[] -> false
end.

downloaded(PID, Index) ->
gen_server:cast(PID, {downloaded, Index}).

init([Info]) ->
% erlang:process_flag(trap_exit, true),
gproc:add_local_name(Info#info.info_hash),
Blocks = ets:new(torrent_blocks, [public,ordered_set]),
true = ets:insert(torrent_owners, {Info#info.info_hash, self()}),
true = ets:insert(torrent_stats, {Info#info.info_hash, 0, 0}),
{ok, State=#state{ missing=Missing }} = init_download_file(#state{ info=Info, blocks=Blocks }),
case Missing of
Expand All @@ -49,7 +43,6 @@ init([Info]) ->
end.

terminate(_Reason, #state{ info=Info }) ->
ets:delete(torrent_owners, Info#info.info_hash),
ok.

handle_call(_Call,_,State) -> {stop, {error, unexpected_call, _Call}, State}.
Expand Down

0 comments on commit 9fbf4b0

Please sign in to comment.