[erlang-questions] Erlang OTP design principle

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Mon Jun 11 11:18:11 CEST 2007


If you want to be able to handle messages while
waiting for a TCP message, you may want to use
inet:setopts(Sock, [{active, once}]), and then
wait for the packet in a normal receive clause.

If the waiting is done in e.g. a gen_server, you
get the handling of system messages for free.
Otherwise, you need to read the OTP Design Principles
document, 

http://www.erlang.org/doc/doc-5.5.4/doc/design_principles/part_frame.htm
l

chapter 6.2 "Special Processes", on how to handle
system messages on your own.

(or use something like plain_fsm in Jungerl, if 
you really want to write textbook receive clauses.)

BR,
Ulf W

> -----Original Message-----
> From: erlang-questions-bounces@REDACTED 
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of Eranga Udesh
> Sent: den 11 juni 2007 07:08
> To: erlang-questions@REDACTED
> Subject: [erlang-questions] Erlang OTP design principle
> 
> Hi,
> 
> What's the best way to make do_recv/2 function below, OTP 
> compliant? I.e. it should receive messages from other 
> processes, handle code update requests, terminate, etc.
> 
> Why gen_tcp, gen_udp, etc., are written as behaviors like 
> gen_server, so that in a handle_*, it will receive incoming 
> messages, etc?
> 
> Thanks,
> - Eranga
> 
> 
> 
> 
> server() ->
>     {ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0}, 
>                                         {active, false}]),
>     {ok, Sock} = gen_tcp:accept(LSock),
>     {ok, Bin} = do_recv(Sock, []),
>     ok = gen_tcp:close(Sock),
>     Bin.
> 
> do_recv(Sock, Bs) ->
>     case gen_tcp:recv(Sock, 0) of
>         {ok, B} ->
>             do_recv(Sock, [Bs, B]);
>         {error, closed} ->
>             {ok, list_to_binary(Bs)}
>     end.
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list