[erlang-questions] "calling" a gen_fsm

Vance Shipley vances@REDACTED
Wed Sep 23 06:30:43 CEST 2015


On Wed, Sep 23, 2015 at 12:32 AM, Roger Lipscombe
<roger@REDACTED> wrote:
> Both Theepan and Vance are correct in that this is a useful way to
> build this kind of thing. In fact, there's a good example (using a
> gen_server, rather than a gen_fsm) here:
> https://erlangcentral.org/wiki/index.php?title=Building_Non_Blocking_Erlang_apps

What I don't like about that example is that it's not purely
functional.  I would prefer:

     handle_call({request, Tr_id, Req}, From, #state{tr = Tr, socket =
Socket} = State) ->
          Data = encode(Tr_id, Req),
          gen_tcp:send(Socket, Data),
          NewState = State#state{tr = dict:store(Tr_id, From, Tr)},
          {noreply, NewState}.

     handle_info({tcp, _Socket, Data}, #state{tr = Tr} = State) ->
          {Tr_id, Result} = decode(Data),
          From = dict:fetch(Tr_id, Tr),
          gen_server:reply(From, Result),
          NewState = State#state{tr = dict:erase(From, Tr)},
          {noreply, NewState}.



-- 
     -Vance



More information about the erlang-questions mailing list