From ab13a717fe4e443c233694c69573deb11dbdf453 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 17:37:18 +0200 Subject: changed user database from dict to mnesia (modules: cldb) --- server/dispatcher.erl | 54 +++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'server/dispatcher.erl') diff --git a/server/dispatcher.erl b/server/dispatcher.erl index e5e0572..3806ef7 100644 --- a/server/dispatcher.erl +++ b/server/dispatcher.erl @@ -1,5 +1,10 @@ -module(dispatcher). --export([start/0, init/0, handle/2]). +-export([start/0, handle/2, checkUserExists/1]). + +checkUserExists([_|_]) -> + false; +checkUserExists(_) -> + true. start() -> %% load server module from common directory, @@ -7,6 +12,8 @@ start() -> code:purge(server), code:load_abs("../common/server"), + cldb:init(), + %% spawn media backend in seperat process try spawn(media, init, []) of _ -> @@ -20,9 +27,10 @@ start() -> %% start server (registered as dis) %% server-module will call handle if message arrives and init to %% initialize the status - try server:start(dis, dispatcher) of + UserExists = checkUserExists(cldb:ask('_', '_')), + try server:start(dis, dispatcher, UserExists) of true -> - io:format("Server started!~n"), + io:format("Server started: ~w!~n", [UserExists]), true catch _ -> @@ -30,34 +38,30 @@ start() -> exit(1) end. -init() -> - dict:new(). +handle({register, {User, Password}}, true) -> + io:format("User created: ~s~n", [User]), + cldb:register(User, Password, admin), + {{ok, user_created}, false}; -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({register, _}, false) -> + {{error, no_rights}, false}; -handle({login, {Node, User, Password}}, Dict) -> - case dict:find(User, Dict) of - {ok, Password} -> - case client:start(Node) of +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}}, Dict}; + {{ok, {logged_in, Pid}}, State}; Why -> - {{error, {unable_to_login, Why}}, Dict} + {{error, {unable_to_login, Why}}, State} end; - _ -> - {{error, user_or_password_invalid}, Dict} + Why -> + {{error, {user_or_password_invalid, Why}}, State} end; -handle({'EXIT', _, _}, Dict) -> - Dict; +handle({'EXIT', _, _}, State) -> + State; -handle(Cmd, Dict) -> +handle(Cmd, State) -> %% standard command, to find transmission errors - {{error, {unknown_command, Cmd}}, Dict}. + {{error, {unknown_command, Cmd}}, State}. -- cgit v1.2.3