From b5dbdd1f5cad9e50c21098f06548ae0c168d928f Mon Sep 17 00:00:00 2001 From: Jesper Louis Andersen Date: Tue, 8 Jul 2008 21:10:11 +0200 Subject: [PATCH] Implement etorrent_event_mgr (renamed from etorrent_event). This implements a simple binlog report logger. We still need to hack up a working binlog reader and browser, but this is a good start. Another simple option is to write a more traditional logger (which is what we probably should do). --- lib/etorrent-1.0/priv/etorrent.config.example | 4 + lib/etorrent-1.0/src/etorrent_event.erl | 118 -------------------------- lib/etorrent-1.0/src/etorrent_event_mgr.erl | 44 ++++++++++ lib/etorrent-1.0/src/etorrent_sup.erl | 4 +- lib/etorrent-1.0/src/etorrent_t_control.erl | 4 +- lib/etorrent-1.0/src/etorrent_torrent.erl | 4 +- 6 files changed, 54 insertions(+), 124 deletions(-) delete mode 100644 lib/etorrent-1.0/src/etorrent_event.erl create mode 100644 lib/etorrent-1.0/src/etorrent_event_mgr.erl diff --git a/lib/etorrent-1.0/priv/etorrent.config.example b/lib/etorrent-1.0/priv/etorrent.config.example index 1e1e15d..bad03a4 100644 --- a/lib/etorrent-1.0/priv/etorrent.config.example +++ b/lib/etorrent-1.0/priv/etorrent.config.example @@ -1,6 +1,10 @@ [{etorrent, [{port, 1729}, {dir, "/var/tmp/etorrent"}]}, + {logger_dir, "/var/log/etorrent"}, + {logger_max_bytes, 64000}, + {logger_max_files, 10}]}, + {kernel, [{start_timer, true}]} # {sasl, diff --git a/lib/etorrent-1.0/src/etorrent_event.erl b/lib/etorrent-1.0/src/etorrent_event.erl deleted file mode 100644 index 0bc5789..0000000 --- a/lib/etorrent-1.0/src/etorrent_event.erl +++ /dev/null @@ -1,118 +0,0 @@ -%%%------------------------------------------------------------------- -%%% File : etorrent_event.erl -%%% Author : Jesper Louis Andersen <> -%%% Description : Event tracking inside the etorrent application. -%%% -%%% Created : 25 Aug 2007 by Jesper Louis Andersen <> -%%%------------------------------------------------------------------- --module(etorrent_event). - --behaviour(gen_event). -%% API --export([start_link/0, add_handler/0, - started_torrent/1, - checking_torrent/1, - seeding_torrent/1]). - -%% gen_event callbacks --export([init/1, handle_event/2, handle_call/2, - handle_info/2, terminate/2, code_change/3]). - --record(state, {}). - --define(SERVER, ?MODULE). - -%%==================================================================== -%% gen_event callbacks -%%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | {error,Error} -%% Description: Creates an event manager. -%%-------------------------------------------------------------------- -start_link() -> - gen_event:start_link({local, ?SERVER}). - -%%-------------------------------------------------------------------- -%% Function: add_handler() -> ok | {'EXIT',Reason} | term() -%% Description: Adds an event handler -%%-------------------------------------------------------------------- -add_handler() -> - gen_event:add_handler(?SERVER, ?MODULE, []). - -started_torrent(Id) -> - gen_event:notify(?SERVER, {started_torrent, Id}). - -checking_torrent(Id) -> - gen_event:notify(?SERVER, {checking_torrent, Id}). - -seeding_torrent(Id) -> - gen_event:notify(?SERVER, {seeding_torrent, Id}). - -%%==================================================================== -%% gen_event callbacks -%%==================================================================== -%%-------------------------------------------------------------------- -%% Function: init(Args) -> {ok, State} -%% Description: Whenever a new event handler is added to an event manager, -%% this function is called to initialize the event handler. -%%-------------------------------------------------------------------- -init([]) -> - {ok, #state{}}. - -%%-------------------------------------------------------------------- -%% Function: -%% handle_event(Event, State) -> {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% Description:Whenever an event manager receives an event sent using -%% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for -%% each installed event handler to handle the event. -%%-------------------------------------------------------------------- -handle_event(_Event, State) -> - {ok, State}. - -%%-------------------------------------------------------------------- -%% Function: -%% handle_call(Request, State) -> {ok, Reply, State} | -%% {swap_handler, Reply, Args1, State1, -%% Mod2, Args2} | -%% {remove_handler, Reply} -%% Description: Whenever an event manager receives a request sent using -%% gen_event:call/3,4, this function is called for the specified event -%% handler to handle the request. -%%-------------------------------------------------------------------- -handle_call(_Request, State) -> - Reply = ok, - {ok, Reply, State}. - -%%-------------------------------------------------------------------- -%% Function: -%% handle_info(Info, State) -> {ok, State} | -%% {swap_handler, Args1, State1, Mod2, Args2} | -%% remove_handler -%% Description: This function is called for each installed event handler when -%% an event manager receives any other message than an event or a synchronous -%% request (or a system message). -%%-------------------------------------------------------------------- -handle_info(_Info, State) -> - {ok, State}. - -%%-------------------------------------------------------------------- -%% Function: terminate(Reason, State) -> void() -%% Description:Whenever an event handler is deleted from an event manager, -%% this function is called. It should be the opposite of Module:init/1 and -%% do any necessary cleaning up. -%%-------------------------------------------------------------------- -terminate(_Reason, _State) -> - ok. - -%%-------------------------------------------------------------------- -%% Function: code_change(OldVsn, State, Extra) -> {ok, NewState} -%% Description: Convert process state when code is changed -%%-------------------------------------------------------------------- -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -%%-------------------------------------------------------------------- -%%% Internal functions -%%-------------------------------------------------------------------- diff --git a/lib/etorrent-1.0/src/etorrent_event_mgr.erl b/lib/etorrent-1.0/src/etorrent_event_mgr.erl new file mode 100644 index 0000000..72dcb2c --- /dev/null +++ b/lib/etorrent-1.0/src/etorrent_event_mgr.erl @@ -0,0 +1,44 @@ +%%%------------------------------------------------------------------- +%%% File : etorrent_event_mgr.erl +%%% Author : Jesper Louis Andersen <> +%%% Description : Event tracking inside the etorrent application. +%%% +%%% Created : 25 Aug 2007 by Jesper Louis Andersen <> +%%%------------------------------------------------------------------- +-module(etorrent_event_mgr). + +%% API +-export([start_link/0, + + started_torrent/1, + checking_torrent/1, + seeding_torrent/1]). + +-define(SERVER, ?MODULE). + +%%==================================================================== +%% gen_event callbacks +%%==================================================================== +%%-------------------------------------------------------------------- +%% Function: start_link() -> {ok,Pid} | {error,Error} +%% Description: Creates an event manager. +%%-------------------------------------------------------------------- +start_link() -> + {ok, Pid} = gen_event:start_link({local, ?SERVER}), + {ok, Dir} = application:get_env(etorrent, logger_dir), + {ok, MaxBytes} = application:get_env(etorrent, logger_max_bytes), + {ok, MaxFiles} = application:get_env(etorrent, logger_max_files), + Args = log_mf_h:init(Dir, MaxBytes, MaxFiles), + gen_event:add_handler(?SERVER, log_mf_h, Args), + {ok, Pid}. + +started_torrent(Id) -> + gen_event:notify(?SERVER, {started_torrent, Id}). + + +checking_torrent(Id) -> + gen_event:notify(?SERVER, {checking_torrent, Id}). + +seeding_torrent(Id) -> + gen_event:notify(?SERVER, {seeding_torrent, Id}). + diff --git a/lib/etorrent-1.0/src/etorrent_sup.erl b/lib/etorrent-1.0/src/etorrent_sup.erl index 2811592..e89c7c0 100644 --- a/lib/etorrent-1.0/src/etorrent_sup.erl +++ b/lib/etorrent-1.0/src/etorrent_sup.erl @@ -29,8 +29,8 @@ start_link() -> %%==================================================================== init([]) -> EventManager = {event_manager, - {etorrent_event, start_link, []}, - permanent, 2000, worker, [etorrent_event]}, + {etorrent_event_mgr, start_link, []}, + permanent, 2000, worker, [etorrent_event_mgr]}, Listener = {listener, {etorrent_listener, start_link, []}, permanent, 2000, worker, [etorrent_listener]}, diff --git a/lib/etorrent-1.0/src/etorrent_t_control.erl b/lib/etorrent-1.0/src/etorrent_t_control.erl index 3037fc1..5c93b39 100644 --- a/lib/etorrent-1.0/src/etorrent_t_control.erl +++ b/lib/etorrent-1.0/src/etorrent_t_control.erl @@ -112,7 +112,7 @@ initializing(timeout, S) -> %% TODO: Try to coalesce some of these operations together. %% Read the torrent, check its contents for what we are missing - etorrent_event:checking_torrent(S#state.id), + etorrent_event_mgr:checking_torrent(S#state.id), {ok, Torrent, FSPid, InfoHash, NumberOfPieces} = etorrent_fs_checker:read_and_check_torrent( S#state.id, @@ -157,7 +157,7 @@ initializing(timeout, S) -> S#state.id), %% Since the process will now go to a hibernation state, GC it - etorrent_event:started_torrent(S#state.id), + etorrent_event_mgr:started_torrent(S#state.id), garbage_collect(), {next_state, started, S#state{file_system_pid = FSPid, diff --git a/lib/etorrent-1.0/src/etorrent_torrent.erl b/lib/etorrent-1.0/src/etorrent_torrent.erl index 6b54610..2539e04 100644 --- a/lib/etorrent-1.0/src/etorrent_torrent.erl +++ b/lib/etorrent-1.0/src/etorrent_torrent.erl @@ -27,7 +27,7 @@ new(Id, {{uploaded, U}, {downloaded, D}, {left, L}, {total, T}}, NPieces) -> State = case L of 0 -> - etorrent_event:seeding_torrent(Id), + etorrent_event_mgr:seeding_torrent(Id), seeding; _ -> leeching end, @@ -153,7 +153,7 @@ statechange(Id, What) when is_integer(Id) -> {atomic, New} = mnesia:transaction(F), case New#torrent.left of 0 -> - etorrent_event:seeding_torrent(Id), + etorrent_event_mgr:seeding_torrent(Id), ok; _ -> ok -- 2.11.4.GIT