From 7b0bf7c8b3e57bbeadfb8e23ebe43696acf0bfe0 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 13:49:14 +0200 Subject: removed trailing whitespaces --- server/media.erl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/media.erl b/server/media.erl index fd4a1b6..0a99275 100644 --- a/server/media.erl +++ b/server/media.erl @@ -29,11 +29,11 @@ init() -> io:format("Initialisation of mnesia successful.\n"), start_playing(). -read_files([FN|Rest],Total,Fail) -> +read_files([FN|Rest],Total,Fail) -> case id3v2:read_file(FN) of {ok, Props} -> % insert entry into mnesia DB Artist = proplists:get_value(tpe1, Props), - Title = proplists:get_value(tit2, Props), + Title = proplists:get_value(tit2, Props), insert(bitstring_to_list(Artist), bitstring_to_list(Title), FN), read_files(Rest, Total+1, Fail); not_found -> read_files(Rest, Total+1, Fail+1) @@ -49,7 +49,6 @@ start_playing() -> insert(Artist, Title, Filepath) -> F = fun() -> - mnesia:write(#track{artist = Artist, title = Title, votes = 0, locked = false, filepath = Filepath}) end, mnesia:transaction(F). @@ -90,7 +89,7 @@ play(Artist, Title, Callback) -> % Of course we need a query to find out whats actually the most wished for track. % We will do it by requesting all the records from the database and then iteramte over just taking a look at the vote -% variable, so it is like list of integers. In case no tracks were voted for we just take the first track we find and play it. Of course it is locked afterwards so another will be choosen. +% variable, so it is like list of integers. In case no tracks were voted for we just take the first track we find and play it. Of course it is locked afterwards so another will be choosen. vote(Artist, Title) -> F = fun() -> -- cgit v1.2.3 From a8e3e940706ebc9f4a9a6a26ed2273d7f73f7905 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 14:07:20 +0200 Subject: fix database creation --- server/media.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'server') diff --git a/server/media.erl b/server/media.erl index 1957bdb..1edf3dc 100644 --- a/server/media.erl +++ b/server/media.erl @@ -19,7 +19,7 @@ init() -> mnesia:create_schema([node()]), mnesia:start(), - mnesia:create_table(track, [{index, [y]}, {attributes, record_info(fields, track)}]), + mnesia:create_table(track, [{attributes, record_info(fields, track)}]), read_files(filelib:wildcard(?TESTPATTERN),0,0), io:format("Initialisation of mnesia successful.\n"), start_playing(). -- cgit v1.2.3 From e70fe72c584a8798f5bd7024949c32de5ce90aa0 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 14:10:10 +0200 Subject: implemented some functions on the server --- server/client.erl | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'server') diff --git a/server/client.erl b/server/client.erl index ce7c99b..333df4a 100644 --- a/server/client.erl +++ b/server/client.erl @@ -10,32 +10,52 @@ start(Node) -> case server:rpc(Client, {change_state, Server}) of {ok} -> {ok, Client}; - _ -> - {error, unknown_error} + Why -> + {error, {unknown_error, Why}} end. loop(Client) -> receive list -> - Client ! {ok, {foo}}, - loop(Client); + try media:all() of + Result -> + Client ! {ok, Result} + catch + _: Why -> + Client ! {error, Why} + end; get_votes -> - Client ! {ok, media:getVotes(Client)}, - loop(Client); + try media:getVotes(Client) of + Result -> + Client ! {ok, Result} + catch + _: Why -> + Client ! {error, Why} + end; {vote, Artist, Title} -> - Client ! media:vote(Artist, Title), - loop(Client); + try media:vote(Artist, Title) of + Result -> + Client ! {ok, Result} + catch + _: Why -> + Client ! {error, Why} + end; {devote, Artist, Title} -> - Client ! media:devote(Artist, Title), - loop(Client); + try media:devote(Artist, Title) of + Result -> + Client ! {ok, Result} + catch + _: Why -> + Client ! {error, Why} + end; Cmd -> - Client ! {error, {unknown_command, Cmd}}, - loop(Client) - end. + Client ! {error, {unknown_command, Cmd}} + end, + loop(Client). register(Client, {Name, Password}) -> dis ! {Client, {register, {Name, Password}}}. -- cgit v1.2.3 From 181245f3cf74febd231d8072a8f1f8f038b0ee31 Mon Sep 17 00:00:00 2001 From: Marco Ziener Date: Thu, 14 Oct 2010 14:22:02 +0200 Subject: Additional Serverfu --- server/media.erl | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/media.erl b/server/media.erl index fb1d951..1463715 100644 --- a/server/media.erl +++ b/server/media.erl @@ -1,6 +1,7 @@ -module(media). -export([init/0,insert/3, ask/2, all/0, play/3, vote/2, devote/2]). -define(TESTPATTERN, "../ac/*.mp3"). +-define(TIMEOUT, 10000). % Since we are not willing to calculate and deliver all the id3 tags everytime they are requested, % we try to get something persistent with mnesia. @@ -19,10 +20,11 @@ init() -> mnesia:create_schema([node()]), mnesia:start(), - mnesia:create_table(track, [{index, [y]}, {attributes, record_info(fields, track)}]), + mnesia:create_table(track, [{attributes, record_info(fields, track)}]), read_files(filelib:wildcard(?TESTPATTERN),0,0), io:format("Initialisation of mnesia successful.\n"), - start_playing(). + start_playing(), + io:format("Starting to play music\n"). read_files([FN|Rest],Total,Fail) -> case id3v2:read_file(FN) of @@ -35,15 +37,17 @@ read_files([FN|Rest],Total,Fail) -> end; read_files([],Total,Fail) -> io:format("Total: ~w, Failed: ~w~n", [Total, Fail]). -% Basic insertion of entrys into the database. Some entries are left out because they are 0 or false. + % Our Runloop to play music all the time, play waits on exit_status + start_playing() -> {Artist, Title} = search_best(media:all(), 0,0), play(Artist, Title, "muh"). -%insert The Track into the database +% Basic insertion of entrys into the database. Some entries are left out because they are 0 or false. + insert(Artist, Title, Filepath) -> F = fun() -> mnesia:write(#track{artist = Artist, title = Title, votes = 0, locked = false, filepath = Filepath}) @@ -115,5 +119,27 @@ devote(Artist, Title) -> end, mnesia:transaction(F). -top() -> - All = all(). +lock(Artist, Title) -> + F = fun() -> + [Head|_] = ask(Artist, Title), + Votes = Head#track.votes -1, + New = Head#track(votes = Votes), + mnesia:write(New), + end, + mnesia:transaction(F). + +unlock(Artist, Title) -> + F = fun() -> + [Head|_] = ask(Artist, Title), + Votes = Head#track.votes -1, + New = Head#track{votes = Votes}, + mnesia:write(New), + end, + mnesia:transaction(F). + +lock_prozess(Artist, Title) -> + lock(Artist, Title), + receive + after TIMEOUT unlock(Artist, Title). + + -- cgit v1.2.3 From 9a20d171233eca0dee4a2c854cd15a878312ba65 Mon Sep 17 00:00:00 2001 From: Marco Ziener Date: Thu, 14 Oct 2010 14:34:18 +0200 Subject: Errors fixed --- server/media.erl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'server') diff --git a/server/media.erl b/server/media.erl index 9d5bcb6..7e84b28 100644 --- a/server/media.erl +++ b/server/media.erl @@ -122,9 +122,9 @@ devote(Artist, Title) -> lock(Artist, Title) -> F = fun() -> [Head|_] = ask(Artist, Title), - Votes = Head#track.votes -1, - New = Head#track(votes = Votes), - mnesia:write(New), + Votes = Head#track.votes - 1, + New = Head#track{votes = Votes}, + mnesia:write(New) end, mnesia:transaction(F). @@ -133,13 +133,16 @@ unlock(Artist, Title) -> [Head|_] = ask(Artist, Title), Votes = Head#track.votes -1, New = Head#track{votes = Votes}, - mnesia:write(New), + mnesia:write(New) end, mnesia:transaction(F). lock_prozess(Artist, Title) -> lock(Artist, Title), receive - after TIMEOUT unlock(Artist, Title). + + after ?TIMEOUT -> + unlock(Artist, Title) end. + -- cgit v1.2.3 From 6c2aac98c6647ed3bec434788c18370e12fff117 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 14:33:36 +0200 Subject: added media:init --- server/dispatcher.erl | 2 ++ 1 file changed, 2 insertions(+) (limited to 'server') diff --git a/server/dispatcher.erl b/server/dispatcher.erl index 4f1f58b..9b4c61b 100644 --- a/server/dispatcher.erl +++ b/server/dispatcher.erl @@ -5,6 +5,8 @@ start() -> code:purge(server), code:load_abs("../common/server"), + spawn(media, init, []), + case server:start(dis, dispatcher) of true -> io:format("Server started... ~n"), -- cgit v1.2.3