Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 8 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
6 src/servtorrent_sup.erl
@@ -36,9 +36,9 @@ init([]) ->
36 36 PeerSup = {peer_sup,
37 37 {peer_sup, start_link, []},
38 38 permanent, 2000, supervisor, [peer_sup]},
39   - Tracker = {tracker_web,
40   - {tracker_web, start, [[{ip, "0.0.0.0"}, {port, 6969}]]},
41   - permanent, 2000, worker, [tracker_web]},
  39 + Tracker = {tracker_sup,
  40 + {tracker_sup, start_link, []},
  41 + permanent, 2000, supervisor, [tracker_sup]},
42 42 SeedList = {seedlist,
43 43 {seedlist, start_link, []},
44 44 permanent, 2000, worker, [seedlist]},
50 src/tracker_manager.erl
... ... @@ -0,0 +1,50 @@
  1 +-module(tracker_manager).
  2 +-behaviour(gen_server).
  3 +
  4 +%gen server stuff
  5 +-export([start_link/0]).
  6 +-export([init/1, handle_call/3, handle_cast/2, handle_info/2,terminate/2, code_change/3]).
  7 +
  8 +% actual api
  9 +-export([check_timeout/0]).
  10 +
  11 +-define(SERVER, ?MODULE).
  12 +
  13 +
  14 +-record(state,{timers=[]}).
  15 +
  16 +
  17 +start_link() ->
  18 + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  19 +
  20 +
  21 +check_timeout() ->
  22 + gen_server:call(?SERVER, check_timeout, 15 * 60 * 1000).
  23 +
  24 +init([]) ->
  25 + {ok, #state{timers = init_timers()}}.
  26 +
  27 +init_timers() ->
  28 + {ok, Timer} = timer:apply_interval(5 * 60 * 1000, ?MODULE, check_timeout, []),
  29 + [Timer].
  30 +
  31 +handle_call(check_timeout, _From, State) ->
  32 + % check timeouts on registered peers
  33 + trackerdb:remove_peers_with_timeout_in_seconds(60*15),
  34 + {reply, ok, State}.
  35 +
  36 +
  37 +% handle death and cleanup of logged in processes
  38 +handle_info(_Info, State) ->
  39 + {noreply, State}.
  40 +
  41 +handle_cast(_Msg, State) ->
  42 + {noreply, State}.
  43 +
  44 +terminate(_Reason, State) ->
  45 + lists:foreach(fun timer:cancel/1,State#state.timers), % cancel all timers
  46 + ok.
  47 +
  48 +code_change(_OldVsn, State, _Extra) ->
  49 + {ok, State}.
  50 +
29 src/tracker_sup.erl
... ... @@ -0,0 +1,29 @@
  1 +-module(tracker_sup).
  2 +-behaviour(supervisor).
  3 +
  4 +%% API
  5 +-export([start_link/0]).
  6 +%% Supervisor callbacks
  7 +-export([init/1]).
  8 +
  9 +
  10 +-define(SERVER, ?MODULE).
  11 +
  12 +start_link() ->
  13 + supervisor:start_link({local, ?SERVER}, ?MODULE, []).
  14 +
  15 +init([]) ->
  16 + RestartStrategy = one_for_one,
  17 + MaxRestarts = 10,
  18 + MaxSecondsBetweenRestarts = 5,
  19 +
  20 + SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
  21 +
  22 + TrackerWeb = {tracker_web,
  23 + {tracker_web, start, [[{ip, "0.0.0.0"}, {port, 6969}]]},
  24 + permanent, 2000, worker, [tracker_web]},
  25 + TrackerManager = {tracker_manager,
  26 + {tracker_manager, start_link, []},
  27 + permanent, 2000, worker, [tracker_manager]},
  28 +
  29 + {ok, {SupFlags, [TrackerWeb, TrackerManager]}}.
48 src/trackerdb.erl
... ... @@ -1,6 +1,8 @@
1 1 -module(trackerdb).
2 2
3   --export([init/0, announce/7]).
  3 +-export([init/0, announce/7, unix_seconds_since_epoch/0, remove_peers_with_timeout_in_seconds/1]).
  4 +
  5 +-include_lib("stdlib/include/qlc.hrl").
4 6
5 7 -record(pirate, { id, info_hash, ip, port, peer_id,
6 8 uploaded, downloaded, left,
@@ -14,7 +16,7 @@ init() ->
14 16 announce(InfoHash, Ip, Port, PeerId, Uploaded, Downloaded, Left) ->
15 17 {atomic, Result} = mnesia:transaction(fun() ->
16 18 AllPeers = mnesia:index_read(pirate, InfoHash, #pirate.info_hash),
17   - Now = make_timestamp(now),
  19 + Now = unix_seconds_since_epoch(),
18 20 PeerUpdate = case mnesia:read(pirate, { InfoHash, Ip, Port }) of
19 21 [Peer = #pirate{ }] -> Peer#pirate { peer_id = PeerId, uploaded = Uploaded,
20 22 downloaded = Downloaded, left = Left, last_seen = Now };
@@ -32,7 +34,43 @@ announce(InfoHash, Ip, Port, PeerId, Uploaded, Downloaded, Left) ->
32 34 end),
33 35 Result.
34 36
  37 +remove_peers_with_timeout_in_seconds(Seconds) ->
  38 + KillTime = unix_seconds_since_epoch() - Seconds, % all pirates iwth an update date of this and below need to go away
  39 + Q = qlc:q([Pirate || Pirate <- mnesia:table(pirate), Pirate#pirate.last_seen < KillTime]),
  40 + F = fun () ->
  41 + PiratesToKill = qlc:e(Q),
  42 + lists:foreach( fun(Pirate) -> mnesia:delete_object(Pirate) end, PiratesToKill),
  43 + io:format("Killing Pirates: ~p \n",[PiratesToKill]) % should not do this in a transaction…
  44 + end,
  45 + graceful_transaction(F).
  46 +
  47 +graceful_transaction(F) ->
  48 + case mnesia:transaction(F) of
  49 + {atomic, Result} ->
  50 + Result;
  51 + {aborted, Reason} ->
  52 + io:format("transaction abort: ~p~n",[Reason]),
  53 + []
  54 + end.
  55 +
  56 +
  57 +% find(Q) ->
  58 +% F = fun() ->
  59 +% qlc:e(Q)
  60 +% end,
  61 +% graceful_transaction(F).
  62 +%
  63 +% read_all(Table) ->
  64 +% Q = qlc:q([X || X <- mnesia:table(Table)]),
  65 +% graceful_transaction(Q).
  66 +
  67 +
  68 +unix_seconds_since_epoch() ->
  69 + LocalDateTime = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
  70 + UnixEpoch = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
  71 + LocalDateTime - UnixEpoch.
  72 +
35 73
36   -make_timestamp(now) ->
37   - { MegaSeconds, Seconds, MicroSeconds} = erlang:now(),
38   - (MegaSeconds * 1000000 + Seconds) * 1000000 + MicroSeconds.
  74 +% make_timestamp(now) ->
  75 +% { MegaSeconds, Seconds, MicroSeconds} = erlang:now(),
  76 +% (MegaSeconds * 1000000 + Seconds) * 1000000 + MicroSeconds.
1  start.sh
@@ -8,4 +8,5 @@ erl -pa ebin -make && \
8 8 erl -config sasl +K true -smp auto \
9 9 -pa ebin \
10 10 -s servtorrent \
  11 + -s reloader \
11 12 $ENV

No commit comments for this range

Something went wrong with that request. Please try again.