aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorMarco Ziener <mziener@lavabit.com>2010-10-14 10:38:37 +0200
committerMarco Ziener <mziener@lavabit.com>2010-10-14 10:38:37 +0200
commitf6ea9554df27f8e0c1c1d41f0a8050a5b10bc017 (patch)
tree10bd442811e0a03faec99ccea0056b4aa6586a47 /common
parentebe8ca46d9288899a1c44490e79e7f17eebae545 (diff)
parenta31253b510636f48c7a50a54106dcb248028cf34 (diff)
downloaderlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.tar.gz
erlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.tar.xz
erlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.zip
Merge branch 'master' of ssh://git.animux.de/erlang
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]).