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

          

                                      
 

                           








                                                           


               
                                             

                                 
                                             
            
                                                    


                                                                  
                                                
                                 
                         





                                                    
            


                                                     



                               
                                     
-module(dispatcher).
-export([start/0, init/0, handle/2]).

start() ->
    code:purge(server),
    code:load_abs("../common/server"),

    spawn(media, init, []),

    case server:start(dis, dispatcher) of
	true ->
	    io:format("Server started... ~n"),
	    true;
	_ ->
	    io:format("Error starting server! Exiting.~n"),
	    exit(1)
    end.

init() ->
    dict:new().

handle({register, {User, Password}}, Dict) ->
    case dict:find(User, Dict) of
	{ok, _}->
	    {{error, duplicated_user}, Dict};
	_ ->
	    io:format("User created: ~s~n", [User]),
	    {{ok, user_created}, dict:store(User, Password, Dict)}
    end;

handle({login, {Node, User, Password}}, Dict) ->
    case dict:find(User, Dict) of
	{ok, Password} ->
	    case client:start(Node) of
		{ok, Pid} ->
		    {{ok, {logged_in, Pid}}, Dict};
		_ ->
		    {{error, unable_to_login}, Dict}
	    end;
	_ ->
	    {{error, user_or_password_invalid}, Dict}
    end;

handle({'EXIT', _, _}, Dict) ->
    Dict;

handle(_, Dict) ->
    {{error, unknown_command}, Dict}.