aboutsummaryrefslogtreecommitdiffstats
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
parentebe8ca46d9288899a1c44490e79e7f17eebae545 (diff)
parenta31253b510636f48c7a50a54106dcb248028cf34 (diff)
downloaderlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.tar.gz
erlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.tar.xz
erlang-f6ea9554df27f8e0c1c1d41f0a8050a5b10bc017.zip
Merge branch 'master' of ssh://git.animux.de/erlang
-rw-r--r--client/client.erl42
-rw-r--r--common/server.erl71
-rw-r--r--server/client.erl25
3 files changed, 88 insertions, 50 deletions
diff --git a/client/client.erl b/client/client.erl
index d0bae4c..85f0e9d 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
@@ -74,3 +72,33 @@ handle({change_state, NewState}, _) ->
handle(Cmd, Server) ->
{{error, {unknown_command, Cmd}}, Server}.
+
+%queries the server for the current votes this client possesses
+getVotes() ->
+ rpc(Server, getVotes, self()),
+ receive
+ Msg ->
+ Msg
+ end.
+
+%positive vote, increments the votes for {Artist, Title} by one
+vote(Artist, Title) ->
+ rpc(Server, vote, [Artist, Title]),
+ receive
+ {_, ok, Msg} ->
+ Msg;
+ {_, error, Msg} ->
+ {error, Msg}
+ end.
+
+%negative vote, decrements the votes for {Artist, Title} by one
+devote(Artist, Title) ->
+ rpc(Server, devote, [Artist, Title]),
+ receive
+ {_, ok, Msg} ->
+ Msg;
+ {_, error, Msg} ->
+ {error, Msg}
+ end.
+
+
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..bf8bc22 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.
@@ -30,3 +30,16 @@ register(Client, {Name, Password}) ->
login(Client, {Node, Name, Password}) ->
dis ! {Client, {login, {Node, Name, Password}}}.
+
+getVotes() ->
+ media:getVotes(self()),
+ receive
+ Msg ->
+ Client ! Msg
+ end.
+
+vote(Artist,Title) ->
+ media:vote(Artist, Title).
+
+devote(Artist,Title) ->
+ media:devote(Artist, Title).