Erlang OTP管理员在启动时退出我的聊天应用程序

a0zr77ik  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(185)

我开发了一个聊天应用程序,它的服务器是“message_server”。这是一个分布式应用程序,我可以在多个节点上产生多个聊天用户,只要给每个聊天用户一个原子名称。两个聊天用户之间的消息传递是通过向聊天用户的节点发送消息来完成的。我不能用管理员启动这个服务器。如何解决这个问题。我需要管理员来管理聊天用户。

消息_服务器代码

-module(message_server).

-behaviour(gen_server).

-export([send_message/2, receive_message/3, start_link/1, stop/0, recv_msg/1, readyto_send/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
  code_change/3]).

-define(SERVER, ?MODULE).

-record(message_server_state, {from, to, msgsent, msgreceived}).
%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================

start_link(Name) ->
  gen_server:start_link({local, ?MODULE}, ?MODULE, Name, []).

init(Name) ->
  io:format("~p is connected with the server...~n", [Name]),
  {ok, #message_server_state{
    from = atom_to_list(node()),
    to = [],
    msgsent = [],
    msgreceived = []
  }}.
stop() ->
  gen_server:stop(?MODULE).


send_message(To, Msg) ->
  gen_server:call({?MODULE, node()}, {send, To, Msg}).

receive_message(Sender, To, Msg) ->
  gen_server:call({?MODULE, list_to_atom(To)}, {reciv, Sender, Msg}).

recv_msg(Msg)->
  gen_server:call({?MODULE, node()}, {recive, Msg}).

readyto_send(To)->
  gen_server:call({?MODULE, list_to_atom(To)}, {ready, To}).

handle_call({send, To, Msg}, _From, State = #message_server_state{to = Receivers, msgsent = Msgsent, from = Sender}) ->
  io:format("Sent message: ~p~n", [Msg]),
%%  chat_fsm:message_send(Msg, To),--toDo add later--
  receive_message(Sender, To, Msg),
  {reply, ok, State#message_server_state{msgsent = [Msg | Msgsent], to = [To | Receivers]}};

handle_call({reciv, Sender, Msg}, _From, State = #message_server_state{msgreceived = Msgreceived}) ->
  io:format("Sent by: ~p~n", [Sender]),
  io:format("Message: ~p~n", [Msg]),
  M = database_server:getalldb(Sender),
  io:format("SENDER-DETAILS>>: ~p~n",[M]),
  {reply, ok, State#message_server_state{msgreceived = [Msg | Msgreceived]}};

handle_call({recive, Msg}, _From, State = #message_server_state{msgreceived = Msgreceived}) ->
  io:format("Message: ~p~n", [Msg]),
  {reply, ok, State#message_server_state{msgreceived = [Msg | Msgreceived]}}.
%%--toDo add later--
%%handle_call({ready, To}, _From, State) ->%%  chat_fsm:ready_to_send(To),
%%  {reply, ok, State}.

handle_cast(_Request, State = #message_server_state{}) ->
  {noreply, State}.

handle_info(_Info, State = #message_server_state{}) ->
  {noreply, State}.

terminate(_Reason, _State = #message_server_state{}) ->
  ok.

code_change(_OldVsn, State = #message_server_state{}, _Extra) ->
  {ok, State}.

聊天_sup主管

-module(chat_sup).

-behaviour(supervisor).

-export([start_link/0, init/1, start_link_shell2/0]).

start_link_shell2()->
  {ok, Pid2}= supervisor:start_link({global,?MODULE}, ?MODULE,[]),
  unlink(Pid2).

start_link() ->
  supervisor:start_link({global, ?MODULE}, ?MODULE, []).

init([]) ->AChild = #{id => 'message_server',
    start => {'message_server', start_link, []},
    restart => permanent,
    shutdown => infinity,
    type => worker,
    modules => ['message_server']},

  {ok, {#{strategy => one_for_all,
    intensity => 5,
    period => 30},
    [AChild]}
  }.

启动Supervisor时出现此错误

(john@DESKTOP-RD414DV)5> chat_sup:start_link_shell2().
=SUPERVISOR REPORT==== 21-Sep-2022::05:49:46.626000 ===
    supervisor: {global,chat_sup}
    errorContext: start_error
    reason: {'EXIT',
                {undef,
                    [{message_server,start_link,[],[]},
                     {supervisor,do_start_child_i,3,
                         [{file,"supervisor.erl"},{line,414}]},
                     {supervisor,do_start_child,2,
                         [{file,"supervisor.erl"},{line,400}]},
                     {supervisor,'-start_children/2-fun-0-',3,
                         [{file,"supervisor.erl"},{line,384}]},
                     {supervisor,children_map,4,
                         [{file,"supervisor.erl"},{line,1250}]},
                     {supervisor,init_children,2,
                         [{file,"supervisor.erl"},{line,350}]},
                     {gen_server,init_it,2,
                         [{file,"gen_server.erl"},{line,848}]},
                     {gen_server,init_it,6,
                         [{file,"gen_server.erl"},{line,811}]}]}}
    offender: [{pid,undefined},
               {id,message_server},
               {mfargs,{message_server,start_link,[]}},
               {restart_type,permanent},
               {significant,false},
               {shutdown,infinity},
               {child_type,worker}]


** exception exit: {shutdown,
                        {supervisor,'-start_children/2-fun-0-',3,
                         [{file,"supervisor.erl"},{line,384}]},
                        {supervisor,children_map,4,
                         [{file,"supervisor.erl"},{line,1250}]},
                        {supervisor,init_children,2,
                         [{file,"supervisor.erl"},{line,350}]},
                        {gen_server,init_it,2,
                         [{file,"gen_server.erl"},{line,848}]},
                        {gen_server,init_it,6,
                         [{file,"gen_server.erl"},{line,811}]}]}}}}
mkshixfv

mkshixfv1#

start mfa(message_server:start_link/0)与message_server:start_link/1签名不匹配

- start => {'message_server', start_link, []}
+ start => {'message_server', start_link, ['message_server_name']}

或者将message_server:start_link/0实现为

start_link() ->
  gen_server:start_link({local, ?MODULE}, ?MODULE, Name, [?MODULE]).

相关问题