[erlang-questions] Generic server and selective receives

ludovic <>
Mon Feb 17 00:55:19 CET 2014


I would like some help to implement a message-buffer in erlang. This
process can be sent messages, and stores them in mailbox. A buffer can
have one client, or can have no client. When there's no client, I
would prevent the process to do anything but accept a new client or
timeout after a small amount of time. So I have two sets of receive

Now, I would like to implement this as a gen_server. But I don't know
how. The Idea is to timeout quickly if no message of the expected
clauses-set comes.

* I thought of a gen_fsm but it will crash if it's sent a message when
in the "no client" state, right ? (Plus i may want to handle more
receive clauses in the "no client" part)
* I thougt about time calculations but it's seems too complicated for
no reason ; I prefer use the simple gen_* timeouts.
* I could keep my current implementation and adapt the module to fit
in a supervision tree but I believe I willll miss common OTP features.

So, I hope this is a common pattern and that someone will tell me a
good solution.

Thank you.

-- lud

This is my current implementation (but not the actual code).

     loop(#state{client_pid=undefined}) ->
             {set_client,Pid} ->
         after 10000 ->

     loop(State=#state{client_pid=Client,mailbox=Mailbox}) ->
             {set_client,Pid} ->
                 %% here we can change the client or set it to undefined
             {message,M} ->
                 Client ! you_have_messages,
             %% A handful of other clauses
         after way more time than 1000 ->
             io:fwrite("Terminating too\n")

Now i'll pray for this mail to be seen in the current huge amount of 
emails (and very intersting by the way) on the list.

More information about the erlang-questions mailing list