diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/client.erl | 25 | ||||
-rw-r--r-- | server/dispatcher.erl | 18 |
2 files changed, 30 insertions, 13 deletions
diff --git a/server/client.erl b/server/client.erl index 9d778f0..31d382f 100644 --- a/server/client.erl +++ b/server/client.erl @@ -1,23 +1,32 @@ -module(client). -export([start/1, loop/1, register/2, login/2]). -start(Client) -> +start(Node) -> process_flag(trap_exit, true), - spawn(client, loop, [Client]). + {Client, Ref} = server:start_on_node(Node, client, undef), + link(Client), + Server = spawn_link(client, loop, [{Client, Ref}]), + + case server:rpc({Client, Ref}, {change_state, Server}) of + {ok} -> + {ok, {Client, Ref}}; + _ -> + {error, unknown_error} + end. loop(Client) -> receive - {list} -> - Client ! {ok, foo}, + list -> + server:send(Client, {ok, {foo}}), loop(Client); - true -> - Client ! {error, unknown_command}, + Cmd -> + server:send(Client, {error, {unknown_command, Cmd}}), loop(Client) end. register(Client, {Name, Password}) -> dis ! {Client, {register, {Name, Password}}}. -login(Client, {Name, Password}) -> - dis ! {Client, {login, {Name, Password}}}. +login(Client, {Node, Name, Password}) -> + dis ! {Client, {login, {Node, Name, Password}}}. diff --git a/server/dispatcher.erl b/server/dispatcher.erl index 390f9e5..4f1f58b 100644 --- a/server/dispatcher.erl +++ b/server/dispatcher.erl @@ -1,5 +1,5 @@ -module(dispatcher). --export([start/0, init/0, handle/3]). +-export([start/0, init/0, handle/2]). start() -> code:purge(server), @@ -17,7 +17,7 @@ start() -> init() -> dict:new(). -handle(_, {register, {User, Password}}, Dict) -> +handle({register, {User, Password}}, Dict) -> case dict:find(User, Dict) of {ok, _}-> {{error, duplicated_user}, Dict}; @@ -26,13 +26,21 @@ handle(_, {register, {User, Password}}, Dict) -> {{ok, user_created}, dict:store(User, Password, Dict)} end; -handle(From, {login, {User, Password}}, Dict) -> +handle({login, {Node, User, Password}}, Dict) -> case dict:find(User, Dict) of {ok, Password} -> - {{ok, {logged_in, client:start(From)}}, Dict}; + case client:start(Node) of + {ok, Pid} -> + {{ok, {logged_in, Pid}}, Dict}; + _ -> + {{error, unable_to_login}, Dict} + end; _ -> {{error, user_or_password_invalid}, Dict} end; -handle(_, _, Dict) -> +handle({'EXIT', _, _}, Dict) -> + Dict; + +handle(_, Dict) -> {{error, unknown_command}, Dict}. |