Browse files

Merge pull request #177 from mihawk/feature/fix_crash_unknow_websocke…

…t_service

fix crash unknow websocket services
  • Loading branch information...
2 parents 6560982 + 86c61db commit d80a55664f5e6a1ecce2ac5e5008875f09ccfce0 @evanmiller evanmiller committed Dec 27, 2012
Showing with 31 additions and 14 deletions.
  1. +31 −14 src/boss/boss_websocket_router.erl
View
45 src/boss/boss_websocket_router.erl
@@ -128,7 +128,12 @@ init([]) ->
handle_call({get_service, ServiceUrl}, _From, State) ->
#state{services=Services} = State,
- Reply = dict:fetch(ServiceUrl, Services),
+ Reply = case dict:find(ServiceUrl, Services) of
+ {ok, Val} ->
+ {ok, Val};
+ error ->
+ {error, service_notfound}
+ end,
{reply, Reply, State};
handle_call({get_all_service}, _From, State) ->
@@ -169,25 +174,37 @@ handle_call(_Request, _From, State) ->
handle_cast({join_service, ServiceUrl, WebSocketId, SessionId}, State) ->
#state{consumers=Consumers, services=Services, nb_consumer=NbConsumer} = State,
- ServiceId = dict:fetch(ServiceUrl, Services),
- boss_service_worker:join(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId),
- NewConsumers = dict:store(WebSocketId, [ServiceId, SessionId], Consumers),
- NewState = #state{consumers=NewConsumers, services=Services,nb_consumer=NbConsumer+1},
- {noreply, NewState};
+ case dict:find(ServiceUrl, Services) of
+ {ok, ServiceId} ->
+ boss_service_worker:join(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId),
+ NewConsumers = dict:store(WebSocketId, [ServiceId, SessionId], Consumers),
+ NewState = #state{consumers=NewConsumers, services=Services,nb_consumer=NbConsumer+1},
+ {noreply, NewState};
+ error ->
+ {noreply, State}
+ end;
handle_cast({incoming_msg, ServiceUrl, WebSocketId, SessionId, Msg}, State) ->
#state{services=Services} = State,
- ServiceId = dict:fetch(ServiceUrl, Services),
- boss_service_worker:incoming(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId, Msg),
- {noreply, State};
+ case dict:find(ServiceUrl, Services) of
+ {ok, ServiceId} ->
+ boss_service_worker:incoming(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId, Msg),
+ {noreply, State};
+ error ->
+ {noreply, State}
+ end;
handle_cast({terminate_service, ServiceUrl, WebSocketId, SessionId}, State) ->
#state{consumers=Consumers, services=Services, nb_consumer=NbConsumer} = State,
- ServiceId = dict:fetch(ServiceUrl, Services),
- boss_service_worker:close(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId),
- NewConsumers = dict:erase(WebSocketId, Consumers),
- NewState = #state{consumers=NewConsumers, services=Services, nb_consumer=NbConsumer-1},
- {noreply, NewState};
+ case dict:find(ServiceUrl, Services) of
+ {ok, ServiceId} ->
+ boss_service_worker:close(ServiceId, binary_to_list(ServiceUrl), WebSocketId, SessionId),
+ NewConsumers = dict:erase(WebSocketId, Consumers),
+ NewState = #state{consumers=NewConsumers, services=Services, nb_consumer=NbConsumer-1},
+ {noreply, NewState};
+ error ->
+ {noreply, State}
+ end;
handle_cast(_Msg, State) ->
{noreply, State}.

0 comments on commit d80a556

Please sign in to comment.