[erlang-questions] "calling" a gen_fsm

Theepan vasdeveloper@REDACTED
Tue Sep 22 16:47:49 CEST 2015


Just use {next_state,NextStateName,NewStateData} instead of
{reply,Reply,NextStateName,NewStateData} and keep the PID of the calling
process in the NewStateData. This helps you in deferring the response. When
the data comes form the service, you can send the response back with
gen_fsm:reply(PID, Reply).

Theepan



On Tue, Sep 22, 2015 at 7:33 PM, Garry Hodgson <garry@REDACTED>
wrote:

> i'm using a gen_fsm to implement the client side of
> a binary protocol. clients connect to a controller, do
> a login thing, then settle into connected state. so far,
> so good.
>
> when in connected state, i need to be able to query
> the fsm for information. some of these queries require
> sending a message to a controller and waiting for a
> response to come back. herein lies the problem, because
> the query relies on the sync event to ask the question,
> but must wait for an { ssl, Socket, Data } event to get
> the answer.
>
> i've got it sort of working, but only by intercepting the
> ssl message inline, vs. the normal handle_info() method.
> this feels like cheating, and seems fragile, as it relies on
> there being no intervening messages.
>
> so i'm wondering, is there a good/standard way of
> accomplishing this? or is gen_fsm not the right approach?
> any advice would be appreciated.
>
> thanks
>
> code snippets follow:
>
> who_provides( Service ) -> gen_fsm:sync_send_event( ?MODULE, {
> request_services, Service } ).
> ...
> connected( { request_services, Service }, _From, #state{ socket=Socket,
> transport=Transport } = State ) ->
>     Transport:send( Socket, service_request( Service ) ),
>     case wait_for_services( Socket ) of
>         { ok, Services } ->
>             { reply, { ok, Services }, connected, State };
>         Other ->
>             { reply, { error, Other }, connected, State }
>     end.
>
> wait_for_services( Socket ) ->
>     receive
>         { ssl, Socket, Data } ->
>             ssl:setopts( Socket, [ { active, once } ] ),
>             { ok, parse( Data ) }
>     after 30000 ->
>         timeout
>     end.
>
>
> --
> Garry Hodgson
> Lead Member of Technical Staff
> AT&T Chief Security Office (CSO)
>
> "This e-mail and any files transmitted with it are AT&T property, are
> confidential, and are intended solely for the use of the individual or
> entity to whom this e-mail is addressed. If you are not one of the named
> recipient(s) or otherwise have reason to believe that you have received
> this message in error, please notify the sender and delete this message
> immediately from your computer. Any other use, retention, dissemination,
> forwarding, printing, or copying of this e-mail is strictly prohibited."
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150922/fcbba093/attachment.htm>


More information about the erlang-questions mailing list