diff options
Diffstat (limited to '')
-rw-r--r-- | server/client.erl | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/server/client.erl b/server/client.erl index 7cee336..421280b 100644 --- a/server/client.erl +++ b/server/client.erl @@ -2,11 +2,19 @@ -export([start/2, loop/2, register/2, login/2]). start(Node, User) -> + %% start linked processes on client and server to get noticed if + %% client disconnects and properly handle the logged in status process_flag(trap_exit, true), Client = server:start_on_node(Node, client, undef), link(Client), + %% store the client pid in the client database to find the correct + %% user on the exit message from the client + cldb:set_client_pid(User, Client), Server = spawn_link(client, loop, [Client, User]), + %% update the server process_id in the state of the client process, + %% so that the client process know directly its counterpart on the + %% server case server:rpc(Client, {change_state, Server}) of {ok} -> {ok, Client}; @@ -26,12 +34,12 @@ execute(Client, F) -> end. loop(Client, User) -> - %% mainloop for client modul in server + %% mainloop for client modul in server, handle the commands form + %% the logged_in client receive {register, Name, Password} -> - case User of - {user, _, _, _, admin} -> - execute(Client, fun() -> cldb:register(Name, Password, none) end); + case cldb:check_rights(User) of + admin -> execute(Client, fun() -> cldb:register(Name, Password, none) end); _ -> Client ! {error, {no_rights}} end; @@ -42,21 +50,30 @@ loop(Client, User) -> execute(Client, fun() -> media:getVotes(Client) end); {vote, Artist, Title} -> - case cldb:decVote(User) of + %% only allow vote if user has vote to give away + case cldb:dec_vote(User) of {ok} -> execute(Client, fun() -> media:vote(Artist, Title) end); _ -> Client ! {error, no_votes_available} - end; + end; {devote, Artist, Title} -> - execute(Client, fun() -> media:devote(Artist, Title) end); + %% only allow devote if user has vote to give away + case cldb:dec_vote(User) of + {ok} -> execute(Client, fun() -> media:devote(Artist, Title) end); + _ -> Client ! {error, no_votes_available} + end; Cmd -> + %% fallback for error messages Client ! {error, {unknown_command, Cmd}} end, loop(Client, User). register(Client, {Name, Password}) -> + %% forward the register messages to the dispatcher (if user is not + %% logged in) dis ! {Client, {register, {Name, Password}}}. login(Client, {Node, Name, Password}) -> + %% forward the login messages to the dispatcher dis ! {Client, {login, {Node, Name, Password}}}. |