1010
1111-record (state , {lsock = undefined ,
1212 pending = queue :new (),
13- count = 0 }).
13+ count = 0 ,
14+ map = undefined }).
1415
1516-record (request , {sock = undefined ,
1617 info = undefined ,
@@ -43,12 +44,14 @@ asset_freed() ->
4344% % {stop, Reason}
4445% % Description: Initiates the server
4546% %--------------------------------------------------------------------
46- init ([Port ]) ->
47+ init ([Port , Configs ]) ->
4748 process_flag (trap_exit , true ),
4849 error_logger :info_msg (" ~p starting~n " , [? MODULE ]),
4950 {ok , LSock } = try_listen (Port , 500 ),
5051 spawn (fun () -> loop (LSock ) end ),
51- {ok , # state {lsock = LSock }}.
52+ Map = init_map (Configs ),
53+ io :format (" pidmap = ~p~n " , [Map ]),
54+ {ok , # state {lsock = LSock , map = Map }}.
5255
5356% %--------------------------------------------------------------------
5457% % Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
@@ -73,19 +76,11 @@ handle_cast({process, Sock}, State) ->
7376 State2 = receive_term (Request , State ),
7477 {noreply , State2 };
7578handle_cast ({asset_freed }, State ) ->
76- case queue :is_empty (State # state .pending ) of
77- false ->
78- case asset_pool :lease () of
79- {ok , Asset } ->
80- {{value , Request }, Pending2 } = queue :out (State # state .pending ),
81- % io:format("d", []),
82- spawn (fun () -> process_now (Request , Asset ) end ),
83- {noreply , State # state {pending = Pending2 }};
84- empty ->
85- % io:format(".", []),
86- {noreply , State }
87- end ;
88- true ->
79+ case queue :out (State # state .pending ) of
80+ {{value , Request }, Pending2 } ->
81+ State2 = process_request (Request , State # state {pending = Pending2 }),
82+ {noreply , State2 };
83+ {empty , _Pending } ->
8984 {noreply , State }
9085 end ;
9186handle_cast (_Msg , State ) -> {noreply , State }.
@@ -101,6 +96,14 @@ code_change(_OldVersion, State, _Extra) -> {ok, State}.
10196% % Internal
10297% %====================================================================
10398
99+ extract_mapping (Config ) ->
100+ Pid = proplists :get_value (pid , Config ),
101+ Mods = proplists :get_value (modules , Config ),
102+ lists :map (fun (X ) -> {X , Pid } end , Mods ).
103+
104+ init_map (Configs ) ->
105+ lists :foldl (fun (X , Acc ) -> Acc ++ extract_mapping (X ) end , [], Configs ).
106+
104107try_listen (Port , 0 ) ->
105108 error_logger :error_msg (" Could not listen on port ~p~n " , [Port ]),
106109 {error , " Could not listen on port" };
@@ -166,45 +169,64 @@ receive_term(Request, State) ->
166169
167170process_request (Request , State ) ->
168171 ActionTerm = binary_to_term (Request # request .action ),
172+ close_if_cast (ActionTerm , Request ),
173+ {_Type , Mod , _Fun , _Args } = ActionTerm ,
174+ Pid = proplists :get_value (Mod , State # state .map ),
175+ case Pid of
176+ undefined ->
177+ logger :debug (" Dispatching to native module~n " , []),
178+ process_native_request (Request , State );
179+ ValidPid ->
180+ logger :debug (" Found extern pid ~p~n " , [ValidPid ]),
181+ process_extern_request (ValidPid , Request , State )
182+ end .
183+
184+ close_if_cast (ActionTerm , Request ) ->
169185 case ActionTerm of
170186 {cast , _Mod , _Fun , _Args } ->
171187 Sock = Request # request .sock ,
172188 gen_tcp :send (Sock , term_to_binary ({noreply })),
173189 ok = gen_tcp :close (Sock ),
174- logger :debug (" Closing cast.~n " , []);
190+ logger :debug (" Closed cast.~n " , []);
175191 _Any ->
176192 ok
177- end ,
193+ end .
194+
195+ process_native_request (_Request , State ) ->
196+ State .
197+
198+ process_extern_request (Pid , Request , State ) ->
178199 case queue :is_empty (State # state .pending ) of
179200 false ->
201+ logger :debug (" Pre queueing request for pool ~p~n " , [Pid ]),
180202 Pending2 = queue :in (Request , State # state .pending ),
181- % io:format("Q", []),
182203 State # state {pending = Pending2 };
183204 true ->
184- try_process_now (Request , State )
205+ try_process_now (Pid , Request , State )
185206 end .
186207
187- try_process_now (Request , State ) ->
208+ try_process_now (Pid , Request , State ) ->
188209 Count = State # state .count ,
189210 State2 = State # state {count = Count + 1 },
190- case asset_pool :lease () of
211+ logger :debug (" Count = ~p~n " , [Count + 1 ]),
212+ case asset_pool :lease (Pid ) of
191213 {ok , Asset } ->
192- % io:format("i ", []),
193- spawn (fun () -> process_now (Request , Asset ) end ),
214+ logger : debug ( " Leased asset for pool ~p~n " , [Pid ]),
215+ spawn (fun () -> process_now (Pid , Request , Asset ) end ),
194216 State2 ;
195217 empty ->
196- % io:format("q ", []),
218+ logger : debug ( " Post queueing request for pool ~p~n " , [Pid ]),
197219 Pending2 = queue :in (Request , State # state .pending ),
198220 State2 # state {pending = Pending2 }
199221 end .
200222
201- process_now (Request , Asset ) ->
223+ process_now (Pid , Request , Asset ) ->
202224 try unsafe_process_now (Request , Asset ) of
203225 _AnyResponse -> ok
204226 catch
205227 _AnyError -> ok
206228 after
207- asset_pool :return (Asset ),
229+ asset_pool :return (Pid , Asset ),
208230 ernie_server :asset_freed (),
209231 gen_tcp :close (Request # request .sock )
210232 end .
0 commit comments