Permalink
Browse files

fixed supervision to allow multiple connections

  • Loading branch information...
1 parent f1ff6b8 commit c3dc452e8cf9972d7e17369190306d02ac417309 @pavlobaron committed Apr 15, 2012
@@ -21,3 +21,6 @@
%% @copyright 2012 Pavlo Baron
-define(SOCK_OPTS, [binary, {active, once}, {packet, 0}, {reuseaddr, true}]).
+
+-define(CONTROL_MSG, {control}).
+-define(CONTROLLING_PROCESS_MSG(S, O), {controlling_process, S, O}).
@@ -17,9 +17,7 @@
%%
%% @author Pavlo Baron <pb at pbit dot org>
-%% @doc Global state records
+%% @doc State records
%% @copyright 2012 Pavlo Baron
-record(worker_state, {sock, request_id=0, rest, lastError=[]}).
-
--record(server_state, {owner :: pid(), sock}).
View
@@ -34,22 +34,23 @@ start_link(IpAddr, Port) ->
gen_nb_server:start_link(?MODULE, IpAddr, Port, []).
init(_Args) ->
- {ok, #server_state{owner=self()}}.
+ {ok, ok}.
new_connection(Sock, State) ->
- Owner = State#server_state.owner,
+ error_logger:info_msg("New connection: ~p, ~p~n", [self(), Sock]),
- error_logger:info_msg("New connection: ~p, ~p, ~p~n", [Owner, Sock, State]),
-
- NewOwner = riak_mongo_worker_sup:new_worker(Sock, Owner),
- NewState = #server_state{owner=NewOwner, sock=Sock},
- {ok, NewState}.
+ riak_mongo_worker_sup:new_worker(Sock, self()),
+ {ok, State}.
sock_opts() -> ?SOCK_OPTS.
-handle_info({controlling_process, Sock, Pid}, State) ->
- riak_mongo_sock:give_control(Sock, Pid),
+handle_info(?CONTROLLING_PROCESS_MSG(Sock, NewOwner), State) ->
+ error_logger:info_msg("Handing over control from to ~p on sock ~p~n", [NewOwner, Sock]),
+
+ gen_tcp:controlling_process(Sock, NewOwner),
+ NewOwner ! ?CONTROL_MSG,
{noreply, State};
+
handle_info(Msg, State) ->
error_logger:info_msg("unknown message in worker callback: ~p~n", [Msg]),
{noreply, State}.
View
@@ -1,32 +0,0 @@
-%%
-%% This file is part of riak_mongo
-%%
-%% Copyright (c) 2012 by Pavlo Baron (pb at pbit dot org)
-%%
-%% Licensed under the Apache License, Version 2.0 (the "License");
-%% you may not use this file except in compliance with the License.
-%% You may obtain a copy of the License at
-%%
-%% http://www.apache.org/licenses/LICENSE-2.0
-%%
-%% Unless required by applicable law or agreed to in writing, software
-%% distributed under the License is distributed on an "AS IS" BASIS,
-%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-%% See the License for the specific language governing permissions and
-%% limitations under the License.
-%%
-
-%% @author Pavlo Baron <pb at pbit dot org>
-%% @doc Some socket stuff
-%% @copyright 2012 Pavlo Baron
-
--module(riak_mongo_sock).
-
--export([change_control/3, give_control/2]).
-
-change_control(Sock, OldOwner, NewOwner) ->
- OldOwner ! {controlling_process, Sock, NewOwner}.
-
-give_control(Sock, NewOwner) ->
- gen_tcp:controlling_process(Sock, NewOwner),
- NewOwner ! {control, Sock}.
View
@@ -30,11 +30,13 @@
-include("riak_mongo_state.hrl").
-include("riak_mongo_sock.hrl").
-start_link(Sock, OldOwner) ->
- gen_server:start_link(?MODULE, [Sock, OldOwner], []).
+start_link(Sock, Owner) ->
+ gen_server:start_link(?MODULE, [Sock, Owner], []).
-init([Sock, OldOwner]) ->
- riak_mongo_sock:change_control(Sock, OldOwner, self()),
+init([Sock, Owner]) ->
+ error_logger:info_msg("Requested to hand over control on sock ~p, ~p~n", [Sock, Owner]),
+
+ Owner ! ?CONTROLLING_PROCESS_MSG(Sock, self()),
InitBin = <<>>,
{ok, #worker_state{sock=Sock, rest=InitBin}}.
@@ -50,16 +52,10 @@ handle_info({tcp, Sock, Data}, State) ->
handle_info({tcp_closed, _Sock}, _) -> {reply, ok};
-handle_info({controlling_process, Sock, Pid}, State) ->
- error_logger:info_msg("Giving away control (worker): ~p, ~p~n", [Sock, Pid]),
-
- riak_mongo_sock:give_control(Sock, Pid),
- {noreply, State};
+handle_info(?CONTROL_MSG, State) ->
+ error_logger:info_msg("Having control: ~p~n", [State#worker_state.sock]),
-handle_info({control, Sock}, State) ->
- error_logger:info_msg("Having control: ~p~n", [Sock]),
-
- inet:setopts(Sock, ?SOCK_OPTS),
+ inet:setopts(State#worker_state.sock, ?SOCK_OPTS),
{noreply, State};
handle_info(Msg, State) ->
@@ -35,4 +35,4 @@ init(_Args) ->
{ok, {{simple_one_for_one, 0, 1}, [WorkerSpec]}}.
new_worker(Sock, Owner) ->
- supervisor:start_child(riak_mongo_worker_sup, [Sock, Owner]).
+ supervisor:start_child(?MODULE, [Sock, Owner]).

0 comments on commit c3dc452

Please sign in to comment.