Skip to content

Commit

Permalink
Introduced socketio_listener and therefore refactored supervision tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed Feb 26, 2011
1 parent f8dd0df commit 43562fd
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 29 deletions.
4 changes: 2 additions & 2 deletions demo/demo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
main(_) ->
application:start(sasl),
application:start(socketio),
{ok, Pid} = socketio_http:start(7878, ?MODULE),
EventMgr = socketio_http:event_manager(Pid),
{ok, _Pid, EventMgr} = socketio_listener:start([{http_port, 7878},
{default_http_handler,?MODULE}]),
ok = gen_event:add_handler(EventMgr, ?MODULE,[]),
receive _ -> ok end.

Expand Down
8 changes: 5 additions & 3 deletions src/socketio_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-behaviour(gen_server).

%% API
-export([start_link/2, start/2]).
-export([start_link/2, start/3]).
-export([event_manager/1, send/2, session_id/1]).

%% gen_server callbacks
Expand Down Expand Up @@ -35,8 +35,10 @@
start_link(SessionId, ConnectionReference) ->
gen_server:start_link(?MODULE, [SessionId, ConnectionReference], []).

start(SessionId, ConnectionReference) ->
supervisor:start_child(socketio_client_sup, [SessionId, ConnectionReference]).
start(Sup0, SessionId, ConnectionReference) ->
Children = supervisor:which_children(Sup0),
{Sup, _, _, _} = lists:keyfind(socketio_client_sup,1, Children),
supervisor:start_child(Sup, [SessionId, ConnectionReference]).

send(Server, Message) ->
gen_server:cast(Server, {send, Message}).
Expand Down
32 changes: 14 additions & 18 deletions src/socketio_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
-behaviour(gen_server).

%% API
-export([start_link/2, start/2]).
-export([event_manager/1]).
-export([start_link/4]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
Expand All @@ -15,7 +14,8 @@
-record(state, {
default_http_handler,
sessions,
event_manager
event_manager,
sup
}).

%%%===================================================================
Expand All @@ -29,14 +29,8 @@
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link(Port, DefaultHttpHandler) ->
gen_server:start_link(?MODULE, [Port, DefaultHttpHandler], []).

start(Port, DefaultHttpHandler) ->
supervisor:start_child(socketio_http_sup, [Port, DefaultHttpHandler]).

event_manager(Server) ->
gen_server:call(Server, event_manager).
start_link(Port, DefaultHttpHandler, EventManager, Sup) ->
gen_server:start_link(?MODULE, [Port, DefaultHttpHandler, EventManager, Sup], []).

%%%===================================================================
%%% gen_server callbacks
Expand All @@ -53,19 +47,19 @@ event_manager(Server) ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([Port, DefaultHttpHandler]) ->
init([Port, DefaultHttpHandler, EventManager, Sup]) ->
Self = self(),
process_flag(trap_exit, true),
misultin:start_link([{port, Port},
{loop, fun (Req) -> handle_http(Self, Req) end},
{ws_loop, fun (Ws) -> handle_websocket(Self, Ws) end},
{ws_autoexit, false}
]),
{ok, EventMgr} = gen_event:start_link(),
{ok, #state{
default_http_handler = DefaultHttpHandler,
sessions = ets:new(socketio_sessions,[public]),
event_manager = EventMgr
event_manager = EventManager,
sup = Sup
}}.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -100,11 +94,13 @@ handle_call({request, Path, Req}, _From, #state{ default_http_handler = HttpHand
{reply, Response, State};

%% Sessions
handle_call({session, generate, ConnectionReference}, _From, #state{ sessions = Sessions,
event_manager = EventManager
} = State) ->
handle_call({session, generate, ConnectionReference}, _From, #state{
sup = Sup,
sessions = Sessions,
event_manager = EventManager
} = State) ->
UUID = binary_to_list(ossp_uuid:make(v4, text)),
{ok, Pid} = socketio_client:start(UUID, ConnectionReference),
{ok, Pid} = socketio_client:start(Sup, UUID, ConnectionReference),
link(Pid),
ets:insert(Sessions, [{UUID, Pid}, {Pid, UUID}]),
gen_event:notify(EventManager, {client, Pid}),
Expand Down
8 changes: 8 additions & 0 deletions src/socketio_listener.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(socketio_listener).
-export([start/1]).

start(Options) ->
{ok, Pid} = supervisor:start_child(socketio_listener_sup_sup, [Options]),
{dictionary, Dict} = erlang:process_info(Pid, dictionary),
EventMgr = proplists:get_value(event_manager, Dict),
{ok, Pid, EventMgr}.
41 changes: 41 additions & 0 deletions src/socketio_listener_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-module(socketio_listener_sup).

-behaviour(supervisor).

%% API
-export([start_link/1]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link(Options) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [Options]).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([Options]) ->
HttpPort = proplists:get_value(http_port, Options, 80),
DefaultHttpHandler = proplists:get_value(default_http_handler, Options),
{ok, EventMgr} = gen_event:start_link(),
put(event_manager, EventMgr), %% this is ok because it is a write-once value
{ok, { {one_for_one, 5, 10}, [
{socketio_http, {socketio_http, start_link, [HttpPort,
DefaultHttpHandler,
EventMgr,
self()]},
permanent, 5000, worker, [socketio_http]},
{socketio_client_sup, {socketio_client_sup, start_link, []},
permanent, infinity, supervisor, [socketio_client_sup]}


]} }.

30 changes: 30 additions & 0 deletions src/socketio_listener_sup_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-module(socketio_listener_sup_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
{ok, { {simple_one_for_one, 5, 10}, [
{socketio_listener_sup, {socketio_listener_sup, start_link, []},
permanent, infinity, supervisor, [socketio_listener_sup]}
]} }.

7 changes: 3 additions & 4 deletions src/socketio_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ start_link() ->

init([]) ->
{ok, { {one_for_one, 5, 10}, [
{socketio_http_sup, {socketio_http_sup, start_link, []},
permanent, infinity, supervisor, [socketio_http_sup]},
{socketio_client_sup, {socketio_client_sup, start_link, []},
permanent, infinity, supervisor, [socketio_client_sup]}
{socketio_listener_sup_sup, {socketio_listener_sup_sup, start_link, []},
permanent, infinity, supervisor, [socketio_listener_sup_sup]}

]} }.

4 changes: 2 additions & 2 deletions test/socketio_client_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ socketio_client_websocket_test_() ->
fun () ->
error_logger:delete_report_handler(error_logger_tty_h), %% suppress annoying kernel logger
application:start(socketio),
{ok, Pid} = socketio_http:start(8989, ?MODULE),
EventMgr = socketio_http:event_manager(Pid),
{ok, _Pid, EventMgr} = socketio_listener:start([{http_port, 8989},
{default_http_handler, ?MODULE}]),
ok = gen_event:add_handler(EventMgr, ?MODULE,[self()]),
?cmd("open -a \"Google Chrome\" http://localhost:8989/"), %% FIXME: will only work on OSX
receive
Expand Down

1 comment on commit 43562fd

@yrashk
Copy link
Owner Author

@yrashk yrashk commented on 43562fd Feb 26, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.