Skip to content
Browse files

Introduced socketio_listener and therefore refactored supervision tree.

  • Loading branch information...
1 parent f8dd0df commit 43562fd54718c440908a2092fd942c369dfd4fae @yrashk committed
View
4 demo/demo.erl
@@ -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.
View
8 src/socketio_client.erl
@@ -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
@@ -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}).
View
32 src/socketio_http.erl
@@ -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,
@@ -15,7 +14,8 @@
-record(state, {
default_http_handler,
sessions,
- event_manager
+ event_manager,
+ sup
}).
%%%===================================================================
@@ -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
@@ -53,7 +47,7 @@ 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},
@@ -61,11 +55,11 @@ init([Port, DefaultHttpHandler]) ->
{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
}}.
%%--------------------------------------------------------------------
@@ -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}),
View
8 src/socketio_listener.erl
@@ -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}.
View
41 src/socketio_listener_sup.erl
@@ -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]}
+
+
+ ]} }.
+
View
30 src/socketio_listener_sup_sup.erl
@@ -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]}
+ ]} }.
+
View
7 src/socketio_sup.erl
@@ -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]}
+
]} }.
View
4 test/socketio_client_tests.erl
@@ -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

1 comment on commit 43562fd

@yrashk
Owner

Please sign in to comment.
Something went wrong with that request. Please try again.