First of all, thanks for answering my question!<br><br>But the solution you provided does not solve the problem completely. Because the main problem here is that if a process sends several messages in a row, then the receiving process may fail to process them correctly. Your solution can only solve the problem if the process only send <b>2</b> <b>messages </b>in a row,<br>
<br> receive<br>
{connected, Connector, MM} -><br>
<div> lib_chan_mm:send(MM, {login, Group, Nick}),<br>
</div> <b> receive {'EXIT', Connector, connectorFinished} -> ok end,</b><br> %% <b>can only take 1 more message </b>here even if you add more receiving match pattern, <br> %% the remaining message still goes into wait_login_response/2<br>
wait_login_response(Widget, MM);<br><br>For example if try_to_connect/4 sends 3 messages to parent,<br><br>try_to_connect(Parent, Host, Port, Pwd) -><br> ...<br> Parent ! {connected, self(), MM},<br>
Parent ! {hello, self(), MM),<br> exit(*connectorFinished*)<br> end.<br><br>The parent still fails to process them even with your solution,<br><br>disconnected(Widget, Group, Nick) -><br> receive<br>
{connected, Connector, MM} -><br> receive<br> {hello, _ } -> io:format("hello in connected~n");<br> // The following clause won't work!<br> {'EXIT', _Connector, connectorFinished} -> io:format("connect finished ~n") <br>
end,<br> lib_chan_mm:send(MM, {login, Group, Nick}),<br> wait_login_response(Widget, MM);<br><br>Because the added receive codes can only take 1 more message, the remaining messages still goes in to wait_login_response(Widget, MM);<br>
<br>So I still think we should make the outer receive codes process the message instead of letting one of matching section (connected here) process the remaining message. But is that possible ?<br><br>Thanks!<br><br>PS maybe you need to see the whole source code so I attach it here.<br>
<br>try_to_connect(Parent, Host, Port, Pwd) -><br> case lib_chan:connect(Host, Port, chat, Pwd, []) of<br> {error, _Why} -><br> Parent ! {status, {cannot, connect, Host, Port}},<br> sleep(2000),<br>
try_to_connect(Parent, Host, Port, Pwd);<br> {ok, MM} -><br> lib_chan_mm:controller(MM, Parent),<br> <b>Parent ! {connected, MM},</b><br> Parent ! hello,<br> exit(connectorFinished)<br>
end.<br><br>disconnected(Widget, Group, Nick) -><br> receive<br> {connected, MM} -><br> insert_str(Widget, "connected to server\nsending data\n"),<br> receive<br> hello -> io:format("hello in connected~n");<br>
{'EXIT', _Connector, connectorFinished} -> io:format("connect finished ~n") <br> end,<br> lib_chan_mm:send(MM, {login, Group, Nick}),<br> wait_login_response(Widget, MM);<br>
{Widget, destroyed} -><br> exit(died);<br> {status, S} -><br> insert_str(Widget, to_str(S)),<br> disconnected(Widget, Group, Nick);<br> Other -><br> io:format("chat_client disconnected unexpected:~p~n",[Other]),<br>
disconnected(Widget, Group, Nick)<br> end.<br><br>wait_login_response(Widget, MM) -><br> receive<br> {chan, MM, ack} -><br> io:format("I am login~n"),<br> active(Widget, MM);<br>
Other -><br> io:format("chat_client login unexpected:~p~n",[Other]),<br> wait_login_response(Widget, MM)<br> end. <br><br><br><div class="gmail_quote">On Wed, Jan 14, 2009 at 6:20 PM, Gleb Peregud <span dir="ltr"><<a href="mailto:gleber.p@gmail.com" target="_blank">gleber.p@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">2009/1/14 lang qiu <<a href="mailto:qiulang@gmail.com" target="_blank">qiulang@gmail.com</a>>:<br>
<div><div></div><div>> Hi all,<br>
><br>
> Nobody replies my question about IRC Lite, so I ask it the second time<br>
> (Change the title in hoping that it may cause some attention). And I notice<br>
> that someone asked about some other questions about IRC Lite before, which<br>
> may suggest this example is tricky.<br>
><br>
> So my here is my question, if a process sends another process several<br>
> messages in a row while in the receiving process when it processes the first<br>
> message, it does that in its own receiving loop, then all the remaining<br>
> message the first process sends goes into this loop and this may against the<br>
> purpose.<br>
><br>
> My words may sound nonsense. So check these codes,<br>
><br>
> In the chat_client.erl,<br>
><br>
> try_to_connect(Parent, Host, Port, Pwd) -><br>
> ...<br>
> Parent ! {connected, MM},<br>
> exit(connectorFinished)<br>
> end.<br>
><br>
> While in parent disconnected/3,<br>
> ...<br>
> receive<br>
> {connected, MM} -><br>
> lib_chan_mm:send(MM, {login, Group, Nick}),<br>
> wait_login_response(Widget, MM);<br>
> {'EXIT', _Pid, _Reason} -> %% if we add this clause here<br>
> ...<br>
> wait_login_response/2 defines,<br>
> ...<br>
> receive<br>
> ...<br>
> Other -><br>
> io:format("chat_client login unexpected:~p~p~n",[Other,MM]),<br>
> ...<br>
><br>
> The result shows that wait_login_response/2 receives try_to_connect's 'EXIT'<br>
> message not disconnected/3. I further test that if try_to_connect sends more<br>
> messages after {connected, MM} , they will all be received by<br>
> wait_login_response instead of disconnected/3 even there are match patterns<br>
> in disconnected/3 (Correct me if I am wrong). But this may not be a good<br>
> idea. Maybe it is better to let disconnected/3 processes the 'EXIT' message<br>
> instead of wait_login_response/2 (because it should only deal with the login<br>
> related message). So how do we fix this problem ? Or I miss something here<br>
> ?<br>
><br>
> Thanks,<br>
><br>
> Qiulang<br>
><br>
</div></div>> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
> <a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
<br>
<br>
<br>
You can try alter the code in the following way:<br>
<div><br>
try_to_connect(Parent, Host, Port, Pwd) -><br>
...<br>
</div> Parent ! {connected, self(), MM},<br>
<div> exit(connectorFinished)<br>
end.<br>
<br>
While in parent disconnected/3,<br>
...<br>
receive<br>
</div> {connected, Connector, MM} -><br>
<div> lib_chan_mm:send(MM, {login, Group, Nick}),<br>
</div> receive {'EXIT', Connector, connectorFinished} -> ok end,<br>
<div> wait_login_response(Widget, MM);<br>
{'EXIT', _Pid, _Reason} -> %% if we add this clause here<br>
...<br>
wait_login_response/2 defines,<br>
...<br>
receive<br>
...<br>
Other -><br>
io:format("chat_client login unexpected:~p~p~n",[Other,MM]),<br>
...<br>
<br>
</div>This change makes parent:disconnected/3 catch 'EXIT' message of<br>
connector at the appropriate moment. Note: I don't know what remaining<br>
code looks like, hence this is based solely on code in your email.<br>
<font color="#888888"><br>
--<br>
Gleb Peregud<br>
<a href="http://gleber.pl/" target="_blank">http://gleber.pl/</a><br>
<br>
Every minute is to be grasped.<br>
Time waits for nobody.<br>
-- Inscription on a Zen Gong<br>
</font></blockquote></div><br>