aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--client/client.erl90
-rw-r--r--server/client.erl6
-rw-r--r--server/dispatcher.erl11
3 files changed, 89 insertions, 18 deletions
diff --git a/client/client.erl b/client/client.erl
index 9781b74..f8d5de2 100644
--- a/client/client.erl
+++ b/client/client.erl
@@ -1,8 +1,14 @@
-module(client).
--export([register/3, login/3, list/0, logged_in/1]).
+-export([register/3, login/3, list/0, handle/3]).
+
+buildNode(Server) ->
+ list_to_atom("distributed_music_system_main_node@" ++ Server).
+
+rpc(Server, Function, Params) ->
+ rpc:call(buildNode(Server), client, Function, Params).
register(Server, Name, Password) ->
- rpc:call(Server, client, register, [self(), {Name, Password}]),
+ rpc(Server, register, [self(), {Name, Password}]),
receive
{_, ok, Msg} ->
Msg;
@@ -11,21 +17,75 @@ register(Server, Name, Password) ->
end.
login(Server, Name, Password) ->
- rpc:call(Server, client, login, [self(), {Name, Password}]),
- receive
- {_, ok, {ok, {logged_in, Client}}} ->
- register(cli, spawn(client, logged_in, [Client]));
- {_, error, Msg} ->
- Msg
+ try checkLogin(false) of
+ _ ->
+ rpc(Server, login, [self(), {Name, Password}]),
+ receive
+ {_, ok, {ok, {logged_in, Client}}} ->
+ code:purge(server),
+ code:load_abs("../common/server"),
+ case server:start(cli, client, Client) of
+ true ->
+ {ok, logged_in};
+ _ ->
+ {error, unable_to_login}
+ end;
+ {_, error, Msg} ->
+ Msg;
+ Msg ->
+ Msg
+ end
+ catch
+ {error, login_state} ->
+ {error, logged_in}
end.
-list() ->
- cli ! {list}.
+contains([], _) ->
+ false;
+contains([H|_], H) ->
+ true;
+contains([_|T], Search) ->
+ contains(T, Search).
-logged_in(Client) ->
- receive
- {list} ->
- Client ! {list};
+checkLogin(Value) ->
+ checkLogin(Value, contains(registered(), cli)).
+checkLogin(Value, Value) ->
+ true;
+checkLogin(_, _) ->
+ throw({error, login_state}).
+
+list() ->
+ try checkLogin(true) of
_ ->
- logged_in(Client)
+ cli ! {self(), list},
+ receive
+ {_, ok, Msg} ->
+ Msg;
+ {_, error, Msg} ->
+ {error, Msg};
+ Msg ->
+ {error, Msg}
+ end
+ catch
+ {error, login_state} ->
+ {error, not_logged_in}
end.
+
+handle(Cli, list, Client) ->
+ Client ! {list},
+ receive
+ {_, {ok, List}} ->
+ io:format("List: ~w~n", List),
+ {{ok}, Client}
+ end;
+
+handle(Cli, {test, X}, Client) ->
+ Client ! {X},
+ receive
+ Msg ->
+ io:format("Test: ~w~n~n", [Msg]),
+ {{ok}, Client}
+ end;
+
+handle(_, Cmd, Client) ->
+ {{error, {unknown_command, Cmd}}, Client}.
diff --git a/server/client.erl b/server/client.erl
index 7139e67..9d778f0 100644
--- a/server/client.erl
+++ b/server/client.erl
@@ -2,12 +2,14 @@
-export([start/1, loop/1, register/2, login/2]).
start(Client) ->
- spawn(client, loop, Client).
+ process_flag(trap_exit, true),
+ spawn(client, loop, [Client]).
loop(Client) ->
receive
{list} ->
- Client ! {ok, media ! {all}};
+ Client ! {ok, foo},
+ loop(Client);
true ->
Client ! {error, unknown_command},
diff --git a/server/dispatcher.erl b/server/dispatcher.erl
index 4cef276..390f9e5 100644
--- a/server/dispatcher.erl
+++ b/server/dispatcher.erl
@@ -2,10 +2,18 @@
-export([start/0, init/0, handle/3]).
start() ->
- server:start(dis, dispatcher).
code:purge(server),
code:load_abs("../common/server"),
+ case server:start(dis, dispatcher) of
+ true ->
+ io:format("Server started... ~n"),
+ true;
+ _ ->
+ io:format("Error starting server! Exiting.~n"),
+ exit(1)
+ end.
+
init() ->
dict:new().
@@ -14,6 +22,7 @@ handle(_, {register, {User, Password}}, Dict) ->
{ok, _}->
{{error, duplicated_user}, Dict};
_ ->
+ io:format("User created: ~s~n", [User]),
{{ok, user_created}, dict:store(User, Password, Dict)}
end;