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

                         
                                                                            


                     

          

                                                

                                      
 
                                

                
                                              







                                                                             
 
                                                                     

                                                                      

                                                                 
                                             
                                                    
               
                                           

                




                                                           
                                             

                                                  
 
                               

                                                                    
                                
 
                                                 
                                     


                                               
                            
                                                    
                      
                                                            
                



                                                      

        

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

checkUserExists([_|_]) ->
    %% helper function to check if given array contains at least one element
    false;
checkUserExists(_) ->
    true.

start() ->
    %% load server module from common directory,
    %% used form server and client
    code:purge(server),
    code:load_abs("../common/server"),

    %% initalize client database
    cldb:init(),

    %% spawn media backend in seperate process
    try spawn(media, init, []) of
	_ ->
	    io:format("Media-Backend started!~n")
    catch
	_: Why ->
	    io:format("Error starting media backend: ~w! Exiting.~n", [Why]),
	    exit(1)
    end,

    %% start server (registered as "dis", arriving messages will call
    %% the handle function to get the result)
    %% The state of the server is true if register is allowed (no user
    %% exists) -> first created user is admin and after the first
    %% registration only the admin can register other users
    UserExists = checkUserExists(cldb:all()),
    try server:start(dis, dispatcher, UserExists) of
	true ->
	    io:format("Server started!~n"),
	    true
    catch
	_ ->
	    io:format("Error starting server! Exiting.~n"),
	    exit(1)
    end.

handle({register, {User, Password}}, true) ->
    %% first registration
    {cldb:register(User, Password, admin), false};

handle({register, _}, false) ->
    %% if state of the server is false, registration is not allowed,
    %% because the admin user already exists
    {{error, no_rights}, false};

handle({login, {Node, User, Password}}, State) ->
    %% login user if password correct
    case cldb:login(User, Password) of
	{ok, UserRow} ->
	    case client:start(Node, UserRow) of
		{ok, Pid} ->
		    {{ok, {logged_in, Pid}}, State};
		Why ->
		    {{error, {unable_to_login, Why}}, State}
	    end;
	{error, Why} ->
	    {{error, Why}, State};
	_ ->
	    {{error, user_or_password_invalid}, State}
    end;

handle({'EXIT', From, _}, State) ->
    %% handle the exit messages from the client, so that the client
    %% logs out if the connection between server and client breaks
    cldb:logout(From),
    State;

handle(Cmd, State) ->
    %% standard command to find invalid commands and emit an error
    {{error, {unknown_command, Cmd}}, State}.