aboutsummaryrefslogtreecommitdiffstats
path: root/common/server.erl
diff options
context:
space:
mode:
authorMichael Wittig <michael.wittig@fu-berlin.de>2010-10-14 10:36:37 +0200
committerMichael Wittig <michael.wittig@fu-berlin.de>2010-10-14 10:36:37 +0200
commit1b6c636b0584d1264209fab522c4476082f0e0fd (patch)
treed38b97c98f36c2bcc8fded4837d0394e9d2fba97 /common/server.erl
parent0dc60dd485a35b2c2336cf56595c2731b4634877 (diff)
parent2a672cf96dc272213a36d15f78ec19b6579de4df (diff)
downloaderlang-1b6c636b0584d1264209fab522c4476082f0e0fd.tar.gz
erlang-1b6c636b0584d1264209fab522c4476082f0e0fd.tar.xz
erlang-1b6c636b0584d1264209fab522c4476082f0e0fd.zip
Merge branch 'master' of ssh://git.animux.de/erlang
Diffstat (limited to 'common/server.erl')
-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]).