aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to '')
-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}).