上一篇中写了关于node节点的,本文是关于holl节点的分析,holl节点主要是用来处理node节点发来的消息,来完成注册,登陆一类的请求。
节点之间的通信是用gen_server:call()和gen_server:cast()的方法来实现的。
gen_server:call()和gen_server:cast()的方法是前面的方法需要等待返回,而后面的方法不需要等待返回。
下面是holl节点的实现
%% @author cb1187
%% @doc @todo Add description to opt_holl.
-module(opt_holl).
%% ====================================================================
%% API functions
%% ====================================================================
-behaviour(gen_server).
-export([]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-compile(export_all).
-define(Server_Holl,holl).
-define(Node_Node,node@simsunny).
-define(Server_Node,node).
-define(Node_Chat,chat@simsunny).
-define(Server_Chat,chat).
%% ====================================================================
%% Internal functions
%% ====================================================================
s() -> gen_server:start_link({local,?Server_Holl}, ?MODULE, [], []).
stop() -> gen_server:call(?Server_Holl, stop).
init([])->
db:start(),
{ok,[]}.
%%registser
handle_call({register,{user,Name},{psw,Psw},{socket,Socket},{server_Name,Server_name}},_From,Tab)->
io:format(" receive msg(register)------~n name:~p~n paddword:~p~n",[Name,Psw]),
Result=db:register(Name, Psw),
case Result of
{user_is_already}->
Request={reigister,user_is_already,{socket,Socket}},
gen_server:cast({Server_name,?Node_Node}, Request);
{register_success}->
Request_node={reigister,register_success,{{user,Name},{psw,Psw},{socket,Socket}}},
Request_chat={reigister,register_success,{{user,Name},{socket,Socket}}},
gen_server:call({?Server_Chat,?Node_Chat}, Request_chat),
gen_server:cast({Server_name,?Node_Node}, Request_node),
io:format("~p is enter ~n", [Name])
end,
{reply, ok, Tab};
%%login
handle_call({login,{user,Name},{psw,Psw},{socket,Socket},{server_Name,Server_name}}, _From,Tab)->
io:format(" receive msg(login)------~n name:~p~n paddword:~p~n",[Name,Psw]),
Result=db:login(Name, Psw),
case Result of
{no_users}->
Request={login,no_urers,{socket,Socket}},
gen_server:cast({Server_name,?Node_Node}, Request);
{psw_wrong}->
Request={login,psw_wrong,{socket,Socket}},
gen_server:cast({Server_name,?Node_Node}, Request);
{login_success}->
Request_node={login,success,{{user,Name},{psw,Psw},{socket,Socket}}},
Request_chat={login,success,{{user,Name},{scoket,Socket}}},
gen_server:call({?Server_Chat,?Node_Chat}, Request_chat),
%%send to chat node
gen_server:cast({Server_name,?Node_Node}, Request_node),
io:format("~p is enter ~n", [Name])
end,
{reply, ok, Tab};
%%quite
handle_call({quite,{name,Name}}, _From,Tab)->
io:format(" receive msg(login)------~n name:~p~n ",[Name]),
io:format("~p is quite ~n", [Name]),
{reply, ok, Tab};
handle_call(stop, _From, Tab) ->
{stop, normal, stopped, Tab}.
handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State,_Extra) -> {ok, State}.
解释:
holl的节点只是接收从node节点发来的消息,然后匹配消息并且处理匹配的消息,在gen_server:cast给node节点,node节点把消息发给客户端
node节点发送消息的代码如下(以注册为例子):
- {register,{user,Name},{psw,Psw}}->
- Request={register,{user,Name},{psw,Psw},{socket,Socket},{server_Name,Server_name}},
- gen_server:call({?Server_Holl,?Node_Holl}, Request);
%%registser
handle_call({register,{user,Name},{psw,Psw},{socket,Socket},{server_Name,Server_name}},_From,Tab)->
io:format(" receive msg(register)------~n name:~p~n paddword:~p~n",[Name,Psw]),
Result=db:register(Name, Psw),
case Result of
{user_is_already}->
Request={reigister,user_is_already,{socket,Socket}},
gen_server:cast({Server_name,?Node_Node}, Request);
{register_success}->
Request_node={reigister,register_success,{{user,Name},{psw,Psw},{socket,Socket}}},
Request_chat={reigister,register_success,{{user,Name},{socket,Socket}}},
gen_server:call({?Server_Chat,?Node_Chat}, Request_chat),
gen_server:cast({Server_name,?Node_Node}, Request_node),
io:format("~p is enter ~n", [Name])
end,
{reply, ok, Tab};
欢迎转载,请注明出处
关于注册和登陆的模块在这里就不贴出了,在源码里给大家的~~