aboutsummaryrefslogtreecommitdiffstats
path: root/server/dispatcher.erl
diff options
context:
space:
mode:
Diffstat (limited to 'server/dispatcher.erl')
-rw-r--r--server/dispatcher.erl31
1 files changed, 20 insertions, 11 deletions
diff --git a/server/dispatcher.erl b/server/dispatcher.erl
index 3806ef7..27c2d4c 100644
--- a/server/dispatcher.erl
+++ b/server/dispatcher.erl
@@ -1,7 +1,8 @@
-module(dispatcher).
--export([start/0, handle/2, checkUserExists/1]).
+-export([start/0, handle/2]).
checkUserExists([_|_]) ->
+ %% helper function to check if given array contains at least one element
false;
checkUserExists(_) ->
true.
@@ -12,6 +13,7 @@ start() ->
code:purge(server),
code:load_abs("../common/server"),
+ %% initalize database
cldb:init(),
%% spawn media backend in seperat process
@@ -24,13 +26,15 @@ start() ->
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('_', '_')),
+ %% 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 admit and after the first
+ %% registration only the admit could register the other user
+ UserExists = checkUserExists(cldb:all()),
try server:start(dis, dispatcher, UserExists) of
true ->
- io:format("Server started: ~w!~n", [UserExists]),
+ io:format("Server started!~n"),
true
catch
_ ->
@@ -39,14 +43,16 @@ start() ->
end.
handle({register, {User, Password}}, true) ->
- io:format("User created: ~s~n", [User]),
- cldb:register(User, Password, admin),
- {{ok, user_created}, false};
+ %% first registration
+ {cldb:register(User, Password, admin), false};
handle({register, _}, false) ->
+ %% if state of the server is false, no registration allowed,
+ %% because the admin user allready exists
{{error, no_rights}, false};
handle({login, {Node, User, Password}}, State) ->
+ %% login user if Password match
case cldb:login(User, Password) of
{ok, UserRow} ->
case client:start(Node, UserRow) of
@@ -59,9 +65,12 @@ handle({login, {Node, User, Password}}, State) ->
{{error, {user_or_password_invalid, Why}}, State}
end;
-handle({'EXIT', _, _}, State) ->
+handle({'EXIT', From, _}, State) ->
+ %% handle the exit messages from the client, so that the client
+ %% logouts if the connection between server and client breaks
+ cldb:logout(From),
State;
handle(Cmd, State) ->
- %% standard command, to find transmission errors
+ %% standard command, to find invalid commands and emit an error
{{error, {unknown_command, Cmd}}, State}.