diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2010-10-14 10:25:44 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2010-10-14 10:25:44 +0200 |
commit | aa16b66ce4c64dd165292f5fb931d57f09d436d6 (patch) | |
tree | 786631e17528772ec71a6d578a322e2afe23a662 | |
parent | 9b5db7ab37a4a2527d523ff39bb0c5525553119e (diff) | |
download | erlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.tar.gz erlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.tar.xz erlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.zip |
removed name in server module
use references for assign the answers to the client
-rw-r--r-- | client/client.erl | 12 | ||||
-rw-r--r-- | common/server.erl | 71 | ||||
-rw-r--r-- | server/client.erl | 12 |
3 files changed, 45 insertions, 50 deletions
diff --git a/client/client.erl b/client/client.erl index d0bae4c..b1d8b0c 100644 --- a/client/client.erl +++ b/client/client.erl @@ -24,10 +24,8 @@ rpc(Server, Function, Params) -> register(Server, Name, Password) -> rpc(Server, register, [self(), {Name, Password}]), receive - {_, ok, Msg} -> - Msg; - {_, error, Msg} -> - {error, Msg} + {_, Msg} -> + Msg end. login(Server, Name, Password) -> @@ -38,10 +36,10 @@ login(Server, Name, Password) -> _ -> rpc(Server, login, [self(), {node(), Name, Password}]), receive - {_, ok, {ok, {logged_in, {Pid, _}}}} -> - server:registration(Pid, cli), + {ok, {ok, {logged_in, Pid}}} -> + register(cli, Pid), {ok, logged_in}; - {_, _, Msg} -> + {_, Msg} -> Msg; Msg -> Msg diff --git a/common/server.erl b/common/server.erl index bb64997..a718be9 100644 --- a/common/server.erl +++ b/common/server.erl @@ -5,69 +5,66 @@ %% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information. -module(server). --export([start/2, start/3, start_on_node/3, rpc/2, send/2, registration/2]). +-export([start/2, start/3, start_on_node/3, rpc/2]). start(Name, Mod) -> - register(Name, spawn(fun() -> loop(Name, Mod, Mod:init()) end)). + register(Name, spawn(fun() -> loop(Mod, Mod:init()) end)). start(Name, Mod, State) -> - register(Name, spawn(fun() -> loop(Name, Mod, State) end)). + register(Name, spawn(fun() -> loop(Mod, State) end)). start_on_node(Node, Mod, State) -> - Ref = make_ref(), - Pid = spawn(Node, fun() -> loop(Ref, Mod, State) end), - {Pid, Ref}. - -send({Client, _}, Data) -> - send(Client, Data); -send(Client, Data) -> - Client ! Data. + spawn(Node, fun() -> loop(Mod, State) end). -wait_response({_, Name}) -> - wait_response(Name); -wait_response(Name) -> +wait_response(_, Ref) -> receive - {Name, crash} -> exit(rpc); - {Name, ok, Response} -> Response + {Ref, crash} -> exit(rpc); + {Ref, ok, Response} -> Response end. rpc(Client, Request) -> - send(Client, {self(), Request}), - wait_response(Client). - -registration(Client, NewName) -> - send(Client, {register, NewName}). + Ref = make_ref(), + Client ! {Ref, self(), Request}, + wait_response(Client, Ref). -loop(Name, Mod, OldState) -> +loop(Mod, OldState) -> receive - {register, NewName} -> - register(NewName, self()), - loop(NewName, Mod, OldState); - {From, Request} -> try Mod:handle(Request, OldState) of {Response, NewState} -> - From ! {Name, ok, Response}, - loop(Name, Mod, NewState) + From ! {ok, Response}, + loop(Mod, NewState) + catch + _: Why -> + log_the_error(Request, Why), + From ! {crash}, + loop(Mod, OldState) + end; + + {Ref, From, Request} when is_reference(Ref) -> + try Mod:handle(Request, OldState) of + {Response, NewState} -> + From ! {Ref, ok, Response}, + loop(Mod, NewState) catch _: Why -> - log_the_error(Name, Request, Why), - From ! {Name, crash}, - loop(Name, Mod, OldState) + log_the_error(Request, Why), + From ! {Ref, crash}, + loop(Mod, OldState) end; {'EXIT', From, Why} -> try Mod:handle({'EXIT', From, Why}, OldState) of NewState -> - loop(Name, Mod, NewState) + loop(Mod, NewState) catch _: Why -> - log_the_error(From, 'EXIT', Why), - loop(Name, Mod, OldState) + log_the_error('EXIT', Why), + loop(Mod, OldState) end end. -log_the_error(Name, Request, Why) -> - io:format("Server ~p request ~p ~n" +log_the_error(Request, Why) -> + io:format("Server request ~p ~n" "caused exception ~p~n", - [Name, Request, Why]). + [Request, Why]). diff --git a/server/client.erl b/server/client.erl index 31d382f..ab55b3d 100644 --- a/server/client.erl +++ b/server/client.erl @@ -3,13 +3,13 @@ start(Node) -> process_flag(trap_exit, true), - {Client, Ref} = server:start_on_node(Node, client, undef), + Client = server:start_on_node(Node, client, undef), link(Client), - Server = spawn_link(client, loop, [{Client, Ref}]), + Server = spawn_link(client, loop, [Client]), - case server:rpc({Client, Ref}, {change_state, Server}) of + case server:rpc(Client, {change_state, Server}) of {ok} -> - {ok, {Client, Ref}}; + {ok, Client}; _ -> {error, unknown_error} end. @@ -17,11 +17,11 @@ start(Node) -> loop(Client) -> receive list -> - server:send(Client, {ok, {foo}}), + Client ! {ok, {foo}}, loop(Client); Cmd -> - server:send(Client, {error, {unknown_command, Cmd}}), + Client ! {error, {unknown_command, Cmd}}, loop(Client) end. |