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