aboutsummaryrefslogtreecommitdiffstats
path: root/client/client.erl
diff options
context:
space:
mode:
Diffstat (limited to 'client/client.erl')
-rw-r--r--client/client.erl60
1 files changed, 35 insertions, 25 deletions
diff --git a/client/client.erl b/client/client.erl
index 67be4ee..46ef59f 100644
--- a/client/client.erl
+++ b/client/client.erl
@@ -1,5 +1,5 @@
-module(client).
--export([register/3, login/3, list/0, handle/2]).
+-export([register/3, login/3, list/0, handle/2, getVotes/0, vote/2, devote/2]).
contains([], _) ->
false;
@@ -58,14 +58,23 @@ list() ->
{error, not_logged_in}
end.
-handle(list, Server) ->
- Server ! list,
+send_to_server(Cmd, Server) ->
+ Server ! Cmd,
receive
{ok, Msg} ->
{Msg, Server};
Msg ->
{Msg, Server}
- end;
+ end.
+
+handle(list, Server) ->
+ send_to_server(list, Server);
+handle(get_votes, Server) ->
+ send_to_server(get_votes, Server);
+handle({vote, Artist, Title}, Server) ->
+ send_to_server({vote, Artist, Title}, Server);
+handle({devote, Artist, Title}, Server) ->
+ send_to_server({devote, Artist, Title}, Server);
handle({change_state, NewState}, _) ->
{{ok}, NewState};
@@ -74,31 +83,32 @@ handle(Cmd, Server) ->
{{error, {unknown_command, Cmd}}, Server}.
%queries the server for the current votes this client possesses
-getVotes(Server) ->
- rpc(Server, getVotes, self()),
- receive
- Msg ->
- Msg
- end.
+getVotes() ->
+ try checkLogin(true) of
+ _ ->
+ server:rpc(cli, get_votes)
+ catch
+ {error, login_state} ->
+ {error, not_logged_in}
+ 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.
+ try checkLogin(true) of
+ _ ->
+ server:rpc(cli, {vote, Artist, Title})
+ catch
+ {error, login_state} ->
+ {error, not_logged_in}
+ 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.
-
+ try checkLogin(true) of
+ _ ->
+ server:rpc(cli, {devote, Artist, Title})
+ catch
+ {error, login_state} ->
+ {error, not_logged_in}
+ end.