From 40d28960b17bfe6b09fabdcfc0555c9db9cd5971 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 14 Oct 2010 11:05:09 +0200 Subject: fixed errors --- client/client.erl | 72 ++++++++++++++++++++++++++++++++++++++----------------- server/client.erl | 25 ++++++++++--------- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/client/client.erl b/client/client.erl index 67be4ee..aab0b4e 100644 --- a/client/client.erl +++ b/client/client.erl @@ -1,5 +1,5 @@ -module(client). --export([register/3, login/3, list/0, handle/2]). +-export([register/3, login/3, list/0, handle/2, getVotes/0, vote/2, devote/2]). contains([], _) -> false; @@ -67,6 +67,33 @@ handle(list, Server) -> {Msg, Server} end; +handle(get_votes, Server) -> + Server ! get_votes, + receive + {ok, Msg} -> + {Msg, Server}; + Msg -> + {Msg, Server} + end; + +handle({vote, Artist, Title}, Server) -> + Server ! {vote, Artist, Title}, + receive + {ok, Msg} -> + {Msg, Server}; + Msg -> + {Msg, Server} + end; + +handle({devote, Artist, Title}, Server) -> + Server ! {vote, Artist, Title}, + receive + {ok, Msg} -> + {Msg, Server}; + Msg -> + {Msg, Server} + end; + handle({change_state, NewState}, _) -> {{ok}, NewState}; @@ -74,31 +101,32 @@ handle(Cmd, Server) -> {{error, {unknown_command, Cmd}}, Server}. %queries the server for the current votes this client possesses -getVotes(Server) -> - rpc(Server, getVotes, self()), - receive - Msg -> - Msg - end. +getVotes() -> + try checkLogin(true) of + _ -> + server:rpc(cli, get_votes) + catch + {error, login_state} -> + {error, not_logged_in} + end. %positive vote, increments the votes for {Artist, Title} by one vote(Artist, Title) -> - rpc(Server, vote, [Artist, Title]), - receive - {_, ok, Msg} -> - Msg; - {_, error, Msg} -> - {error, Msg} - end. + try checkLogin(true) of + _ -> + server:rpc(cli, {vote, Artist, Title}) + catch + {error, login_state} -> + {error, not_logged_in} + end. %negative vote, decrements the votes for {Artist, Title} by one devote(Artist, Title) -> - rpc(Server, devote, [Artist, Title]), - receive - {_, ok, Msg} -> - Msg; - {_, error, Msg} -> - {error, Msg} - end. - + try checkLogin(true) of + _ -> + server:rpc(cli, {devote, Artist, Title}) + catch + {error, login_state} -> + {error, not_logged_in} + end. diff --git a/server/client.erl b/server/client.erl index bf8bc22..ce7c99b 100644 --- a/server/client.erl +++ b/server/client.erl @@ -20,6 +20,18 @@ loop(Client) -> Client ! {ok, {foo}}, loop(Client); + get_votes -> + Client ! {ok, media:getVotes(Client)}, + loop(Client); + + {vote, Artist, Title} -> + Client ! media:vote(Artist, Title), + loop(Client); + + {devote, Artist, Title} -> + Client ! media:devote(Artist, Title), + loop(Client); + Cmd -> Client ! {error, {unknown_command, Cmd}}, loop(Client) @@ -30,16 +42,3 @@ register(Client, {Name, Password}) -> login(Client, {Node, Name, Password}) -> dis ! {Client, {login, {Node, Name, Password}}}. - -getVotes() -> - media:getVotes(self()), - receive - Msg -> - Client ! Msg - end. - -vote(Artist,Title) -> - media:vote(Artist, Title). - -devote(Artist,Title) -> - media:devote(Artist, Title). -- cgit v1.2.3