diff options
-rw-r--r-- | server/media.erl | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/server/media.erl b/server/media.erl index 1edf3dc..9d5bcb6 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. @@ -22,7 +23,8 @@ init() -> 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). + + |