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





                                                

          

                                                

                                      
 

                
                                             







                                                                             
 


                                                                    

                                                     
               
                                                             

                




                                                           



                                             
 

                                
 



                                                 
                            
                                                    
                      
                                                            
                

                                                             

        

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

checkUserExists([_|_]) ->
    false;
checkUserExists(_) ->
    true.

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

    cldb:init(),

    %% spawn media backend in seperat 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)
    %% server-module will call handle if message arrives and init to
    %% initialize the status
    UserExists = checkUserExists(cldb:ask('_', '_')),
    try server:start(dis, dispatcher, UserExists) of
	true ->
	    io:format("Server started: ~w!~n", [UserExists]),
	    true
    catch
	_ ->
	    io:format("Error starting server! Exiting.~n"),
	    exit(1)
    end.

handle({register, {User, Password}}, true) ->
    io:format("User created: ~s~n", [User]),
    cldb:register(User, Password, admin),
    {{ok, user_created}, false};

handle({register, _}, false) ->
    {{error, no_rights}, false};

handle({login, {Node, User, Password}}, State) ->
    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;
	Why ->
	    {{error, {user_or_password_invalid, Why}}, State}
    end;

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

handle(Cmd, State) ->
    %% standard command, to find transmission errors
    {{error, {unknown_command, Cmd}}, State}.