-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and decouple QEMU for better testing
- Loading branch information
Showing
11 changed files
with
322 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-module(virtuerl_reg). | ||
|
||
-behaviour(gen_server). | ||
|
||
-export([start_link/0, register_name/2, unregister_name/1, whereis_name/1, send/2]). | ||
%% Callbacks for `gen_server` | ||
-export([init/1, handle_call/3, handle_cast/2]). | ||
|
||
-type name() :: {atom(), atom()}. | ||
|
||
|
||
-spec register_name(Name :: name(), Pid :: pid()) -> yes | no. | ||
register_name(Name, Pid) -> gen_server:call(?MODULE, {register_name, Name, Pid}). | ||
|
||
|
||
-spec unregister_name(Name :: name()) -> _. | ||
unregister_name(Name) -> gen_server:cast(?MODULE, {unregister_name, Name}). | ||
|
||
|
||
-spec whereis_name(Name :: name()) -> pid() | undefined. | ||
whereis_name(Name) -> gen_server:call(?MODULE, {whereis_name, Name}). | ||
|
||
|
||
-spec send(Name :: name(), Msg :: term()) -> pid(). | ||
send(Name, Msg) -> | ||
case whereis_name(Name) of | ||
undefined -> exit({badarg, Name, Msg}); | ||
Pid -> Pid ! Msg, Pid | ||
end. | ||
|
||
|
||
start_link() -> | ||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). | ||
|
||
|
||
init([]) -> | ||
{ok, #{}}. | ||
|
||
|
||
% -spec handle_call({register_name, name(), pid()}, _From :: _, State :: #{}) -> {reply, no | yes, #{}}. | ||
handle_call({register_name, Name, Pid}, _From, State) -> | ||
{Res, NewState} = case State of | ||
#{Name := _} -> {no, State}; | ||
_ -> {yes, State#{Name => Pid}} | ||
end, | ||
{reply, Res, NewState}; | ||
|
||
handle_call({whereis_name, Name}, _From, State) -> | ||
Res = case State of | ||
#{Name := Pid} -> Pid; | ||
_ -> undefined | ||
end, | ||
{reply, Res, State}. | ||
|
||
|
||
handle_cast({unregister_name, Name}, State) -> | ||
{noreply, ok, maps:remove(Name, State)}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-module(virtuerl_server). | ||
|
||
% -behaviour(gen_server). | ||
|
||
-export([start/2]). | ||
|
||
%% Callbacks for `gen_server` | ||
% -export([init/1, handle_call/3, handle_cast/2]). | ||
|
||
% start_link([]) -> | ||
% gen_server:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
% init([]) -> | ||
% _ = ets:new(virtuerl_servers, [named_table]), | ||
% Tab = ets:whereis(virtuerl_servers), | ||
% erlang:error(not_implemented). | ||
|
||
% handle_call({update_config, Name, Conf},_From,State) -> | ||
|
||
% handle_cast(Request,State) -> | ||
% erlang:error(not_implemented). | ||
|
||
|
||
start(Name, Conf) -> | ||
{ok, SupPid} = supervisor:start_child(virtuerl_sup_sup, | ||
#{id => Name, start => {virtuerl_sup, start_link, [Name, Conf]}, type => supervisor}), | ||
{ok, SupPid}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-module(virtuerl_sup_sup). | ||
|
||
-behaviour(supervisor). | ||
|
||
-export([start_link/0]). | ||
-export([init/1]). | ||
|
||
|
||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
|
||
init([]) -> | ||
SupFlags = #{ | ||
strategy => one_for_one, | ||
intensity => 300, | ||
period => 5 | ||
}, | ||
ChildSpecs = [#{id => virtuerl_reg, start => {virtuerl_reg, start_link, []}}], | ||
{ok, {SupFlags, ChildSpecs}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[{kernel, | ||
[{logger_level, all}, | ||
{logger, | ||
[{handler, default, logger_std_h, | ||
#{ level => info, | ||
formatter => {logger_formatter, #{single_line => false}}}}, | ||
{handler, debug, logger_disk_log_h, | ||
#{ level => debug, | ||
config => #{file => "var/log/virtuerl.log"}, | ||
max_no_bytes => 10485760, | ||
formatter => {logger_formatter, #{single_line => false}}}} | ||
]}]}, | ||
{erlexec, []} | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.