Skip to content

Commit

Permalink
Cleaning up multiple manager supports
Browse files Browse the repository at this point in the history
This now works using gproc instead of list_to_atom.
This is done using gproc and adds a dependency,
warranting a version bump from 1 to 1.1
  • Loading branch information
ferd committed Mar 10, 2012
1 parent a7a1a7c commit ad4b319
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
3 changes: 2 additions & 1 deletion demo/demo.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env escript
%%! -pa ../ebin ../deps/misultin/ebin ../deps/ossp_uuid/ebin ../deps/jsx/ebin
%%! -pa ../ebin ../deps/misultin/ebin ../deps/ossp_uuid/ebin ../deps/jsx/ebin ../deps/gproc/ebin/
-mode(compile).
-include_lib("../include/socketio.hrl").
-compile(export_all).
Expand All @@ -12,6 +12,7 @@
main(_) ->
appmon:start(),
application:start(sasl),
application:start(gproc),
application:start(misultin),
application:start(socketio),
{ok, Pid} = socketio_listener:start([{http_port, 7878},
Expand Down
3 changes: 2 additions & 1 deletion demo/demo_ssl.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env escript
%%! -pa ../ebin ../deps/misultin/ebin ../deps/ossp_uuid/ebin ../deps/jsx/ebin
%%! -pa ../ebin ../deps/misultin/ebin ../deps/ossp_uuid/ebin ../deps/jsx/ebin ../deps/gproc/ebin
-mode(compile).
-include_lib("../include/socketio.hrl").
-compile(export_all).
Expand All @@ -12,6 +12,7 @@
main(_) ->
appmon:start(),
application:start(sasl),
application:start(gproc),
application:start(misultin),
application:start(socketio),
{ok, Pid} = socketio_listener:start([{http_port, 7878},
Expand Down
7 changes: 4 additions & 3 deletions src/socketio.app.src
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{application, socketio,
[
{description, ""},
{vsn, "1"},
{vsn, "1.1"},
{registered, []},
{agner, [{requires, ["misultin", "ossp_uuid",{"jsx","0.9.0"},"proper","ex_uri"]}]},
{agner, [{requires, ["misultin", "ossp_uuid",{"jsx","0.9.0"},"proper","ex_uri", "gproc"]}]},
{applications, [
kernel,
stdlib,
misultin
gproc,
misultin
]},
{mod, { socketio_app, []}},
{env, [{heartbeat_interval, 10000},
Expand Down
4 changes: 1 addition & 3 deletions src/socketio_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ start_link(Sup, Module, SessionId, ServerModule, ConnectionReference) ->
Module:start_link(Sup, SessionId, ServerModule, ConnectionReference).

start(Sup0, Module, SessionId, ServerModule, ConnectionReference, Port) ->
Children = supervisor:which_children(Sup0),
Name = list_to_atom(atom_to_list(socketio_client_sup) ++ "_" ++ integer_to_list(Port)),
{Sup, _, _, _} = lists:keyfind(Name ,1, Children),
Sup = gproc:lookup_local_name({socketio_client_sup, Port}),
supervisor:start_child(Sup, [Sup0, Module, SessionId, ServerModule, ConnectionReference]).


Expand Down
6 changes: 3 additions & 3 deletions src/socketio_client_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
%% ===================================================================

start_link(Port) ->
Name = list_to_atom(atom_to_list(?MODULE)++"_"++integer_to_list(Port)),
supervisor:start_link({local, Name}, ?MODULE, []).
supervisor:start_link(?MODULE, [Port]).

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

init([]) ->
init([Port]) ->
gproc:add_local_name({?MODULE, Port}),
{ok, { {simple_one_for_one, 5, 10}, [
{socketio_client, {socketio_client, start_link, []},
transient, 5000, worker, [socketio_client]}
Expand Down
63 changes: 36 additions & 27 deletions src/socketio_listener_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,47 @@

start_link(Options) ->
%% This is a terrible, sad hack to avoid duplicates
Port = proplists:get_value(http_port, Options),
Name = list_to_atom(atom_to_list(?MODULE)++"_"++integer_to_list(Port)),
supervisor:start_link({local, Name}, ?MODULE, [Options]).
case supervisor:start_link(?MODULE, [Options]) of
{ok, Pid} ->
{ok, Pid};
ignore ->
{ok, gproc:lookup_local_name({?MODULE,proplists:get_value(http_port, Options, 80)})}
end.

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

init([Options]) ->
ServerModule = proplists:get_value(server, Options, socketio_http_misultin),
HttpPort = proplists:get_value(http_port, Options, 80),
DefaultHttpHandler = proplists:get_value(default_http_handler, Options),
Resource = lists:reverse(string:tokens(proplists:get_value(resource, Options, "socket.io"),"/")),
SSL = proplists:get_value(ssl, Options),
Origins = proplists:get_value(origins,Options,[{"*", "*"}]),
{ok, { {one_for_one, 5, 10}, [
{socketio_listener_event_manager, {gen_event, start_link, []},
permanent, 5000, worker, [gen_event]},

{socketio_listener, {socketio_listener, start_link, [self(), Origins]},
permanent, 5000, worker, [socketio_listener]},

{socketio_http, {socketio_http, start_link, [ServerModule,
HttpPort,
Resource,
SSL,
DefaultHttpHandler,
self()]},
permanent, 5000, worker, [socketio_http]},

{list_to_atom("socketio_client_sup_" ++ integer_to_list(HttpPort)), {socketio_client_sup, start_link, [HttpPort]},
permanent, infinity, supervisor, [socketio_client_sup]}

]} }.
try gproc:add_local_name({?MODULE, HttpPort}) of
_ ->
ServerModule = proplists:get_value(server, Options, socketio_http_misultin),
DefaultHttpHandler = proplists:get_value(default_http_handler, Options),
Resource = lists:reverse(string:tokens(proplists:get_value(resource, Options, "socket.io"),"/")),
SSL = proplists:get_value(ssl, Options),
Origins = proplists:get_value(origins,Options,[{"*", "*"}]),
{ok, { {one_for_one, 5, 10}, [
{socketio_listener_event_manager, {gen_event, start_link, []},
permanent, 5000, worker, [gen_event]},

{socketio_listener, {socketio_listener, start_link, [self(), Origins]},
permanent, 5000, worker, [socketio_listener]},

{socketio_http, {socketio_http, start_link, [ServerModule,
HttpPort,
Resource,
SSL,
DefaultHttpHandler,
self()]},
permanent, 5000, worker, [socketio_http]},

{{socketio_client_sup,HttpPort}, {socketio_client_sup, start_link, [HttpPort]},
permanent, infinity, supervisor, [socketio_client_sup]}

]} }
catch
error:badarg ->
ignore
end.

0 comments on commit ad4b319

Please sign in to comment.