Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qmp: simplify design to a single gen_server #178

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/virtuerl/src/virtuerl_qemu.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ handle_continue(setup_qmp, #{id := ID} = State) ->
%% {ok, _} = exec:run(iolist_to_binary(["inotifywait -e create --include 'qmp\\.sock' ", ID]), [sync]),
case wait_for_socket(QmpSocketPath) of
ok ->
{ok, QmpPid} = virtuerl_qmp:start_link(QmpSocketPath, self()),
% TODO: start_link might be too strong here, consider attaching it to a supervisor instead (or sth like gen_tcp's design)
{ok, QmpPid} = virtuerl_qmp:start_link(QmpSocketPath),
virtuerl_qmp:exec(QmpPid, cont),
{noreply, State#{qmp_pid => QmpPid}, {continue, setup_serial}};
timeout ->
Expand Down
100 changes: 29 additions & 71 deletions apps/virtuerl/src/virtuerl_qmp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,60 @@

-behaviour(gen_server).

-export([start_link/2, exec/2, stop/1]).
-export([start_link/1, exec/2, stop/1]).
-export([init/1, terminate/2, handle_cast/2, handle_call/3, handle_info/2]).

-include_lib("kernel/include/logger.hrl").


exec(Pid, Command) ->
gen_server:call(Pid, Command).


stop(Pid) ->
gen_server:stop(Pid).


%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================


start_link(QmpSocketPath, Receiver) ->
gen_server:start_link(?MODULE, {QmpSocketPath, Receiver}, []).
Pid ! {qmp, Command},
receive
{qmp, #{<<"return">> := #{}}} -> ok
end.


init({QmpSocketPath, Receiver}) ->
Self = self(),
Pid = spawn_link(fun() -> qmp_translator(QmpSocketPath, Self) end),
io:format("virtuerl_qmp: init~n"),
start_link(QmpSocketPath) ->
{ok, Pid} = gen_server:start_link(?MODULE, {QmpSocketPath, self()}, []),
receive
{qmp, #{<<"QMP">> := #{}}} -> ok
after
1000 ->
exit(qmp_not_responding)
end,
io:format("virtuerl_qmp: init after~n"),
execute(Pid, qmp_capabilities),
io:format("virtuerl_qmp: qmp caps~n"),
{ok, {Pid, Receiver}}.
exec(Pid, qmp_capabilities),
{ok, Pid}.


terminate(_Reason, {Pid, _}) ->
Ref = monitor(process, Pid),
exit(Pid, normal),
receive
{'DOWN', Ref, process, Pid, normal} -> ok
end,
true = demonitor(Ref),
io:format("exiting QMP server~n").
stop(Pid) ->
gen_server:stop(Pid).


handle_call(Command, _From, {Pid, _} = State) ->
execute(Pid, Command),
{reply, ok, State}.
init({QmpSocketPath, Receiver}) ->
{ok, QmpSocket} = gen_tcp:connect({local, QmpSocketPath}, 0, [local, {active, true}]),
{ok, {QmpSocket, Receiver}}.


handle_cast(Request, State) ->
handle_call(_Request, _From, _State) ->
erlang:error(not_implemented).


handle_info({qmp, #{<<"event">> := _} = Event}, {_, Receiver} = State) ->
Receiver ! {qmp, Event},
{noreply, State}.


execute(Pid, Command) when is_pid(Pid) ->
Pid ! {qmp, Command},
receive
{qmp, #{<<"return">> := #{}}} -> ok
end.

handle_cast(_Request, _State) ->
erlang:error(not_implemented).

%%%===================================================================
%%% Internal functions
%%%===================================================================

handle_info({tcp, _Socket, RawData}, {_QmpSocket, Receiver} = State) ->
Lines = re:split(RawData, "\r?\n", [trim]),
Jsons = lists:map(fun(Line) -> {ok, Json} = thoas:decode(Line), Json end, Lines),
?LOG_DEBUG(#{qmp_raw => RawData, qmp_parsed => Jsons}),
[ Receiver ! {qmp, Json} || Json <- Jsons ],
{noreply, State};

qmp_translator(QmpSocketPath, Receiver) ->
process_flag(trap_exit, true),
io:format("starting QMP translator (~s)!~n", [QmpSocketPath]),
{ok, QmpSocket} = gen_tcp:connect({local, QmpSocketPath}, 0, [local, {active, true}]),
qmp_loop(QmpSocket, Receiver).
handle_info({qmp, Command}, {QmpSocket, _Receiver} = State) ->
?LOG_INFO(#{who => virtuerl_qmp, command => Command}),
ok = gen_tcp:send(QmpSocket, thoas:encode(#{execute => Command})),
{noreply, State}.


qmp_loop(QmpSocket, Receiver) ->
receive
{tcp, _Socket, RawData} ->
Lines = re:split(RawData, "\r?\n", [trim]),
Jsons = lists:map(fun(Line) -> {ok, Json} = thoas:decode(Line), Json end, Lines),
?LOG_DEBUG(#{qmp_raw => RawData, qmp_parsed => Jsons}),
[ Receiver ! {qmp, Json} || Json <- Jsons ],
qmp_loop(QmpSocket, Receiver);
{qmp, Command} when is_atom(Command) ->
io:format("qmp_loop/qmp: ~p~n", [Command]),
ok = gen_tcp:send(QmpSocket, thoas:encode(#{execute => Command})),
qmp_loop(QmpSocket, Receiver);
{'EXIT', _SenderID, Reason} ->
io:format("closing QMP socket (~p)!~n", [Reason]),
ok = gen_tcp:close(QmpSocket),
exit(Reason)
end.
terminate(_Reason, {QmpSocket, _Receiver}) ->
gen_tcp:close(QmpSocket).