-module(client).
-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};
Why ->
{error, {unknown_error, Why}}
end.
execute(Client, F) ->
%% execute F() with error handling
%% (sends error message on exception)
try F() of
Result ->
Client ! {ok, Result}
catch
_: Why ->
Client ! {error, Why}
end.
loop(Client, User) ->
%% mainloop for client modul in server, handle the commands form
%% the logged_in client
receive
{register, Name, Password} ->
case cldb:check_rights(User) of
admin -> execute(Client, fun() -> cldb:register(Name, Password, none) end);
_ -> Client ! {error, {no_rights}}
end;
list ->
execute(Client, fun() -> media:all() end);
get_votes ->
execute(Client, fun() -> cldb:get_votes(User) end);
{vote, Artist, Title} ->
%% 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;
{devote, Artist, Title} ->
%% 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}}}.