-module(client).
-export([start/1, loop/1, register/2, login/2]).
start(Node) ->
process_flag(trap_exit, true),
Client = server:start_on_node(Node, client, undef),
link(Client),
Server = spawn_link(client, loop, [Client]),
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) ->
%% mainloop for client modul in server
receive
list ->
execute(Client, fun() -> media:all() end);
get_votes ->
execute(Client, fun() -> media:getVotes(Client) end);
{vote, Artist, Title} ->
execute(Client, fun() -> media:vote(Artist, Title) end);
{devote, Artist, Title} ->
execute(Client, fun() -> media:devote(Artist, Title) end);
Cmd ->
Client ! {error, {unknown_command, Cmd}}
end,
loop(Client).
register(Client, {Name, Password}) ->
dis ! {Client, {register, {Name, Password}}}.
login(Client, {Node, Name, Password}) ->
dis ! {Client, {login, {Node, Name, Password}}}.