1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
-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}}}.
|