aboutsummaryrefslogtreecommitdiffstats
path: root/server/dispatcher.erl
blob: 4f1f58b1ee1b8fd1523cae1cadd653fe833de561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-module(dispatcher).
-export([start/0, init/0, handle/2]).

start() ->
    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().

handle({register, {User, Password}}, Dict) ->
    case dict:find(User, Dict) of
	{ok, _}->
	    {{error, duplicated_user}, Dict};
	_ ->
	    io:format("User created: ~s~n", [User]),
	    {{ok, user_created}, dict:store(User, Password, Dict)}
    end;

handle({login, {Node, User, Password}}, Dict) ->
    case dict:find(User, Dict) of
	{ok, Password} ->
	    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({'EXIT', _, _}, Dict) ->
    Dict;

handle(_, Dict) ->
    {{error, unknown_command}, Dict}.