aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJakob Pfender <jpfender@zedat.fu-berlin.de>2010-10-14 18:28:53 +0200
committerJakob Pfender <jpfender@zedat.fu-berlin.de>2010-10-14 18:28:53 +0200
commitf6bd2f3832095574abd682ef889ac1332295dc79 (patch)
treec9cf63bd80bff217f987bb646ad5280e02ec43fb /server
parentca4402975a29405ddb8e6501a635de1f91a66292 (diff)
parentf2164544210f511664ed7186107789ec55ec3edf (diff)
downloaderlang-f6bd2f3832095574abd682ef889ac1332295dc79.tar.gz
erlang-f6bd2f3832095574abd682ef889ac1332295dc79.tar.xz
erlang-f6bd2f3832095574abd682ef889ac1332295dc79.zip
Merge branch 'master' of ssh://git.animux.de/erlang
Diffstat (limited to 'server')
-rw-r--r--server/cldb.erl56
-rw-r--r--server/client.erl17
-rw-r--r--server/dispatcher.erl54
-rw-r--r--server/media.erl2
4 files changed, 78 insertions, 51 deletions
diff --git a/server/cldb.erl b/server/cldb.erl
index 982aafc..d514ff9 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,61 @@ 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.
-
+
+% after a song every client gets on more vote
+give_votes([Head|Rest]) ->
+ User = Head#user.name,
+ incVote(User),
+ give_votes(Rest);
+give_votes([]) -> ok.
+
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}.
diff --git a/server/media.erl b/server/media.erl
index 5eea592..80a3fea 100644
--- a/server/media.erl
+++ b/server/media.erl
@@ -112,7 +112,7 @@ play(Artist, Title) ->
io:format("playing: ~s, Artist: ~s~n", [Title, Artist]),
receive
- {Port, {exit_status, 0}} -> start_playing();
+ {Port, {exit_status, 0}} -> cldb:give_votes(cldb:ask("_","_")), start_playing();
{Port, {exit_status, S}} -> throw({commandfailed, S})
end.