aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2010-10-14 10:25:44 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2010-10-14 10:25:44 +0200
commitaa16b66ce4c64dd165292f5fb931d57f09d436d6 (patch)
tree786631e17528772ec71a6d578a322e2afe23a662 /common
parent9b5db7ab37a4a2527d523ff39bb0c5525553119e (diff)
downloaderlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.tar.gz
erlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.tar.xz
erlang-aa16b66ce4c64dd165292f5fb931d57f09d436d6.zip
removed name in server module
use references for assign the answers to the client
Diffstat (limited to 'common')
-rw-r--r--common/server.erl71
1 files changed, 34 insertions, 37 deletions
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]).