aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--client/client.erl31
-rw-r--r--server/client.erl8
-rw-r--r--server/dispatcher.erl10
3 files changed, 43 insertions, 6 deletions
diff --git a/client/client.erl b/client/client.erl
new file mode 100644
index 0000000..9781b74
--- /dev/null
+++ b/client/client.erl
@@ -0,0 +1,31 @@
+-module(client).
+-export([register/3, login/3, list/0, logged_in/1]).
+
+register(Server, Name, Password) ->
+ rpc:call(Server, client, register, [self(), {Name, Password}]),
+ receive
+ {_, ok, Msg} ->
+ Msg;
+ {_, error, Msg} ->
+ Msg
+ 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
+ end.
+
+list() ->
+ cli ! {list}.
+
+logged_in(Client) ->
+ receive
+ {list} ->
+ Client ! {list};
+ _ ->
+ logged_in(Client)
+ end.
diff --git a/server/client.erl b/server/client.erl
index 72658b7..7139e67 100644
--- a/server/client.erl
+++ b/server/client.erl
@@ -1,5 +1,5 @@
-module(client).
--export([start/1, loop/1]).
+-export([start/1, loop/1, register/2, login/2]).
start(Client) ->
spawn(client, loop, Client).
@@ -13,3 +13,9 @@ loop(Client) ->
Client ! {error, unknown_command},
loop(Client)
end.
+
+register(Client, {Name, Password}) ->
+ dis ! {Client, {register, {Name, Password}}}.
+
+login(Client, {Name, Password}) ->
+ dis ! {Client, {login, {Name, Password}}}.
diff --git a/server/dispatcher.erl b/server/dispatcher.erl
index 86b2627..a296c78 100644
--- a/server/dispatcher.erl
+++ b/server/dispatcher.erl
@@ -8,18 +8,18 @@ init() ->
dict:new().
handle(_, {register, {User, Password}}, Dict) ->
- case dict:find(User) of
- {ok, _} ->
+ case dict:find(User, Dict) of
+ {ok, _}->
{{error, duplicated_user}, Dict};
- true ->
+ _ ->
{{ok, user_created}, dict:store(User, Password, Dict)}
end;
handle(From, {login, {User, Password}}, Dict) ->
- case dict:find(User) of
+ case dict:find(User, Dict) of
{ok, Password} ->
{{ok, {logged_in, client:start(From)}}, Dict};
- true ->
+ _ ->
{{error, user_or_password_invalid}, Dict}
end;