aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Ziener <mziener@lavabit.com>2010-10-14 16:14:21 +0200
committerMarco Ziener <mziener@lavabit.com>2010-10-14 16:14:21 +0200
commit3ab5da8355e114953c154114aa4c0c36b719e92e (patch)
tree8827715187800fb1bed183caff813dcbb4c05a0a
parentbde55a9f9456b7ab38b8ae35a3dc65cf4044aabe (diff)
parent8ee69a40c7782a69d44b9be08538cbe17531420b (diff)
downloaderlang-3ab5da8355e114953c154114aa4c0c36b719e92e.tar.gz
erlang-3ab5da8355e114953c154114aa4c0c36b719e92e.tar.xz
erlang-3ab5da8355e114953c154114aa4c0c36b719e92e.zip
Merge branch 'master' of ssh://git.animux.de/erlang
-rw-r--r--client/client.erl51
1 files changed, 20 insertions, 31 deletions
diff --git a/client/client.erl b/client/client.erl
index 46ef59f..0e2fada 100644
--- a/client/client.erl
+++ b/client/client.erl
@@ -1,19 +1,15 @@
-module(client).
-export([register/3, login/3, list/0, handle/2, getVotes/0, vote/2, devote/2]).
-contains([], _) ->
- false;
-contains([H|_], H) ->
- true;
-contains([_|T], Search) ->
- contains(T, Search).
-
checkLogin(Value) ->
- checkLogin(Value, contains(registered(), cli)).
-checkLogin(Value, Value) ->
- true;
-checkLogin(_, _) ->
- throw({error, login_state}).
+ %% check if user meets the requirements to be locked in or not by
+ %% locking in to the registered processes and check if the cli
+ %% process is registered
+ LoggedIn = lists:member(cli, registered()),
+ if LoggedIn == Value ->
+ true;
+ true -> throw({error, login_state})
+ end.
buildNode(Server) ->
list_to_atom("distributed_music_system_main_node@" ++ Server).
@@ -82,33 +78,26 @@ handle({change_state, NewState}, _) ->
handle(Cmd, Server) ->
{{error, {unknown_command, Cmd}}, Server}.
-%queries the server for the current votes this client possesses
-getVotes() ->
+try_logged_in_rpc(Rpc) ->
+ %% tries to execute an rpc for logged in user and returns error
+ %% message if user is not logged in
try checkLogin(true) of
_ ->
- server:rpc(cli, get_votes)
+ server:rpc(cli, Rpc)
catch
{error, login_state} ->
{error, not_logged_in}
end.
-%positive vote, increments the votes for {Artist, Title} by one
+getVotes() ->
+ %% queries the server for the current votes this client possesses
+ try_logged_in_rpc(get_votes).
+
vote(Artist, Title) ->
- try checkLogin(true) of
- _ ->
- server:rpc(cli, {vote, Artist, Title})
- catch
- {error, login_state} ->
- {error, not_logged_in}
- end.
+ %% positive vote, increments the votes for {Artist, Title} by one
+ try_logged_in_rpc({vote, Artist, Title}).
-%negative vote, decrements the votes for {Artist, Title} by one
devote(Artist, Title) ->
- try checkLogin(true) of
- _ ->
- server:rpc(cli, {devote, Artist, Title})
- catch
- {error, login_state} ->
- {error, not_logged_in}
- end.
+ %% negative vote, decrements the votes for {Artist, Title} by one
+ try_logged_in_rpc({devote, Artist, Title}).