aboutsummaryrefslogtreecommitdiffstats
path: root/server/media.erl
diff options
context:
space:
mode:
Diffstat (limited to 'server/media.erl')
-rw-r--r--server/media.erl45
1 files changed, 37 insertions, 8 deletions
diff --git a/server/media.erl b/server/media.erl
index 8921724..d20c8e9 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,28 +23,31 @@ 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) ->
+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)
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})
@@ -104,7 +108,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() ->
@@ -124,5 +128,30 @@ 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) end.
+
+
+