diff options
author | Marco Ziener <mziener@lavabit.com> | 2010-10-13 12:01:52 +0200 |
---|---|---|
committer | Marco Ziener <mziener@lavabit.com> | 2010-10-13 12:01:52 +0200 |
commit | c99c47da34ef134d226c074c30e87319d74e450c (patch) | |
tree | cf3c03eb68d45abf95a1c3017e5a3bf5fa31ca42 /server | |
parent | 68f420b2468d4881c66548321c46307f7ce5c5fc (diff) | |
parent | 3b4edbdddf8af55f63c22bb3ba80fffa46156dd2 (diff) | |
download | erlang-c99c47da34ef134d226c074c30e87319d74e450c.tar.gz erlang-c99c47da34ef134d226c074c30e87319d74e450c.tar.xz erlang-c99c47da34ef134d226c074c30e87319d74e450c.zip |
Merge branch 'master' of ssh://git.animux.de/erlang
Diffstat (limited to '')
-rw-r--r-- | server/Makefile | 16 | ||||
-rw-r--r-- | server/client.erl | 15 | ||||
-rw-r--r-- | server/dispatcher.erl | 27 | ||||
-rw-r--r-- | server/id3v2.erl (renamed from id3v2.erl) | 0 | ||||
-rw-r--r-- | server/media.erl (renamed from media.erl) | 0 | ||||
-rw-r--r-- | server/server.erl | 45 |
6 files changed, 103 insertions, 0 deletions
diff --git a/server/Makefile b/server/Makefile new file mode 100644 index 0000000..f447307 --- /dev/null +++ b/server/Makefile @@ -0,0 +1,16 @@ +SRC=$(wildcard *.erl) +BIN=$(SRC:.erl=.beam) + +.PHONY: all clean + +####################### + +all: $(BIN) + +%.beam: %.erl + erlc $< + +####################### + +clean: + -$(RM) $(BIN) 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/id3v2.erl b/server/id3v2.erl index fc49183..fc49183 100644 --- a/id3v2.erl +++ b/server/id3v2.erl diff --git a/media.erl b/server/media.erl index 644d533..644d533 100644 --- a/media.erl +++ b/server/media.erl 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]). |