diff options
author | Jakob Pfender <jpfender@zedat.fu-berlin.de> | 2010-10-14 16:34:44 +0200 |
---|---|---|
committer | Jakob Pfender <jpfender@zedat.fu-berlin.de> | 2010-10-14 16:34:44 +0200 |
commit | a53e0ebbf1e466abd0fce29688eb4112e7e6bff5 (patch) | |
tree | 4bcb80aa32bec556d717e3338e6d96e32d4ba989 /server | |
parent | 83c76ed4f695dfd033308c663024f7a7e51c13e2 (diff) | |
parent | 3ab5da8355e114953c154114aa4c0c36b719e92e (diff) | |
download | erlang-a53e0ebbf1e466abd0fce29688eb4112e7e6bff5.tar.gz erlang-a53e0ebbf1e466abd0fce29688eb4112e7e6bff5.tar.xz erlang-a53e0ebbf1e466abd0fce29688eb4112e7e6bff5.zip |
documentation for media.erl
Diffstat (limited to 'server')
-rw-r--r-- | server/cldb.erl | 47 | ||||
-rw-r--r-- | server/media.erl | 48 |
2 files changed, 84 insertions, 11 deletions
diff --git a/server/cldb.erl b/server/cldb.erl index 0f9f407..982aafc 100644 --- a/server/cldb.erl +++ b/server/cldb.erl @@ -1,12 +1,53 @@ % The clientdatabase -module(cldb). --export([init/0]). --record(user, {name, passwd, votes}). +-export([init/0, ask/2, login/2, decVote/2, incVote/2, register/3, check_rights/2]). +-record(user, {name, passwd, votes, rights}). init() -> mnesia:create_schema([node()]), mnesia:start(), - mnesia:create_table(track, [{attributes, record_info(fields, track)}]), + 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, '_'}) + end, + mnesia:transaction(F). + + + +login(User, Pwd) -> + {_, Reason} = ask(User, Pwd), + Reason. + +register(User, Pwd, Root) -> + F = fun() -> + mnesia:write(#user{name = User, passwd = Pwd, votes = 5, rights = Root}) + end, + {_, Reason} = mnesia:transaction(F), + Reason. + +decVote(User, Pwd) -> + F = fun() -> + [Head|_] = ask(User, Pwd), + Votes = Head#user.votes - 1, + New = Head#user{votes = Votes}, + mnesia:write(New) + end, + mnesia:transaction(F). + +incVote(User, Pwd) -> + F = fun() -> + [Head|_] = ask(User, Pwd), + 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), + Rights. + diff --git a/server/media.erl b/server/media.erl index 0ef5a44..d6adc5b 100644 --- a/server/media.erl +++ b/server/media.erl @@ -1,5 +1,5 @@ -module(media). --export([init/0,insert/3, ask/2, all/0, play/3, vote/2, devote/2, lock_process/2]). +-export([init/0,insert/3, ask/2, all/0, play/2, vote/2, devote/2, lock_process/2]). -define(TESTPATTERN, "../ac/*.mp3"). -define(TIMEOUT, 100000000). @@ -59,7 +59,11 @@ insert(Artist, Title, Filepath) -> end, mnesia:transaction(F). -% search the track with the highest votes and return {Artist, Title} +% 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. + search_best([Head|Rest], Max_Votes, Track) -> if @@ -72,7 +76,7 @@ search_best([], _, Track) -> {Track#track.artist, Track#track.title}. % if nothing is playable anymore (because all songs are locked) just reset all songs and start playing again... -reset_all([Head|Rest) -> +reset_all([Head|Rest]) -> unlock(Head#track.artist, Head#track.title), reset_all(Rest); reset_all([]) -> ok. @@ -104,18 +108,22 @@ play(Artist, Title) -> {_, Title, Artist, _, _, Fp} = Head, Port = erlang:open_port({spawn_executable, "/usr/bin/mplayer"}, [{args, [Fp]}, exit_status]), reset_votes(Artist, Title), - lock_process(Artist, Title), + spawn(media, lock_process, [Artist, Title]), io:format("playing: ~s, Artist: ~s~n", [Title, Artist]), receive {Port, {exit_status, 0}} -> start_playing(); {Port, {exit_status, S}} -> throw({commandfailed, S}) end. +<<<<<<< HEAD % Of course we need a query to find out what the highest voted track is. % We accomplish this by requesting all the records from the database and then iterate over them, just taking a look at the vote % variable, so it is like a list of integers. In case no tracks were voted for we just take the first track we found and play it. Of course this song is locked afterwards so a different one will be chosen. +======= +%Increase the vote in the database so it will hopefully be played in the future +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e %votes for a track, i.e. increases its vote count by one. vote(Artist, Title) -> @@ -127,7 +135,12 @@ vote(Artist, Title) -> end, mnesia:transaction(F). +<<<<<<< HEAD %votes against a track, i.e. decreases its vote count by one. +======= +% decrease votes in database + +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e devote(Artist, Title) -> F = fun() -> [Head|_] = ask(Artist, Title), @@ -137,7 +150,12 @@ devote(Artist, Title) -> end, mnesia:transaction(F). +<<<<<<< HEAD %resets the vote count of selected track to 0 (this is called when the song is played). +======= +% Reset votes to zero in database + +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e reset_votes(Artist, Title) -> F = fun() -> [Head|_] = ask(Artist, Title), @@ -146,28 +164,42 @@ reset_votes(Artist, Title) -> end, mnesia:transaction(F). +<<<<<<< HEAD %locks a song so it can't be played again or voted for for that time. +======= +% Lock a song + +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e lock(Artist, Title) -> F = fun() -> [Head|_] = ask(Artist, Title), - Votes = Head#track.lock = true, - New = Head#track{lock = Votes}, + New = Head#track{locked = true}, mnesia:write(New) end, mnesia:transaction(F). +<<<<<<< HEAD %unlocks a song so it can be played and voted for again. +======= +% Unlock a song... + +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e unlock(Artist, Title) -> F = fun() -> [Head|_] = ask(Artist, Title), - Votes = Head#track.lock = false, - New = Head#track{lock = Votes}, + New = Head#track{locked = false}, mnesia:write(New) end, mnesia:transaction(F). +<<<<<<< HEAD %locks a song and then waits for a specified amount of time before unlocking it again. %this is called when the song is played. +======= +% Lock a song if it was just played, after a Timeout it will be unlocked automaticly +% If all songs are locked, all will be unlocked. + +>>>>>>> 3ab5da8355e114953c154114aa4c0c36b719e92e lock_process(Artist, Title) -> lock(Artist, Title), receive |