From 4b73af956cd4e4a307720f9aeaa0d8d3a7ce4b45 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 15 Oct 2010 09:18:48 +0200 Subject: added that only logged_in clients get vote points, added comments --- server/dispatcher.erl | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'server/dispatcher.erl') 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}. -- cgit v1.2.3