标签:
作为gen_server启动
初始化时创建s2s(mnesia)表,保存的是服务器之间的联系信息,server和server之间的路由
主要为do_route(From, To, Packet)方法
do_route(From, To, Packet) -> ?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket " "~P~n", [From, To, Packet, 8]), case find_connection(From, To) of {atomic, Pid} when is_pid(Pid) -> ?DEBUG("sending to process ~p~n", [Pid]), #xmlel{name = Name, attrs = Attrs, children = Els} = Packet, NewAttrs = jlib:replace_from_to_attrs(jid:to_string(From), jid:to_string(To), Attrs), #jid{lserver = MyServer} = From, ejabberd_hooks:run(s2s_send_packet, MyServer, [From, To, Packet]), send_element(Pid, #xmlel{name = Name, attrs = NewAttrs, children = Els}), ok; {aborted, _Reason} -> case xml:get_tag_attr_s(<<"type">>, Packet) of <<"error">> -> ok; <<"result">> -> ok; _ -> Err = jlib:make_error_reply(Packet, ?ERR_SERVICE_UNAVAILABLE), ejabberd_router:route(To, From, Err) end, false end.
首先根据from,to在s2s里面查找是否存在路由find_connection(From, To)
如找到则返回存在的pid
如果没找到则利用open_several_connections函数创建:
启动一个ejabberd_s2s_out进程去建立连接,并把建立的信息和pid保存到s2s里面,方便下次路由用到
其中ejabberd_s2s_out作为ejabberd_s2s_out_sup的子进程,连接到别的节点开启的ejabberd_s2s_in 服务(默认开启三个监听接口ejabberd_c2s、ejabberd_s2s_in、ejabberd_http)
标签:
原文地址:http://www.cnblogs.com/lawen/p/5052563.html