aboutsummaryrefslogtreecommitdiffstats
path: root/server/client.erl
blob: 333df4a293bc3722f468301a9957ceca460bc940 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
-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.

loop(Client) ->
    receive
	list ->
	    try media:all() of
		Result ->
		    Client ! {ok, Result}
	    catch
		_: Why ->
		    Client ! {error, Why}
	    end;

	get_votes ->
	    try media:getVotes(Client) of
		Result ->
		    Client ! {ok, Result}
	    catch
		_: Why ->
		    Client ! {error, Why}
	    end;

	{vote, Artist, Title} ->
	    try media:vote(Artist, Title) of
		Result ->
		    Client ! {ok, Result}
	    catch
		_: Why ->
		    Client ! {error, Why}
	    end;

	{devote, Artist, Title} ->
	    try media:devote(Artist, Title) of
		Result ->
		    Client ! {ok, Result}
	    catch
		_: Why ->
		    Client ! {error, Why}
	    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}}}.