Skip to content

Commit

Permalink
18/n Better callback handling in erlang_service_server
Browse files Browse the repository at this point in the history
Summary: Make callback processing in erlang_service_server generic, push encoding/decoding to calling functions in the API.

Reviewed By: michalmuskala

Differential Revision: D60597132

fbshipit-source-id: 9c5b2e98ade1dd3a048d6aa82e83c5aca9daf06c
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 2, 2024
1 parent f36bb54 commit e9bade8
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions erlang_service/src/erlang_service_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ process(Data) ->
gen_server:cast(?SERVER, {process, Data}).

-spec path_open(id(), string(), normal|lib)
-> {value, string()} | {failed, string()}.
-> {value, [string()]} | failed.
path_open(ReqId, Name, IncludeType) ->
gen_server:call(?SERVER, {path_open, ReqId, Name, IncludeType}).
case gen_server:call(?SERVER, {request, ReqId,
[unicode:characters_to_binary(add_include_type(Name, IncludeType))]}) of
{value, Data} ->
Paths = collect_paths(Data),
{value, Paths};
X -> X
end.

%%==============================================================================
%% gen_server callbacks
Expand All @@ -87,14 +93,14 @@ init(noargs) ->

-spec handle_call({path_open, string(), string(), normal|lib|doc}, any(), state())
-> {noreply, state()} | {stop|reply, any(), state()}.
handle_call({path_open, ReqId, Req, IncludeType}, From,
handle_call({request, ReqId, Data}, From,
#{io := IO, requests := Requests, own_requests := OwnRequests} = State) ->
case lists:keytake(ReqId, 2, Requests) of
{value, {_Pid, Id, _}, _NewRequests} ->
request(Id, [unicode:characters_to_binary(add_include_type(Req, IncludeType))], IO),
request(Id, Data, IO),
{noreply, State#{own_requests => [{Id, From}|OwnRequests]}};
_ ->
{reply, {failed, Req}, State}
{reply, failed, State}
end;
handle_call(Req, _From, State) ->
{stop, {unexpected_request, Req}, State}.
Expand Down Expand Up @@ -206,10 +212,9 @@ handle_request(<<"CTI", Id:64/big, Data/binary>>, State) ->
%% Start of callback responses
handle_request(<<"REP", OrigId:64/big, Data/binary>>,
#{own_requests := OwnRequests} = State) ->
Path = collect_paths(Data),
case lists:keytake(OrigId, 1, OwnRequests) of
{value, {OrigId, ReplyFrom}, NewOwnRequests} ->
gen_server:reply(ReplyFrom, {value, Path}),
gen_server:reply(ReplyFrom, {value, Data}),
{noreply, State#{own_requests => NewOwnRequests}};
_ ->
{noreply, State}
Expand Down

0 comments on commit e9bade8

Please sign in to comment.