fsm and networks

Vance Shipley <>
Sat May 1 19:09:05 CEST 2004

On Sat, May 01, 2004 at 02:09:26AM -0700, Dustin Sallings wrote:
}  	I don't quite know how to tell (or preferably wait until) I'm in the 
}  right state to issue queries.  I am not ready after init is finished.  
}  The server will send me an authentication request of some sort, I 
}  respond (via a handle_packet/5), and the server sends a few more things 
}  over and eventually a command letting me know it's ready to accept 
}  queries.  Ideally, init could block while this stuff occurs, but a 
}  variety of commands could arrive before the server's ready.

Your FSM isn't really an FSM when you don't track state.
The following may seem inefficient but it solves your problem:

init(Args) ->
	{ok, wait_for_auth, StateData}.

wait_for_auth(Packet, StateData) ->
	{next_state, wait_for_queries, StateData}.

wait_for_queries(Packet, StateData) ->
   {next_state, wait_for_queries, StateData}.

handle_info({tcp, _Port, Packet}, StateName, StateData) ->
	gen_fsm:send_event(self(), Packet),
	{next_state, StateName, StateData}.

}  	Similarly, I have the following:
}  execute(Fsm, Query) ->
}      gen_fsm:sync_send_event(Fsm,{execute,Query}).
}  and its corresponding
}  ready_for_query({execute, Query}, Pid, Info) ->
}      io:format("Executing query:  ~p~n", [Query]),
}      ok = gen_tcp:send(Info#pginfo.socket, [$Q|makeString(Query ++ 
}  "\0")]),
}      {reply, ok, query_pending, Info}.
}  	I'm not sure how to go about getting the results from this query.  
}  Right now, I just return, and let the handle_packet/5 print out the 
}  various stuff the DB sends back.

execute(Fsm, Query) ->
	gen_fsm:sync_send_event(Fsm, Query).

wait_for_queries(Query, From, StateData) ->
	% store From in NewStateData
	{next_state, wait_for_queries, NewStateData}.

wait_for_queries(ReplyPacket, StateData) ->
	% find appropriate From in StateData
	gen_fsm:reply(From, Reply),
   {next_state, wait_for_queries, StateData};


More information about the erlang-questions mailing list