Erlang basic general server debugger output interpretation -
i'm having problem erlang module. here 1 wrote:
-module(basic_gen_server). -export([start/1, call/2, cast/2]).
start(module) -> register(server, spawn(basic_gen_server,gen_server_loop,[module, module:init()])), server. call(pid,request) -> pid ! {call, self(), request}, receive reply -> reply end. cast(pid,request) -> pid ! {cast, self(), request}, receive _ -> ok end. gen_server_loop(module, currentstate) -> io:fwrite("gen_server_loop~n", []), receive {call, callpid, request} -> {reply, reply, newstate} = module:handle_call(request,self(),currentstate), callpid ! reply, gen_server_loop(module, newstate); {cast, castpid, request} -> {noreply, newstate} = module:handle_cast(request, currentstate), castpid ! noreply, gen_server_loop(module, newstate) end.
and here callback module defined:
% written caleb helbling % lastly updated oct 10, 2014 -module(name_server). -export([init/0, add/3, whereis/2, handle_cast/2, handle_call/3, handle_swap_code/1]). %% client routines add(serverpid, person, place) -> basic_gen_server:cast(serverpid, {add, person, place}). whereis(serverpid, person) -> basic_gen_server:call(serverpid, {whereis, person}). %% callback routines init() -> maps:new(). handle_cast({add, person, place}, state) -> newstate = maps:put(person, place, state), {noreply, newstate}. handle_call({whereis, person}, _from, state) -> reply = case maps:find(person, state) of {ok, place} -> place; error -> error end, newstate = state, {reply, reply, newstate}. handle_swap_code(state) -> {ok, state}.
upon trying initialize server next command:
myserver = basic_gen_server:start(name_server).
i next debug output:
=error report==== 29-oct-2014::12:41:42 === error in process <0.70.0> exit value: {undef,[{basic_gen_server,gen_server_loop,[name_server,#{}],[]}]}
conceptually, understand notion of making serial code basic server system, believe have syntax error haven't been able find using either syntax highlighting or google. in advance help!
function gen_server_loop
not exported. can not phone call basic_gen_server:gen_server_loop(module, module:init())
, happening within spawn(basic_gen_server,gen_server_loop,[module, module:init()])
.
if read error message tells function trying phone call in undefined (trougn undef
atom). function beingness {basic_gen_server,gen_server_loop,[name_server,#{}],[]}
, or have {module, function, listofargs, ...}
. should check
all local calls (like loop(someargs)
, without module specified) not compile if function not defined. , can local phone call dynamically (funtionname(someargs)
1 time again without module name).
edit after comment need of local calls.
you utilize lambda this. there spawn/1
funciton, takes lambda (or fun
if like), can phone call spawn( fun local_functino/0).
. issue fact fun
can not take arguments, there way around it, utilize of closures.
spawn(fun () -> gen_server_loop(module, module:init()) end).
and gen_serve_loop
stays local call.
erlang gen-server
No comments:
Post a Comment