diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/cldb.erl | 48 | ||||
-rw-r--r-- | server/client.erl | 17 | ||||
-rw-r--r-- | server/dispatcher.erl | 54 |
3 files changed, 69 insertions, 50 deletions
diff --git a/server/cldb.erl b/server/cldb.erl index 982aafc..0602b7d 100644 --- a/server/cldb.erl +++ b/server/cldb.erl @@ -1,7 +1,7 @@ % The clientdatabase -module(cldb). --export([init/0, ask/2, login/2, decVote/2, incVote/2, register/3, check_rights/2]). +-export([init/0, ask/2, login/2, decVote/1, incVote/1, register/3, check_rights/1]). -record(user, {name, passwd, votes, rights}). init() -> @@ -9,45 +9,53 @@ init() -> mnesia:start(), mnesia:create_table(user, [{attributes, record_info(fields, user)}]), io:format("Userdb up and running \n"). - + ask(User, Pwd) -> F = fun() -> - mnesia:match_object({user, User, Pwd, '_'}) + mnesia:match_object({user, User, Pwd, '_', '_'}) end, mnesia:transaction(F). - - - + login(User, Pwd) -> - {_, Reason} = ask(User, Pwd), - Reason. + case ask(User, Pwd) of + {atomic, [UserRow|_]} -> {ok, UserRow}; + _-> {error} + end. register(User, Pwd, Root) -> - F = fun() -> - mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root}) - end, - {_, Reason} = mnesia:transaction(F), - Reason. + case ask(User, '_') of + {atomic, []} -> + F = fun() -> + mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root}) + end, + case mnesia:transaction(F) of + {atomic, ok} -> + {ok, user_created}; + {atomic, Why} -> + {error, Why} + end; + _ -> + {error, duplicated_user} + end. -decVote(User, Pwd) -> +decVote(User) -> F = fun() -> - [Head|_] = ask(User, Pwd), + [Head|_] = ask(User, '_'), Votes = Head#user.votes - 1, New = Head#user{votes = Votes}, mnesia:write(New) end, mnesia:transaction(F). -incVote(User, Pwd) -> +incVote(User) -> F = fun() -> - [Head|_] = ask(User, Pwd), + [Head|_] = ask(User, '_'), Votes = Head#user.votes + 1, New = Head#user{votes = Votes}, mnesia:write(New) end, mnesia:transaction(F). -check_rights(User, Name) -> - {_, _, Rights} = ask(User, Name), +check_rights(User) -> + {_, _, Rights} = ask(User, '_'), Rights. - diff --git a/server/client.erl b/server/client.erl index cb6504e..d12c891 100644 --- a/server/client.erl +++ b/server/client.erl @@ -1,11 +1,11 @@ -module(client). --export([start/1, loop/1, register/2, login/2]). +-export([start/2, loop/2, register/2, login/2]). -start(Node) -> +start(Node, User) -> process_flag(trap_exit, true), Client = server:start_on_node(Node, client, undef), link(Client), - Server = spawn_link(client, loop, [Client]), + Server = spawn_link(client, loop, [Client, User]), case server:rpc(Client, {change_state, Server}) of {ok} -> @@ -25,9 +25,16 @@ execute(Client, F) -> Client ! {error, Why} end. -loop(Client) -> +loop(Client, User) -> %% mainloop for client modul in server receive + {register, Name, Password} -> + case User of + {user, _, _, _, admin} -> + execute(Client, fun() -> cldb:register(Name, Password, none) end); + _ -> Client ! {error, {no_rights}} + end; + list -> execute(Client, fun() -> media:all() end); @@ -43,7 +50,7 @@ loop(Client) -> Cmd -> Client ! {error, {unknown_command, Cmd}} end, - loop(Client). + loop(Client, User). register(Client, {Name, Password}) -> dis ! {Client, {register, {Name, Password}}}. 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}. |