【发布时间】:2014-04-12 23:31:44
【问题描述】:
我有一个简单的服务器(如下)。我可以使用cell_tracer:start_link() 创建服务器,这很有效;我知道这一点,因为当我重复同一个电话时,我收到一个already_started 错误。问题是在服务器创建后,使用cell_tracker:get_cells()(或执行任何gen_server:call(...))会导致服务器退出并出现如下异常:
** exception exit: {noproc,{gen_server,call,[cell_tracker,get_cells]}}
in function gen_server:call/2 (gen_server.erl, line 180)
我可以直接调用handle_call(get_cells...),我得到了预期的结果。
我不确定这里发生了什么。它没有给我太多的信息来处理,我没有看到问题。我该如何进一步深入研究?
-module(cell_tracker).
-behavior(gen_server).
-export([start_link/0, stop/0]).
-export([add_cell/1, del_cell/1, get_cells/0]).
-export([init/1,
handle_cast/2,
handle_call/3,
terminate/2,
handle_info/2,
code_change/3]).
%% operational api
start_link() ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
stop() ->
gen_server:cast(?MODULE, stop).
%% traking api
add_cell(Cell) ->
gen_server:call(?MODULE, { add_cell, Cell }).
del_cell(Cell) ->
gen_server:call(?MODULE, { del_cell, Cell }).
get_cells() ->
gen_server:call(?MODULE, get_cells).
%% gen_server callbacks
init(Coords) ->
{ok, Coords}.
handle_cast(stop, Coords) ->
{stop, normal, Coords}.
handle_call({add_cell, Cell}, _From, Cells) ->
{reply, ok, [Cell|Cells]};
handle_call({del_cell, Cell}, _From, Cells) ->
{reply, ok, Cells -- Cell};
handle_call(get_cells, _From, Cells) ->
{reply, Cells, Cells}.
terminate(unnormal, _State) -> ok.
handle_info(_,_) -> ok.
code_change(_,State,_) -> {ok, State}.
【问题讨论】:
标签: erlang erlang-otp