Permalink
Browse files

Merge pull request #72 from ferd/master

Allowing multiple handlers through a hack
  • Loading branch information...
2 parents 4f39789 + ad4b319 commit 993b2ec3a308241f0a3e72a8fc7a57bfb2886442 @yrashk committed Mar 10, 2012
Showing with 63 additions and 42 deletions.
  1. +5 −1 demo/demo.erl
  2. +2 −1 demo/demo_ssl.erl
  3. +4 −3 src/socketio.app.src
  4. +3 −4 src/socketio_client.erl
  5. +5 −4 src/socketio_client_sup.erl
  6. +7 −4 src/socketio_http.erl
  7. +37 −25 src/socketio_listener_sup.erl
View
@@ -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).
@@ -12,12 +12,16 @@
main(_) ->
appmon:start(),
application:start(sasl),
+ application:start(gproc),
application:start(misultin),
application:start(socketio),
{ok, Pid} = socketio_listener:start([{http_port, 7878},
{default_http_handler,?MODULE}]),
{ok, Pid} = socketio_listener:start([{http_port, 7878},
{default_http_handler,?MODULE}]),
+ {ok, Pid2} = socketio_listener:start([{http_port, 7879},
+ {default_http_handler,?MODULE}]),
+ true = Pid2 =/= Pid,
EventMgr = socketio_listener:event_manager(Pid),
ok = gen_event:add_handler(EventMgr, ?MODULE,[]),
receive _ -> ok end.
View
@@ -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).
@@ -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},
@@ -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},
@@ -2,7 +2,7 @@
-include_lib("socketio.hrl").
%% API
--export([start_link/5, start/5]).
+-export([start_link/5, start/6]).
-export([event_manager/1, send/2, session_id/1, request/1]).
%%%===================================================================
@@ -19,9 +19,8 @@
start_link(Sup, Module, SessionId, ServerModule, ConnectionReference) ->
Module:start_link(Sup, SessionId, ServerModule, ConnectionReference).
-start(Sup0, Module, SessionId, ServerModule, ConnectionReference) ->
- Children = supervisor:which_children(Sup0),
- {Sup, _, _, _} = lists:keyfind(socketio_client_sup,1, Children),
+start(Sup0, Module, SessionId, ServerModule, ConnectionReference, Port) ->
+ Sup = gproc:lookup_local_name({socketio_client_sup, Port}),
supervisor:start_child(Sup, [Sup0, Module, SessionId, ServerModule, ConnectionReference]).
@@ -3,7 +3,7 @@
-behaviour(supervisor).
%% API
--export([start_link/0]).
+-export([start_link/1]).
%% Supervisor callbacks
-export([init/1]).
@@ -15,14 +15,15 @@
%% API functions
%% ===================================================================
-start_link() ->
- supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+start_link(Port) ->
+ 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]}
View
@@ -18,7 +18,8 @@
sup,
web_server_monitor,
server_module,
- resource
+ resource,
+ port % very dirty hack
}).
%%%===================================================================
@@ -64,7 +65,8 @@ init([ServerModule, Port, Resource, SSL, DefaultHttpHandler, Sup]) ->
sup = Sup,
web_server_monitor = WebServerRef,
server_module = ServerModule,
- resource = Resource
+ resource = Resource,
+ port = Port
}}.
%%--------------------------------------------------------------------
@@ -206,10 +208,11 @@ handle_call({session, generate, ConnectionReference, Transport}, _From, #state{
sup = Sup,
sessions = Sessions,
event_manager = EventManager,
- server_module = ServerModule
+ server_module = ServerModule,
+ port = Port
} = State) ->
UUID = binary_to_list(ossp_uuid:make(v4, text)),
- {ok, Pid} = socketio_client:start(Sup, Transport, UUID, ServerModule, ConnectionReference),
+ {ok, Pid} = socketio_client:start(Sup, Transport, UUID, ServerModule, ConnectionReference, Port),
link(Pid),
ets:insert(Sessions, [{UUID, Pid}, {Pid, UUID}]),
gen_event:notify(EventManager, {client, Pid}),
@@ -16,36 +16,48 @@
%% ===================================================================
start_link(Options) ->
- supervisor:start_link({local, ?MODULE}, ?MODULE, [Options]).
+ %% This is a terrible, sad hack to avoid duplicates
+ 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]},
-
- {socketio_client_sup, {socketio_client_sup, start_link, []},
- 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 993b2ec

Please sign in to comment.