[erlang-questions] a question on misultin

3 views
Skip to first unread message

gnoblin

unread,
Aug 17, 2010, 5:56:23 AM8/17/10
to erlang-q...@erlang.org
Hello,

I've decided to use misultin as a webserver for my client-server
application, and I am trying to do the following:

Case 1:

% process1
handle_http(Req) ->
% get params
Args = Req:parse_qs(),

case proplists:get_value("value", Args) of
%% we got an empty request?
undefined ->
ok;

Input ->
io:format("got a request~p~n",
[Input]),
Req:ok([?CONTENT_TYPE], "<html>"++"Ok"++"</html>")

Result: I see the response in my browser.

Case 2:
If instead of Req:ok([?CONTENT_TYPE], "<html>"++"Ok"++"</html>") I
send a message to process2, which processes the request and sends the
response via a message to process1,

and it is received in process1 here:

loop() ->
receive
{respond, Req, Json} ->
io:format("Responding back! ~n"),
%% falls somewhere here
%% it seems i can't do it like this...
Req:ok([?CONTENT_TYPE], "<html>"++Json++"</html>"),
loop();
Other ->
io:format("says: I've got some strange message here: ~p~n",
[Other])
end.

Result: I don't see the response.

---
The question is: if I can't do it like that - what is the correct way
to do it? I'd like to get a string via http from client, send it to
other processes, get the response back to process with misultin and
send it back to the user.

Thanks

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questio...@erlang.org

Roberto Ostinelli

unread,
Aug 17, 2010, 6:04:28 AM8/17/10
to gnoblin, erlang-q...@erlang.org
hello goblin,

for every request a process is spawned to manage its response [aka the
response controlling process], and it's this process that calls the
handle function, which MUST return a response. this is a precise
server design, since every single request/response entity has a
precise lifetime which needs to be controlled server-wise, and this
lifetime corresponds to the lifetime of the controlling process.

if for whatever reason you need to build a response from an external
process, you may do so by 'locking' the response controlling process
and sending to it the response so that it can return it. a working
example follows.


-module(misultin_hello_world).
-export([start/1, stop/0]).
-export([external_proc/1]).

% start misultin http server
start(Port) ->
misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]).

% stop misultin
stop() ->
misultin:stop().

% callback on request received
handle_http(Req) ->
% spawn external process
Pid = spawn(?MODULE, external_proc, [self()]),
% send req
Pid ! Req,
% wait
receive
Response -> Response
end.

external_proc(ControllerPid) ->
receive
Req ->
% build response
Response = Req:ok("Hello World."),
% send it to controller
ControllerPid ! Response
end.

cheers,

r.

2010/8/17 gnoblin <gno...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages