Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix bug

  • Loading branch information...
commit bd1e55ecd06188281c225398279df3052a396ffe 1 parent 6b5cced
@mihawk mihawk authored
View
6 src/boss/boss_mochicow_handler.erl
@@ -22,7 +22,7 @@ terminate(_Req, _State) ->
websocket_init(_Any, Req, _Opts) ->
SessionKey = boss_env:get_env(session_key, "_boss_session"),
- {ServiceName, _Req1} = cowboy_http_req:path(Req),
+ {[ServiceName], _Req1} = cowboy_http_req:path(Req),
{SessionId, _Req2} = cowboy_http_req:cookie(list_to_binary(SessionKey), Req),
WebsocketId = self(),
State= #state{websocket_id=WebsocketId,
@@ -32,7 +32,7 @@ websocket_init(_Any, Req, _Opts) ->
{ok, Req, State, hibernate}.
websocket_handle({text, Msg}, Req, State) ->
- error_logger:info_msg("State:~p~nincoming:~p~n",[State, Msg]),
+ %error_logger:info_msg("State:~p~nincoming:~p~n",[State, Msg]),
#state{websocket_id=WebsocketId,
session_id=SessionId,
service_name=ServiceName } = State,
@@ -53,6 +53,6 @@ websocket_terminate(_Reason, _Req, State) ->
#state{websocket_id=WebsocketId,
session_id=SessionId,
service_name=ServiceName } = State,
- error_logger:info_msg("websocket terminate~nState=~p~n", [State]),
+ %error_logger:info_msg("websocket terminate~nState=~p~n", [State]),
boss_service:terminate(ServiceName, WebsocketId, SessionId),
ok.
View
63 src/boss/boss_service.erl
@@ -19,7 +19,9 @@
unregister/2,
join/3,
terminate/3,
- incoming/4
+ incoming/4,
+ service/1,
+ services/0
]).
%% gen_server callbacks
@@ -73,6 +75,12 @@ register(ServiceName, ServiceId) ->
unregister(ServiceName, ServiceId) ->
gen_server:call(?SERVER, {unregister_service, ServiceName, ServiceId}).
+service(ServiceName) ->
+ gen_server:call(?SERVER, {get_service, ServiceName}).
+services() ->
+ gen_server:call(?SERVER, {get_all_service}).
+
+
%async
join(ServiceName, WebSocketId, SessionId) ->
gen_server:cast(?SERVER, {join_service, ServiceName, WebSocketId, SessionId}).
@@ -82,8 +90,10 @@ terminate(ServiceName, WebSocketId, SessionId) ->
incoming(ServiceName, WebSocketId, SessionId, Message) ->
gen_server:cast(?SERVER, {incoming_msg, ServiceName, WebSocketId, SessionId, Message}).
-
+%% ??
+%% broadcast/1 -> broadcast a message to all consummer
+%% broadcast/2 -> broadcast a message to all consummer for a particular service
%%%===================================================================
%%% gen_server callbacks
@@ -111,7 +121,7 @@ init([]) ->
mnesia:create_table(boss_consummers, [{attributes,
record_info(fields, boss_consummers)},
{type, bag},
- {disc_copies, [node()]}])
+ {ram_copies, [node()]}])
end,
try
mnesia:table_info(boss_services, type)
@@ -119,7 +129,7 @@ init([]) ->
exit: _ ->
mnesia:create_table(boss_services, [{attributes, record_info(fields, boss_services)},
{type, bag},
- {disc_copies, [node()]}])
+ {ram_copies, [node()]}])
end,
{ok, #state{nb_consummer = 0, nb_service = 0}}.
@@ -137,6 +147,18 @@ init([]) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
+%% get_service(ServiceName) ->
+%% gen_server:call(?SERVER, {get_service, ServiceName}).
+%% get_services() ->
+%% gen_server:call(?SERVER, get_all_service).
+handle_call({get_service, ServiceName}, _From, State) ->
+ Reply = get_service(ServiceName),
+ {reply, Reply, State};
+
+handle_call({get_all_service}, _From, State) ->
+ Reply = get_all_service(),
+ {reply, Reply, State};
+
handle_call({register_service, ServiceName, ServiceId}, _From, State) ->
Reply = register_service(ServiceName, ServiceId),
@@ -180,10 +202,11 @@ handle_call(_Request, _From, State) ->
handle_cast({join_service, ServiceName, WebSocketId, SessionId}, State) ->
case get_service(ServiceName) of
[{boss_services, _, ServiceId, _}] ->
-
- ServiceId ! {join_service, WebSocketId, SessionId};
+ %% error_logger:info_msg("{~p,~p} join Service (~p)~nMailbox:~p~n",
+ %% [WebSocketId, SessionId, ServiceName, ServiceId]),
+ ServiceId ! {join_service, ServiceName, WebSocketId, SessionId};
Unknow ->
- error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
+ %% error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
Unknow
end,
register_consummer(ServiceName, WebSocketId, SessionId),
@@ -192,9 +215,11 @@ handle_cast({join_service, ServiceName, WebSocketId, SessionId}, State) ->
handle_cast({incoming_msg, ServiceName, WebSocketId, SessionId, Msg}, State) ->
case get_service(ServiceName) of
[{boss_services, _, ServiceId, _}] ->
- ServiceId ! {incoming_msg, WebSocketId, SessionId, Msg};
+ %% error_logger:info_msg("From {~p,~p} incoming_msg to (~p)~nMailbox:~p~n",
+ %% [WebSocketId, SessionId, ServiceName, ServiceId]),
+ ServiceId ! {incoming_msg, ServiceName, WebSocketId, SessionId, Msg};
Unknow ->
- error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
+ %% error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
Unknow
end,
{noreply, State};
@@ -202,9 +227,11 @@ handle_cast({incoming_msg, ServiceName, WebSocketId, SessionId, Msg}, State) ->
handle_cast({terminate, ServiceName, WebSocketId, SessionId}, State) ->
case get_service(ServiceName) of
[{boss_services, _, ServiceId, _}] ->
+ %% error_logger:info_msg("From {~p,~p} incoming_msg to (~p)~nMailBox:~p",
+ %% [WebSocketId, SessionId, ServiceName, ServiceId]),
ServiceId ! {terminate_service, WebSocketId, SessionId};
Unknow ->
- error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
+ %% error_logger:info_msg("unknow Service (~p)~n", [ServiceName]),
Unknow
end,
@@ -280,12 +307,26 @@ get_service(ServiceName) ->
Query = qlc:q([M || M <- mnesia:table(boss_services),
M#boss_services.service_name =:= ServiceName
]),
- Results = qlc:e(Query),
+ Order = fun(A,B) ->
+ A#boss_services.created_on > B#boss_services.created_on
+ end,
+ Results = qlc:e(qlc:sort(Query, {order, Order})),
+ %Results = qlc:e(Query),
Results
end,
{atomic, Service} = mnesia:transaction(F),
Service.
+get_all_service() ->
+ F = fun() ->
+ Query = qlc:q([M || M <- mnesia:table(boss_services)
+ ]),
+ Results = qlc:e(Query),
+ Results
+ end,
+ {atomic, Services} = mnesia:transaction(F),
+ Services.
+
register_consummer(ServiceName, WebsocketId, SessionId) ->
F = fun() ->
View
11 src/boss/boss_web_controller.erl
@@ -130,16 +130,7 @@ init(Config) ->
cowboy ->
%SessionKey = boss_env:get_env(session_key, "_boss_session"),
boss_service_sup:start_link(),
- Dispatch = [
- {'_', [
- {'_',
- boss_mochicow_handler,
- [{loop, {boss_mochicow_handler, loop}}
- %,{session_key, SessionKey}
- ]
- }]
- }
- ],
+ Dispatch = [{'_', [{'_', boss_mochicow_handler, [{loop, {boss_mochicow_handler, loop}}]}]}],
error_logger:info_msg("Starting cowboy... on ~p~n", [MasterNode]),
application:start(cowboy),
HttpPort = boss_env:get_env(port, 8001),
Please sign in to comment.
Something went wrong with that request. Please try again.