다음은 http://learnyousomeerlang.com/static/erlang/kitty_gen_server.erl입니다.gen_server 캐스트의 상태 반환
내 응용 프로그램 논리가 temple.erl 안에 있습니다. 이 코드는 모두 &이 예상대로 실행됩니다. 내 land.erl은 템플이 들어있는 서버를위한 것입니다.
gen_server를 사용하여 메시지 전달을 추상화하고 최소한의 오버 헤드로 상태를 추적 할 수 있다는 점을 이해합니다 (모르는 부분을 수정하십시오).
나는 나의 init 함수
init([]) -> {ok, temple:new()}.
에서 두 번째로 tuplevalue 내 서버의 상태 이해 - temple:new()
의 경우, 반환 값.
그래서 I c(land)
입니다. (코드 아래)이 시도 :
19> {ok, Pid2} = land:start_link().
{ok,<0.108.0>}
20> land:join(Pid2, a).
ok
을 내가 코드를 읽고 kitty_gen_server을 실행하는 내 경험을 비교에서 조인 메시지를 보낼 때 난 그냥 확인을 다시 원자를 얻을, 나는 상태가 제대로 업데이트됩니다 생각 값 temple:join(Temple, Name)
하지만 확인 원자 나는 사원 내 상태를 업데이트 할 수있는 방법을
handle_call({join, Name}, _From, Temple) ->
{reply, ok, temple:join(Temple, Name)};
로부터의 응답 값입니다 (사원, 이름)에 가입하고 클라이언트에이 값을 돌려줍니다? 나는 같은 기능을 두 번 호출하고 싶지 않다.
는handle_call({join, Name}, _From, Temple) ->
{reply, temple:join(Temple, Name), temple:join(Temple, Name)};
는
는 그래서 kitty_gen_server보고 내가
handle_call({join, Name}, _From, Temple) ->
[{reply, JoinedTemple, JoinedTemple} || JoinedTemple <- temple:join(Temple, Name)];
을 시도하고 내가 구문 오류에 대한 메시지와 함께이 작업을 할 때 내가 || 함수 절 충돌을 얻고, 나는이 볼 만 목록 입니다 내시경
temple:join(Temple, Name)]
의 값을 계산하고 토지 호출자에게 돌아가려면 어떻게해야합니까? 토지의 상태에 가입하고 업데이트 하시겠습니까?
-module(land).
-behaviour(gen_server).
-export([start_link/0, join/2, input/3, fight/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
start_link() ->
gen_server:start_link(?MODULE, [], []).
join(Pid, Name) ->
gen_server:call(Pid, {join, Name}).
input(Pid, Action, Target) ->
gen_server:call(Pid, {input, Action, Target}).
fight(Pid) ->
gen_server:call(Pid, fight).
init([]) -> {ok, temple:new()}.
handle_call({join, Name}, _From) ->
{reply, ok, temple:join(Name)}.
handle_call({join, Name}, _From, Temple) ->
{reply, temple:join(Temple, Name), temple:join(Temple, Name)};
handle_call(terminate, _From, Temple) ->
{stop, normal, ok, Temple}.
handle_info(Msg, Temple) ->
io:format("Unexpected message: ~p~n",[Msg]),
{noreply, Temple }.
terminate(normal, Temple) ->
io:format("Temple bathed in blood.~p~n", [Temple]),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
handle_cast(_, Temple) ->
{noreply, Temple}.
찰흙을 물론! 고맙습니다! – quantumpotato