Download:
child 33:654a2f3e9ccf
parent 31:72639365f60a
32:e528c326cf10
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 15:09:55 +0900
Make rps_nickserv a gen_server.

3 файлов изменено, 30 вставок(+), 19 удалений(-) [+]
src/rps.erl file | annotate | diff | comparison | revisions
src/rps_bots.erl file | annotate | diff | comparison | revisions
src/rps_nickserv.erl file | annotate | diff | comparison | revisions
--- a/src/rps.erl Sun Mar 02 22:41:13 2014 +0900
+++ b/src/rps.erl Mon Mar 03 15:09:55 2014 +0900
@@ -6,7 +6,7 @@
-define(BOTS, [fool, copycat, gambler]).
go() ->
- register(nickserv, spawn_link(rps_nickserv, nickserv, [])),
+ rps_nickserv:start_link(),
register(lobby, spawn_link(?MODULE, lobby, [])),
register(counter, spawn_link(?MODULE, counter, [])),
register(printer, spawn_link(?MODULE, printer, [])),
--- a/src/rps_bots.erl Sun Mar 02 22:41:13 2014 +0900
+++ b/src/rps_bots.erl Mon Mar 03 15:09:55 2014 +0900
@@ -3,7 +3,7 @@
-export([fool/0, copycat/0, gambler/0]).
fool() ->
- nickserv ! {nick, self(), "fool"},
+ rps_nickserv:nick(self(), "fool"),
lobby ! {join, self()},
fool(joined).
@@ -25,7 +25,7 @@
end.
copycat() ->
- nickserv ! {nick, self(), "copycat"},
+ rps_nickserv:nick(self(), "copycat"),
lobby ! {join, self()},
copycat(joined).
@@ -45,7 +45,7 @@
end.
gambler() ->
- nickserv ! {nick, self(), "gambler"},
+ rps_nickserv:nick(self(), "gambler"),
lobby ! {join, self()},
gambler(joined).
--- a/src/rps_nickserv.erl Sun Mar 02 22:41:13 2014 +0900
+++ b/src/rps_nickserv.erl Mon Mar 03 15:09:55 2014 +0900
@@ -1,20 +1,31 @@
-module(rps_nickserv).
--export([nickserv/0, whois/1]).
+-behaviour(gen_server).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+-export([start_link/0, nick/2, whois/1]).
-nickserv() ->
- nickserv(dict:new()).
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+init([]) ->
+ {ok, dict:new()}.
-nickserv(Nicknames) ->
- receive
- {nick, Who, Nickname} ->
- nickserv(dict:store(Who, Nickname, Nicknames));
- {whois, Pid, Who} ->
- Pid ! {ok, dict:fetch(Who, Nicknames)},
- nickserv(Nicknames)
- end.
+handle_call({nick, Who, Nickname}, _From, Nicknames) ->
+ io:format("~w registered as ~s.~n", [Who, Nickname]),
+ {reply, ok, dict:store(Who, Nickname, Nicknames)};
+
+handle_call({whois, Who}, _From, Nicknames) ->
+ {reply, dict:fetch(Who, Nicknames), Nicknames}.
+
+handle_cast(_Msg, State) -> {noreply, State}.
+
+handle_info(_Msg, State) -> {noreply, State}.
+
+terminate(_Reason, _State) -> ok.
+
+code_change(_OldVersion, State, _Extra) -> {ok, State}.
+
+nick(Who, Nickname) ->
+ gen_server:call(?MODULE, {nick, Who, Nickname}).
whois(Who) ->
- nickserv ! {whois, self(), Who},
- receive
- {ok, Nickname} -> Nickname
- end.
+ gen_server:call(?MODULE, {whois, Who}).