aboutsummaryrefslogblamecommitdiffstats
path: root/server/client.erl
blob: 333df4a293bc3722f468301a9957ceca460bc940 (plain) (tree)
1
2
3
4
5
6
7
8
9
                
                                                
 
              
                                  
                                                       
                 
                                                
 
                                                      
               
                         

                                         
        


               
               






                                         
 
                    






                                         

                                






                                            

                                  






                                              
 
              


                                                    



                                                 

                                                    
-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}}}.