Code examples for my questions:<br><br>Simplified Socket Server, taken from<br>http://www.erlang.org/doc/man/gen_tcp.html and modified <br>for auto updates:<br><br>-module(test).<br>-compile(export_all).<br><br>start() -><br> spawn(?MODULE, start1, [3,5678]).<br><br>start1(Num,Port) -><br> register(?MODULE, self()),<br> start(Num,Port),<br> sloop([]).<br><br>sloop(Ls) -><br> receive<br> {accept, Pid} -><br> sloop([Pid|Ls]);<br> upgrade -><br> io:format("TEST: Loop upgrading.~n"),<br> lists:foreach(fun(Pid) -> <br> Pid ! upgrade,<br> io:format("TEST: Loop .. upgrading
..~p..~n",[Pid])<br> end, Ls),<br> ?MODULE:sloop(Ls);<br> quit -><br> io:format("Loop quitting~n"),<br> sloop(Ls);<br> Other -><br> io:format("Loop other: ~p~n",[Other]),<br> sloop(Ls)<br> end.<br><br><br>start(Num,LPort) -><br> case gen_tcp:listen(LPort,[{active, false},{packet,2}]) of<br> {ok, ListenSock} -><br> start_servers(Num,ListenSock),<br> {ok, Port} = inet:port(ListenSock),<br>
Port;<br> {error,Reason} -><br> {error,Reason}<br> end.<br><br>start_servers(0,_) -><br> ok;<br>start_servers(Num,LS) -><br> Pid=spawn(?MODULE,server,[LS]),<br> ?MODULE ! {accept, Pid},<br> start_servers(Num-1,LS).<br> <br>server(LS) -><br> case gen_tcp:accept(LS) of<br> {ok,S} -><br> loop(S),<br> ?MODULE:start_servers(1,LS);<br> upgrade -><br> io:format("TEST: ACPT upgrading ~p~n",
[self()]),<br> ?MODULE:server(LS);<br> Other -><br> io:format("accept returned ~w - goodbye!~n",[Other]),<br> ok<br> end,<br><br> receive<br> upgrade -><br> io:format("TEST: ACPT2 upgrading ~p~n", [self()]),<br> ?MODULE:server(LS);<br> Other1 -><br> io:format("accept returned ~w - goodbye!~n",[Other1]),<br> server(LS)<br> end.<br><br><br>loop(S) -><br> inet:setopts(S,[{active,once}]),<br>
receive<br> {tcp,S,Data} -><br> Answer = process(Data), <br> gen_tcp:send(S,Answer),<br> loop(S);<br> {tcp_closed,S} -><br> io:format("Socket ~w closed [~w]~n",[S,self()]),<br> ok;<br> upgrade -><br> io:format("TEST: Client upgrading ~p~n", [self()]),<br> ?MODULE:loop(S)<br> end.<br><br>process(Data) -> Data.<br><br><br>******************************<br>The original code has a flaw in that if you type in<br>something erroneous in the
shell and your program will<br>terminate, because the socket responds to errors in the<br>controlling process, which by default is whoever created it,<br>so I modified the code so that the program spawns a loop <br>function (the controlling process) and stays there, hence<br>errors in the shell no longer affect it.<br><br>Problem: when sending a "test ! upgrade." the instruction <br>"gen_tcp:accept(LS)" does NOT receive the update and <br>hence the 2nd update fails and terminates the code. How <br>can I make this work ??<br><br>*******************************************************************<br>Parser questions:<br>This example should work now:<br><br>parsecmd(T) -><br> parsecmd(T, []).<br><br>parsecmd("{USR:" ++ T, L) -><br> {Url, T1} = collect_usr(T, reverse("")),<br> parsecmd(T1, [Url|L]);<br><br>parsecmd([_|T], L) -><br> parsecmd(T, L);<br><br>parsecmd([], L)
-><br> L.<br><br>collect_usr("}" ++ T, L) -> {reverse(L, ""), T};<br>collect_usr([H|T], L) -> collect_usr(T, [H|L]);<br>collect_usr([], _) -> {[],[]}.<br><br>*******************************************************************<br>What does not work is this:<br><br>parsecmd(Cmd, T) -><br> parsecmd(Cmd, T, []).<br><br>parsecmd(Cmd ++ T, L) -><br> {Url, T1} = collect_usr(T, reverse("")),<br> parsecmd(T1, [Url|L]);<br>.......<br><br>One cannot insert a variable Cmd in this case to make this search more flexible, Erlang complains about it. The statement to be evaluated MUST be a fixed one, which is not so nice, so "parsecmd(Cmd ++ T, L) ->" will not work,<br>"parsecmd("{USR:" ++ T, L) ->" will, but the term is fixed, I would like it to be variable. <br><br>I hope this clarifies my doubts a
bit.<br><br>Thanks,<br><br>Bo<br><br><p>
<hr size=1>You rock. That's why Blockbuster's offering you <a href="http://us.rd.yahoo.com/evt=47523/*http://tc.deals.yahoo.com/tc/blockbuster/text5.com">one month of Blockbuster Total Access</a>, No Cost.