aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/client.erl36
1 files changed, 30 insertions, 6 deletions
diff --git a/client/client.erl b/client/client.erl
index 0e2fada..f500fe1 100644
--- a/client/client.erl
+++ b/client/client.erl
@@ -1,5 +1,5 @@
-module(client).
--export([register/3, login/3, list/0, handle/2, getVotes/0, vote/2, devote/2]).
+-export([register/2, register/3, login/3, list/0, handle/2, getVotes/0, vote/2, devote/2]).
checkLogin(Value) ->
%% check if user meets the requirements to be locked in or not by
@@ -17,13 +17,34 @@ buildNode(Server) ->
rpc(Server, Function, Params) ->
rpc:call(buildNode(Server), client, Function, Params).
-register(Server, Name, Password) ->
- rpc(Server, register, [self(), {Name, Password}]),
+wait_for_reply() ->
receive
{_, Msg} ->
- Msg
+ Msg;
+ Why -> Why
+ after 3000 ->
+ {error, timeout}
+ end.
+
+register(Name, Password) ->
+ try checkLogin(true) of
+ _ ->
+ server:rpc(cli, {register, Name, Password})
+ catch
+ {error, login_state} ->
+ {error, not_logged_in}
end.
+register(Server, Name, Password) ->
+ try checkLogin(false) of
+ _ ->
+ rpc(Server, register, [self(), {Name, Password}])
+ catch
+ {error, login_state} ->
+ {error, logged_in}
+ end,
+ wait_for_reply().
+
login(Server, Name, Password) ->
code:purge(server),
code:load_abs("../common/server"),
@@ -33,12 +54,14 @@ login(Server, Name, Password) ->
rpc(Server, login, [self(), {node(), Name, Password}]),
receive
{ok, {ok, {logged_in, Pid}}} ->
- register(cli, Pid),
+ erlang:register(cli, Pid),
{ok, logged_in};
{_, Msg} ->
Msg;
Msg ->
Msg
+ after 3000 ->
+ {error, timeout}
end
catch
{error, login_state} ->
@@ -71,7 +94,8 @@ 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({register, Name, Password}, Server) ->
+ send_to_server({register, Name, Password}, Server);
handle({change_state, NewState}, _) ->
{{ok}, NewState};