aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2010-10-13 11:53:17 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2010-10-13 11:53:17 +0200
commit3b4edbdddf8af55f63c22bb3ba80fffa46156dd2 (patch)
tree1e0d949871832ab65809e17b0fabac1c55c45cce
parentc9243b1a59b5494f75485d02d4db13762b886136 (diff)
downloaderlang-3b4edbdddf8af55f63c22bb3ba80fffa46156dd2.tar.gz
erlang-3b4edbdddf8af55f63c22bb3ba80fffa46156dd2.tar.xz
erlang-3b4edbdddf8af55f63c22bb3ba80fffa46156dd2.zip
added some modules for server
-rw-r--r--server/client.erl15
-rw-r--r--server/dispatcher.erl27
-rw-r--r--server/server.erl45
3 files changed, 87 insertions, 0 deletions
diff --git a/server/client.erl b/server/client.erl
new file mode 100644
index 0000000..72658b7
--- /dev/null
+++ b/server/client.erl
@@ -0,0 +1,15 @@
+-module(client).
+-export([start/1, loop/1]).
+
+start(Client) ->
+ spawn(client, loop, Client).
+
+loop(Client) ->
+ receive
+ {list} ->
+ Client ! {ok, media ! {all}};
+
+ true ->
+ Client ! {error, unknown_command},
+ loop(Client)
+ end.
diff --git a/server/dispatcher.erl b/server/dispatcher.erl
new file mode 100644
index 0000000..86b2627
--- /dev/null
+++ b/server/dispatcher.erl
@@ -0,0 +1,27 @@
+-module(dispatcher).
+-export([start/0, init/0, handle/3]).
+
+start() ->
+ server:start(dis, dispatcher).
+
+init() ->
+ dict:new().
+
+handle(_, {register, {User, Password}}, Dict) ->
+ case dict:find(User) 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
+ {ok, Password} ->
+ {{ok, {logged_in, client:start(From)}}, Dict};
+ true ->
+ {{error, user_or_password_invalid}, Dict}
+ end;
+
+handle(_, _, Dict) ->
+ {{error, unknown_command}, Dict}.
diff --git a/server/server.erl b/server/server.erl
new file mode 100644
index 0000000..42b1822
--- /dev/null
+++ b/server/server.erl
@@ -0,0 +1,45 @@
+%% ---
+%% Excerpted from "Programming Erlang",
+%% published by The Pragmatic Bookshelf.
+%% Copyrights apply to this code. It may not be used to create training material,
+%% courses, books, articles, and the like. Contact us if you are in doubt.
+%% We make no guarantees that this code is fit for any purpose.
+%% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
+%%---
+-module(server).
+-export([start/2, rpc/2, swap_code/2]).
+
+start(Name, Mod) ->
+ register(Name, spawn(fun() -> loop(Name,Mod,Mod:init()) end)).
+
+swap_code(Name, Mod) -> rpc(Name, {swap_code, Mod}).
+
+rpc(Name, Request) ->
+ Name ! {self(), Request},
+ receive
+ {Name, crash} -> exit(rpc);
+ {Name, ok, Response} -> Response
+ end.
+
+loop(Name, Mod, OldState) ->
+ receive
+ {From, {swap_code, NewCallbackMod}} ->
+ From ! {Name, ok, ack},
+ loop(Name, NewCallbackMod, OldState);
+ {From, Request} ->
+ try Mod:handle(From, Request, OldState) of
+ {Response, NewState} ->
+ From ! {Name, ok, Response},
+ loop(Name, Mod, NewState)
+ catch
+ _: Why ->
+ log_the_error(Name, Request, Why),
+ From ! {Name, crash},
+ loop(Name, Mod, OldState)
+ end
+ end.
+
+log_the_error(Name, Request, Why) ->
+ io:format("Server ~p request ~p ~n"
+ "caused exception ~p~n",
+ [Name, Request, Why]).