gen_server:以新状态呼叫

问题描述 投票:3回答:1

模块调用gen_server来处理流,该流使用记录作为状态。

[handle_call使用State中的函数处理流,该函数将完整的数据和尾部分开,

现在,下一次,应该在模块发送更多数据之前,先给尾巴喂入,但尾巴要更新State

handle_call({stream, Data}, _From, State = #mystate{myfun=Fun}) ->
    case Fun(Data) of
       {completed piece,tail} -> 
           dosomethingwithpieace,
           NewState = State##mystate{myfun=resetfun()};
           % How do i call this again to feed Tail first with new state?
       {stillstreaming, Currentstate} ->
           NewState = State##mystate{myfun=CurrentState};

我无法调用gen_server:call(self(),{stream, Tail}),因为需要先更新State。而且我无法用新的State进行答复,因为模块将发送更多数据,并且尾部将消失。

是否有一种方法可以用更新的State再次调用它,而无需用模块的尾巴答复和反馈尾巴?

更新,代码:

% caller module
case gen_tcp:recv(Socket, 0) of % cannot set Length as it will block untill it is done reading Length number of bytes 
    {ok, Data} ->
        Response = gen_server:call(Pid, {handle_init,Socket,Data}),
        case Response of
            {ok, continue} ->
                pre_loop(Socket, Pid);
            {ok, logged_in} ->
                {UserId, UserName} = get_user_data(), % ignore it for now.
                receiver_loop(UserId, UserName, Socket, Pid);
            {stop, Reason} ->
                io:format("Error at pre_loop: ~p~n", [Reason]);
            _ ->
                io:format("Unknown response from call in pre-loop: ~p~n", [Response])
        end;
    {error, closed} -> % done here as no data was stored in mnesia yet.
        gen_server:stop(Pid),
        io:format("Client died in pre_loop~n")
end.

和gen_server模块:

% gen_server module
handle_call({handle_init, _Socket, Data}, _From, State = #server_state{data_fun =  {incomplete, Fun}}) ->
    case catch Fun(Data) of
        {incomplete, F} ->
            NewState = State#server_state{data_fun = {incomplete, F}},
            {reply, {ok, continue}, NewState};
        {with_tail, Term, Tail} ->
            % handle Term login/register only
            case handle_input_init(Term, Tail) of
                {incomplete, Fn, logged_in} ->
                    NewState = State#server_state{data_fun = {incomplete, Fn}},
                    {reply, {ok, logged_in}, NewState};
                {incomplete, Fn} ->
                    NewState = State#server_state{data_fun = {incomplete, Fn}},
                    {reply, {ok, continue}, NewState};
                {stop, malformed_data} ->
                    {reply, {stop, malformed_data}, State}
            end;
        _ ->
            {reply, {stop, malformed_data}, State}
    end;

handle_call(_Message, _From, State = #server_state{}) ->
{reply, {stop , unknown_call}, State}.

handle_input_init(Term, Tail) ->
case handle_term_init(Term) of
    {ok, login_passed} ->
        io:format("send user a login pass msg"),
        handle_tail_init(Tail, logged_in);
    {error, login_failed} ->
        io:format("send user a login failed error~n"),
        handle_tail_init(Tail);
    {ok, registration_passed} ->
        io:format("send user a registeration passed msg"),
        handle_tail_init(Tail);
    {error, registration_failed} ->
        io:format("send user a registeration failed error"),
        handle_tail_init(Tail);
    {error, invalidreq} ->
        io:format("send user an invalid requst error~n"),
        handle_tail_init(Tail)
end.

handle_tail_init(Tail) ->
case catch jsx:decode(Tail, [stream, return_tail, return_maps]) of
    {incomplete, F} ->
        {incomplete, F};
    {with_tail, Term, Tail2} ->
        handle_input_init(Term, Tail2);
    _ ->
        {stop, malformed_data}
end.

handle_tail_init(Tail, logged_in) -> % because it was logged in already, any further requests should be ignored
case catch jsx:decode(Tail, [stream, return_tail, return_maps]) of
    {incomplete, F} ->
        {incomplete, F, logged_in};
    {with_tail, _Term, Tail2} ->
        io:format("send user an invalid requst error~n"),
        handle_tail_init(Tail2, logged_in);
    _ ->
        {stop, malformed_data}
end.

handle_term_init(Term) ->
case Term of
    #{<<"Login">> := [UserName,Password]} ->
        login_user(UserName,Password);
    #{<<"Register">> := [UserName,Password]} ->
        register_user(UserName,Password);
    _ ->
        {error, invalidreq}
end.

它按预期工作,但这是我有史以来第一个Erlang代码,我很肯定可以将其简化为一个recursive handle_call,并保持OTP风格,这是我选择Erlang的原因。

erlang otp gen-server
1个回答
3
投票

我无法调用gen_server:call(self(),{stream,Tail}),因为State需要先更新。

我无法真正理解您要说的话,但是如果您的意思是:

我不能递归调用gen_server:call(self(),{stream, Tail}),即我无法在handle:call()中编写递归调用handle_call()的代码。

然后,您当然可以将handle:call()中的所有数据发送到另一个递归调用自身的函数:

handle_call(...) ->

    ...
    NewState = ....
    Result = ...
    {FinalResult, FinalState} = helper_func(Result, NewState, Tail)
    {reply, FinalResult, FinalState}

helper_func(Result, State, []) ->
    {Result, State};
helper_func(Result, State, [Piece|Tail]) ->
    ...
    NewState = ...
    NewResult = ...
    helper_func(NewResult, NewState, Tail).  %% Recursive call here
© www.soinside.com 2019 - 2024. All rights reserved.