<br><br><div class="gmail_quote">On 7 December 2011 15:00, Vance Shipley <span dir="ltr"><<a href="mailto:vances@motivity.ca">vances@motivity.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Wed, Dec 07, 2011 at 01:31:42PM +0700, Maxim Treskin wrote:<br>
} 1. gen_fsm incompatible with gen_server. We cannot use synchronous call<br>
} from gen_server for gen_fsm because it different.<br>
<br>
</div>Are you suggesting that you should be able to write:<br>
<br>
Reply = Module:call(Ref, Request)<br>
<br>
Where Ref is a reference to either a gen_server a or gen_fsm?<br>
You still need to use the correct module. I don't see how much<br>
that helps you over:<br>
<br>
Reply = case Module of<br>
gen_server -><br>
gen_server:call(Ref, Request);<br>
gen_fsm -><br>
gen_fsm:sync_send_event(Ref, Request)<br>
end<br>
<div class="im"><br></div></blockquote><div>So I need to pass behaviour with pid() of process to client.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">
} 2. Two different actions: send_event и send_all_state_event with different<br>
} handlers. Why? If we want to send message to FSM, we just send message to<br>
} process without knowledge of its internal state-related issues, right?<br>
<br>
</div>When you call gen_fsm:send_event/2 to send an event the function<br>
StateName/2 in your callback module will be called for any state.<br>
You don't need to use send_all_state_event/2 at all. If you do<br>
it is processed in your callback module's handle_event/3 function<br>
and not the individual state's event handler StateName/2.<br>
<div class="im"><br>
} 3. For complex protocols and when one message emits almost equal actions in<br>
} various process states, code of gen_fsm-process becomes pumpkinized. Every<br>
} time I start writing a new gen_fsm module, I think that this time the code<br>
} will not become pumpkinized, but this wrong. Large number of pattern<br>
} matching with records in events and internal state of process in various<br>
} state handlers makes code duplicated and unreadable.<br>
<br>
</div>How can it be any other way? You say "_almost_ equal actions in<br>
_various_ process states". So it's not all states and not the same<br>
action. Therefore you need to uniquely handle events in states.<br>
<div class="im"><br></div></blockquote><div>Putting state name in #state{name = StateName} of gen_server gives necessary result. We can do</div><div><br></div><div>handle_call(#request{... large pattern matching ...} = Request, From, #state{name = StateName, ... another pattern matching...} = State) -></div>
<div> do_smth...,</div><div> case StateName of</div><div> %% State specific code</div><div> ....</div><div> end,</div><div> do_yet_another_smth...,</div><div> {reply, Result, State}</div><div><br></div><div>instead of copies `large pattern matching` Request with State if we use gen_fsm.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">
} Is there any best practices in gen_fsm usage? May be I and other people<br>
} uses gen_fsm improperly?<br>
<br>
</div>Obviously putting common processing in seperate functions and calling<br>
them from the state handlers is the way to handle 3.<br>
<br>
If you still have a problem with 1 or 2 you might want to just simply<br>
send messages ('Ref ! Request') and handle them in<br>
gen_server:handle_info/2 and gen_fsm:handle_info/3.<br>
<br>
If you are still unsatisfied you could implement your own FSM<br>
behaviour in place of gen_fsm. You may find this thread interesting:<br>
<br>
<a href="http://erlang.org/pipermail/erlang-questions/2005-April/015097.html" target="_blank">http://erlang.org/pipermail/erlang-questions/2005-April/015097.html</a><br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
-Vance<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>Maxim Treskin<br>