Add Network Manager screen

This commit is contained in:
Craig Everett 2024-10-14 20:38:42 +09:00
parent 3afc3cfa02
commit 50f1ea1b8d
4 changed files with 512 additions and 24 deletions

View File

@ -1,7 +1,7 @@
% Node, Chain and Net represent the physical network. % Node, Chain and Net represent the physical network.
-record(node, -record(node,
{ip = {161,97,102,143} :: inet:ip_address(), {ip = {161,97,102,143} :: string() | inet:ip_address(),
external = 3013 :: inet:port_number(), % 3013 external = 3013 :: inet:port_number(), % 3013
internal = none :: none | inet:port_number(), % 3113 internal = none :: none | inet:port_number(), % 3113
rosetta = none :: none | inet:port_number(), % 8080 rosetta = none :: none | inet:port_number(), % 8080
@ -10,7 +10,7 @@
-record(chain, -record(chain,
{id = <<"mint.devnet">> :: binary(), {id = <<"groot.devnet">> :: binary(),
coins = ["gaju"] :: [string()], coins = ["gaju"] :: [string()],
nodes = [#node{}] :: [#node{}]}). nodes = [#node{}] :: [#node{}]}).
@ -30,7 +30,7 @@
-record(coin, -record(coin,
{id = "gaju" :: string(), {id = "gaju" :: string(),
mint = <<"mint.devnet">> :: binary(), mint = <<"groot.devnet">> :: binary(),
acs = [#ac{}] :: [#ac{}]}). acs = [#ac{}] :: [#ac{}]}).
@ -39,7 +39,7 @@
-record(balance, -record(balance,
{coin = "gaju" :: string(), {coin = "gaju" :: string(),
total = 0 :: non_neg_integer(), total = 0 :: non_neg_integer(),
dist = [{<<"mint.devnet">>, 0}] :: [{Chain :: binary(), non_neg_integer()}]}). dist = [{<<"groot.devnet">>, 0}] :: [{Chain :: binary(), non_neg_integer()}]}).
-record(poa, -record(poa,
@ -79,6 +79,6 @@
poas = [] :: [#poa{}], poas = [] :: [#poa{}],
keys = [] :: [#key{}], keys = [] :: [#key{}],
pass = none :: none | binary(), pass = none :: none | binary(),
chain_id = <<"mint.devnet">> :: binary(), chain_id = <<"groot.devnet">> :: binary(),
endpoint = #node{} :: #node{}, endpoint = #node{} :: #node{},
chains = [#chain{}] :: [#chain{}]}). nets = [#net{}] :: [#net{}]}).

View File

@ -11,11 +11,12 @@
-license("GPL-3.0-or-later"). -license("GPL-3.0-or-later").
-behavior(gen_server). -behavior(gen_server).
-export([open_wallet/2, close_wallet/0, password/2, -export([show_ui/1,
nonce/1, spend/2, open_wallet/2, close_wallet/0, password/2,
nonce/1, spend/2, chain/1,
make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1]). make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1]).
-export([encrypt/2, decrypt/2]). -export([encrypt/2, decrypt/2]).
-export([start_link/0, stop/0, save/1]). -export([start_link/0, stop/0, save/1, save/2]).
-export([init/1, terminate/2, code_change/3, -export([init/1, terminate/2, code_change/3,
handle_call/3, handle_cast/2, handle_info/2]). handle_call/3, handle_cast/2, handle_info/2]).
-include("$zx_include/zx_logger.hrl"). -include("$zx_include/zx_logger.hrl").
@ -26,20 +27,36 @@
%%% Type and Record Definitions %%% Type and Record Definitions
-record(ui,
{name = none :: none | ui_name(),
pid = none :: none | pid(),
wx = none :: none | wx:wx_object(),
mon = none :: none | reference()}).
-record(s, -record(s,
{version = 1 :: integer(), {version = 1 :: integer(),
window = none :: none | wx:wx_object(), window = none :: none | wx:wx_object(),
tasks = [] :: [#ui{}],
wallet = #wallet{} :: #wallet{}, wallet = #wallet{} :: #wallet{},
prefs = #{} :: #{atom() := term()}}). prefs = #{} :: #{atom() := term()}}).
-type state() :: #s{}. -type state() :: #s{}.
-type ui_name() :: gmc_v_netman
| gmc_v_conman.
%% Interface %% Interface
-spec show_ui(Name) -> ok
when Name :: ui_name().
show_ui(Name) ->
gen_server:cast(?MODULE, {show_ui, Name}).
-spec open_wallet(Path, Password) -> ok -spec open_wallet(Path, Password) -> ok
when Path :: file:filename(), when Path :: file:filename(),
Password :: string(). Password :: string().
@ -79,6 +96,13 @@ spend(KeyID, TX) ->
gen_server:cast(?MODULE, {spend, KeyID, TX}). gen_server:cast(?MODULE, {spend, KeyID, TX}).
-spec chain(ID) -> ok
when ID :: string().
chain(ID) ->
gen_server:cast(?MODULE, {chain, ID}).
-spec make_key(Type, Size, Name, Seed, Encoding, Transform) -> ok -spec make_key(Type, Size, Name, Seed, Encoding, Transform) -> ok
when Type :: {eddsa, ed25519}, when Type :: {eddsa, ed25519},
Size :: 256, Size :: 256,
@ -142,6 +166,14 @@ save(Prefs) ->
gen_server:call(?MODULE, {save, Prefs}). gen_server:call(?MODULE, {save, Prefs}).
-spec save(Label, Pref) -> ok
when Label :: term(),
Pref :: term().
save(Label, Pref) ->
gen_server:call(?MODULE, {save, Label, Pref}).
%%% Startup Functions %%% Startup Functions
@ -202,6 +234,9 @@ read_prefs() ->
handle_call({save, Prefs}, _, State) -> handle_call({save, Prefs}, _, State) ->
Response = do_save(State#s{prefs = Prefs}), Response = do_save(State#s{prefs = Prefs}),
{reply, Response, State}; {reply, Response, State};
handle_call({save, Label, Pref}, _, State) ->
NewState = do_save(Label, Pref, State),
{reply, ok, NewState};
handle_call({mnemonic, ID}, _, State) -> handle_call({mnemonic, ID}, _, State) ->
Response = do_mnemonic(ID, State), Response = do_mnemonic(ID, State),
{reply, Response, State}; {reply, Response, State};
@ -218,6 +253,9 @@ handle_call(Unexpected, From, State) ->
%% The gen_server:handle_cast/2 callback. %% The gen_server:handle_cast/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2 %% See: http://erlang.org/doc/man/gen_server.html#Module:handle_cast-2
handle_cast({show_ui, Name}, State) ->
NewState = do_show_ui(Name, State),
{noreply, NewState};
handle_cast({open_wallet, Path, Phrase}, State) -> handle_cast({open_wallet, Path, Phrase}, State) ->
NewState = do_open_wallet(Path, Phrase, State), NewState = do_open_wallet(Path, Phrase, State),
{noreply, NewState}; {noreply, NewState};
@ -227,6 +265,9 @@ handle_cast({password, Old, New}, State) ->
handle_cast({spend, KeyID, TX}, State) -> handle_cast({spend, KeyID, TX}, State) ->
ok = do_spend(KeyID, TX, State), ok = do_spend(KeyID, TX, State),
{noreply, State}; {noreply, State};
handle_cast({chain, ID}, State) ->
NewState = do_chain(ID, State),
{noreply, NewState};
handle_cast({make_key, Name, Seed, Encoding, Transform}, State) -> handle_cast({make_key, Name, Seed, Encoding, Transform}, State) ->
NewState = do_make_key(Name, Seed, Encoding, Transform, State), NewState = do_make_key(Name, Seed, Encoding, Transform, State),
{noreply, NewState}; {noreply, NewState};
@ -255,11 +296,25 @@ handle_cast(Unexpected, State) ->
%% The gen_server:handle_info/2 callback. %% The gen_server:handle_info/2 callback.
%% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2 %% See: http://erlang.org/doc/man/gen_server.html#Module:handle_info-2
handle_info({'DOWN', Mon, process, PID, Info}, State) ->
NewState = handle_down(Mon, PID, Info, State),
{noreply, NewState};
handle_info(Unexpected, State) -> handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]), ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}. {noreply, State}.
handle_down(Mon, PID, Info, State = #s{tasks = Tasks}) ->
case lists:keytake(Mon, #ui.mon, Tasks) of
{value, #ui{}, NewTasks} ->
State#s{tasks = NewTasks};
false ->
Unexpected = {'DOWN', Mon, process, PID, Info},
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
State
end.
%% @private %% @private
%% gen_server callback to handle state transformations necessary for hot %% gen_server callback to handle state transformations necessary for hot
@ -279,6 +334,27 @@ terminate(Reason, State) ->
%%% %%%
do_show_ui(Name, State = #s{tasks = Tasks, prefs = Prefs, wallet = Wallet}) ->
#wallet{nets = Nets} = Wallet,
case lists:keyfind(Name, #ui.name, Tasks) of
#ui{wx = Win} ->
ok = Name:to_front(Win),
State;
false ->
Win = Name:start_link({Prefs, Nets}),
PID = wx_object:get_pid(Win),
Mon = monitor(process, PID),
UI = #ui{name = Name, pid = PID, wx = Win, mon = Mon},
State#s{tasks = [UI | Tasks]}
end.
do_chain(ID, State = #s{prefs = Prefs}) ->
tell("Would be doing chain in do_chain/2 here"),
State.
do_make_key(Name, <<>>, _, Transform, State) -> do_make_key(Name, <<>>, _, Transform, State) ->
Bin = crypto:strong_rand_bytes(32), Bin = crypto:strong_rand_bytes(32),
do_make_key2(Name, Bin, Transform, State); do_make_key2(Name, Bin, Transform, State);
@ -422,7 +498,8 @@ do_open_wallet(Path, none, State) ->
State#s{wallet = Recovered}; State#s{wallet = Recovered};
Error -> Error ->
ok = gmc_gui:trouble(Error), ok = gmc_gui:trouble(Error),
State New = default_wallet(),
State#s{wallet = New}
end; end;
do_open_wallet(Path, Phrase, State) -> do_open_wallet(Path, Phrase, State) ->
Pass = pass(Phrase), Pass = pass(Phrase),
@ -433,9 +510,19 @@ do_open_wallet(Path, Phrase, State) ->
State#s{wallet = Recovered}; State#s{wallet = Recovered};
Error -> Error ->
ok = gmc_gui:trouble(Error), ok = gmc_gui:trouble(Error),
State New = default_wallet(),
State#s{wallet = New}
end. end.
default_wallet() ->
DevNet = #net{id = <<"devnet">>, chains = [#chain{}]},
TestChain1 = #chain{id = <<"groot.testnet">>,
nodes = [#node{ip = {1,2,3,4}}, #node{ip = {5,6,7,8}}]},
TestChain2 = #chain{id = <<"test_ac.testnet">>,
nodes = [#node{ip = {11,12,13,14}}, #node{ip = {15,16,17,18}}]},
TestNet = #net{id = <<"testnet">>, chains = [TestChain1, TestChain2]},
#wallet{nets = [DevNet, TestNet]}.
do_password(none, none, State) -> do_password(none, none, State) ->
State; State;
@ -504,7 +591,14 @@ do_spend2(PrivKey,
[{signatures, [Signature]}, [{signatures, [Signature]},
{transaction, NetworkTX}], {transaction, NetworkTX}],
SignedTX = aeser_chain_objects:serialize(SigTxType, SigTxVsn, SigTemplate, TX_Data), SignedTX = aeser_chain_objects:serialize(SigTxType, SigTxVsn, SigTemplate, TX_Data),
tell("SpendTX: ~p", [SignedTX]). Encoded = aeser_api_encoder:encode(transaction, SignedTX),
tell("SpendTX: ~p", [Encoded]).
do_save(Label, Pref, State = #s{prefs = Prefs}) ->
NewPrefs = maps:put(Label, Pref, Prefs),
ok = persist(Prefs),
State#s{prefs = NewPrefs}.
do_save(State = #s{prefs = Prefs}) -> do_save(State = #s{prefs = Prefs}) ->

View File

@ -25,7 +25,7 @@
-record(w, -record(w,
{name = none :: atom(), {name = none :: atom(),
id = 0 :: integer(), id = 0 :: integer(),
wx = none :: wx:wx_object()}). wx = none :: none | wx:wx_object()}).
-record(s, -record(s,
{wx = none :: none | wx:wx_object(), {wx = none :: none | wx:wx_object(),
@ -290,6 +290,8 @@ handle_event(#wx{event = #wxCommand{type = command_button_clicked},
State = #s{buttons = Buttons}) -> State = #s{buttons = Buttons}) ->
NewState = NewState =
case lists:keyfind(ID, #w.id, Buttons) of case lists:keyfind(ID, #w.id, Buttons) of
#w{name = chain} -> netman(State);
% #w{name = node} -> set_node(State);
#w{name = make_key} -> make_key(State); #w{name = make_key} -> make_key(State);
#w{name = recover} -> recover_key(State); #w{name = recover} -> recover_key(State);
#w{name = mnemonic} -> show_mnemonic(State); #w{name = mnemonic} -> show_mnemonic(State);
@ -342,6 +344,41 @@ terminate(Reason, State) ->
%%% Doers %%% Doers
% TODO: Make a network/chain management activity
netman(State) ->
ok = gmc_con:show_ui(gmc_v_netman),
State.
%set_chain(State = #s{frame = Frame, j = J, buttons = Buttons}) ->
% Label = "Node",
% Dialog = wxDialog:new(Frame, ?wxID_ANY, J(Label)),
% Sizer = wxBoxSizer:new(?wxVERTICAL),
% NodeSz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, J(Label)}]),
% NodeTx = wxTextCtrl:new(Dialog, ?wxID_ANY),
% _ = wxStaticBoxSizer:add(NodeSz, NodeTx, zxw:flags(wide)),
% ButtSz = wxBoxSizer:new(?wxHORIZONTAL),
% Affirm = wxButton:new(Dialog, ?wxID_OK),
% Cancel = wxButton:new(Dialog, ?wxID_CANCEL),
% _ = wxBoxSizer:add(ButtSz, Affirm, zxw:flags(wide)),
% _ = wxBoxSizer:add(ButtSz, Cancel, zxw:flags(wide)),
% _ = wxBoxSizer:add(Sizer, NodeSz, zxw:flags(base)),
% _ = wxBoxSizer:add(Sizer, ButtSz, zxw:flags(base)),
% ok =
% case wxDialog:showModal(Dialog) of
% ?wxID_OK ->
% String = wxTextCtrl:getValue(NodeTx),
% set_node2(String);
% ?wxID_CANCEL ->
% ok
% end,
% State.
%
%set_node2("") ->
% ok;
%set_node2(String) ->
% case lists:
make_key(State = #s{frame = Frame, j = J}) -> make_key(State = #s{frame = Frame, j = J}) ->
Dialog = wxDialog:new(Frame, ?wxID_ANY, J("New Key")), Dialog = wxDialog:new(Frame, ?wxID_ANY, J("New Key")),
Sizer = wxBoxSizer:new(?wxVERTICAL), Sizer = wxBoxSizer:new(?wxVERTICAL),

357
src/gmc_v_netman.erl Normal file
View File

@ -0,0 +1,357 @@
-module(gmc_v_netman).
-vsn("0.1.0").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("QPQ AG <info@qpq.swiss>").
-license("GPL-3.0-or-later").
-behavior(wx_object).
-include_lib("wx/include/wx.hrl").
-export([to_front/1, set_manifest/1]).
-export([start_link/1]).
-export([init/1, terminate/2, code_change/3,
handle_call/3, handle_cast/2, handle_info/2, handle_event/2]).
-include("$zx_include/zx_logger.hrl").
-include("gmc.hrl").
-record(w,
{name = none :: atom(),
id = 0 :: integer(),
wx = none :: none | wx:wx_object()}).
-record(s,
{wx = none :: none | wx:wx_object(),
frame = none :: none | wx:wx_object(),
lang = en :: en | jp,
j = none :: none | fun(),
prefs = #{} :: map(),
buttons = [] :: [#w{}],
nets = [] :: [#net{}],
book = #w{} :: #w{}}).
%%% Interface
-spec to_front(Win) -> ok
when Win :: wx:wx_object().
to_front(Win) ->
wx_object:cast(Win, to_front).
-spec set_manifest(Entries) -> ok
when Entries :: [ael:conf_meta()].
set_manifest(Entries) ->
case is_pid(whereis(?MODULE)) of
true -> wx_object:cast(?MODULE, {set_manifest, Entries});
false -> ok
end.
%%% Startup Functions
start_link(Args) ->
wx_object:start_link({local, ?MODULE}, ?MODULE, Args, []).
init({Prefs, Manifest}) ->
Lang = maps:get(lang, Prefs, en_us),
Trans = gmc_jt:read_translations(?MODULE),
J = gmc_jt:j(Lang, Trans),
Wx = wx:new(),
Frame = wxFrame:new(Wx, ?wxID_ANY, J("Networks")),
MainSz = wxBoxSizer:new(?wxVERTICAL),
ButtSz = wxBoxSizer:new(?wxHORIZONTAL),
ButtonTemplates =
[{node, J("Add Node")},
{drop, J("Delete")}],
MakeButton =
fun({Name, Label}) ->
B = wxButton:new(Frame, ?wxID_ANY, [{label, Label}]),
#w{name = Name, id = wxButton:getId(B), wx = B}
end,
Buttons = lists:map(MakeButton, ButtonTemplates),
AddButton = fun(#w{wx = B}) -> wxSizer:add(ButtSz, B, zxw:flags(wide)) end,
Notebook = wxNotebook:new(Frame, ?wxID_ANY, [{style, ?wxBK_DEFAULT}]),
ok = add_pages(Notebook, J, Manifest),
ok = lists:foreach(AddButton, Buttons),
_ = wxSizer:add(MainSz, ButtSz, zxw:flags(base)),
_ = wxSizer:add(MainSz, Notebook, zxw:flags(wide)),
_ = wxFrame:setSizer(Frame, MainSz),
_ = wxSizer:layout(MainSz),
ok =
case safe_size(Prefs) of
{pref, max} ->
wxTopLevelWindow:maximize(Frame);
{pref, WSize} ->
wxFrame:setSize(Frame, WSize);
{center, WSize} ->
ok = wxFrame:setSize(Frame, WSize),
wxFrame:center(Frame)
end,
ok = wxFrame:connect(Frame, close_window),
ok = wxFrame:connect(Frame, command_button_clicked),
ok = wxFrame:center(Frame),
true = wxFrame:show(Frame),
State =
#s{wx = Wx, frame = Frame,
j = J, prefs = Prefs,
buttons = Buttons, nets = Manifest, book = Notebook},
{Frame, State}.
safe_size(Prefs) ->
Display = wxDisplay:new(),
GSize = wxDisplay:getGeometry(Display),
CSize = wxDisplay:getClientArea(Display),
PPI =
try
wxDisplay:getPPI(Display)
catch
Class:Exception -> {Class, Exception}
end,
ok = log(info, "Geometry: ~p", [GSize]),
ok = log(info, "ClientArea: ~p", [CSize]),
ok = log(info, "PPI: ~p", [PPI]),
Geometry =
case maps:find({?MODULE, geometry}, Prefs) of
{ok, none} ->
{X, Y, _, _} = GSize,
{center, {X, Y, 420, 520}};
{ok, G} ->
{pref, G};
error ->
{X, Y, _, _} = GSize,
{center, {X, Y, 420, 520}}
end,
ok = wxDisplay:destroy(Display),
Geometry.
add_pages(Notebook, J, [#net{id = ID, chains = Chains} | Rest]) ->
Page = wxScrolledWindow:new(Notebook),
PageSz = wxBoxSizer:new(?wxVERTICAL),
ChainPanels = draw_chain_panels(Page, J, Chains),
AddPanel = fun(Panel) -> wxSizer:add(PageSz, Panel, zxw:flags(wide)) end,
ok = lists:foreach(AddPanel, ChainPanels),
ok = wxWindow:setSizer(Page, PageSz),
ok = wxSizer:layout(PageSz),
true = wxNotebook:addPage(Notebook, Page, ID, []),
add_pages(Notebook, J, Rest);
add_pages(_, _, []) ->
ok.
draw_chain_panels(Page, J, [#chain{id = ID, coins = Coins, nodes = Nodes} | Rest]) ->
Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Page, [{label, ID}]),
CurrencySizer = wxBoxSizer:new(?wxHORIZONTAL),
CurrencyLabel = wxStaticText:new(Page, ?wxID_ANY, J("Currencies: ")),
Currencies = wxStaticText:new(Page, ?wxID_ANY, string:join(Coins, " ")),
_ = wxSizer:add(CurrencySizer, CurrencyLabel, zxw:flags(base)),
_ = wxSizer:add(CurrencySizer, Currencies, zxw:flags(base)),
List = wxListCtrl:new(Page, [{style, ?wxLC_REPORT bor ?wxLC_SINGLE_SEL}]),
Labels =
[J("Address"),
J("External"),
J("Internal"),
J("Rosetta"),
J("Channel"),
J("Middleware")],
Columns = indexify(Labels),
AddColumn = fun({I, L}) -> wxListCtrl:insertColumn(List, I, L, []) end,
ok = lists:foreach(AddColumn, Columns),
IndexedNodes = indexify(Nodes),
AddNodes =
fun({Index,
#node{ip = IP,
external = E, internal = I, rosetta = R, channel = C, mdw = M}}) ->
Address =
case is_list(IP) of
false -> inet:ntoa(IP);
true -> IP
end,
Elements = indexify([Address | stringify_ports([E, I, R, C, M])]),
tell("Elements: ~p", [Elements]),
_ = wxListCtrl:insertItem(List, Index, ""),
AddText = fun({K, L}) -> wxListCtrl:setItem(List, Index, K, L) end,
lists:foreach(AddText, Elements)
end,
ok = lists:foreach(AddNodes, IndexedNodes),
_ = wxListCtrl:setColumnWidth(List, 0, ?wxLIST_AUTOSIZE),
_ = wxSizer:add(Sizer, CurrencySizer, zxw:flags(base)),
_ = wxSizer:add(Sizer, List, zxw:flags(wide)),
[Sizer | draw_chain_panels(Page, J, Rest)];
draw_chain_panels(_, _, []) ->
[].
indexify(List) ->
lists:zip(lists:seq(0, length(List) -1), List).
stringify_ports([none | T]) -> ["" | stringify_ports(T)];
stringify_ports([Port | T]) -> [integer_to_list(Port) | stringify_ports(T)];
stringify_ports([]) -> [].
%%% OTP callbacks
handle_call(Unexpected, From, State) ->
ok = log(warning, "Unexpected call from ~tp: ~tp~n", [From, Unexpected]),
{noreply, State}.
handle_cast(Unexpected, State) ->
ok = log(warning, "Unexpected cast: ~tp~n", [Unexpected]),
{noreply, State}.
handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}.
handle_event(E = #wx{event = #wxCommand{type = command_button_clicked},
id = ID},
State = #s{buttons = Buttons}) ->
NewState =
case lists:keyfind(ID, #w.id, Buttons) of
#w{name = node} -> add_node(State);
#w{name = drop} -> drop_network(State);
false ->
tell("Received message: ~w", [E]),
State
end,
{noreply, NewState};
handle_event(#wx{event = #wxClose{}}, State = #s{frame = Frame}) ->
Geometry =
case wxTopLevelWindow:isMaximized(Frame) of
true ->
max;
false ->
{X, Y} = wxWindow:getPosition(Frame),
{W, H} = wxWindow:getSize(Frame),
{X, Y, W, H}
end,
ok = gmc_con:save({?MODULE, geometry}, Geometry),
ok = wxWindow:destroy(Frame),
{noreply, State};
handle_event(Event, State) ->
ok = tell(info, "Unexpected event ~tp State: ~tp~n", [Event, State]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(Reason, State) ->
ok = log(info, "Reason: ~tp, State: ~tp", [Reason, State]),
wx:destroy().
%%% Doers
add_node(State = #s{frame = Frame, j = J}) ->
Dialog = wxDialog:new(Frame, ?wxID_ANY, J("Add Node")),
Sizer = wxBoxSizer:new(?wxVERTICAL),
AddressSz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, J("Address")}]),
AddressTx = wxTextCtrl:new(Dialog, ?wxID_ANY),
_ = wxSizer:add(AddressSz, AddressTx, zxw:flags(wide)),
PortSz = wxBoxSizer:new(?wxHORIZONTAL),
Labels =
[J("External"),
J("Internal"),
J("Rosetta"),
J("Channel"),
J("Middleware")],
MakePortCtrl =
fun(L) ->
Sz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, L}]),
Tx = wxTextCtrl:new(Dialog, ?wxID_ANY),
_ = wxSizer:add(Sz, Tx, zxw:flags(wide)),
_ = wxSizer:add(PortSz, Sz, zxw:flags(wide)),
Tx
end,
PortCtrls = lists:map(MakePortCtrl, Labels),
ButtSz = wxBoxSizer:new(?wxHORIZONTAL),
Affirm = wxButton:new(Dialog, ?wxID_OK),
Cancel = wxButton:new(Dialog, ?wxID_CANCEL),
_ = wxBoxSizer:add(ButtSz, Affirm, zxw:flags(wide)),
_ = wxBoxSizer:add(ButtSz, Cancel, zxw:flags(wide)),
_ = wxSizer:add(Sizer, AddressSz, zxw:flags(base)),
_ = wxSizer:add(Sizer, PortSz, zxw:flags(base)),
_ = wxSizer:add(Sizer, ButtSz, zxw:flags(wide)),
ok = wxDialog:setSizer(Dialog, Sizer),
ok = wxBoxSizer:layout(Sizer),
ok = wxFrame:setSize(Dialog, {500, 200}),
ok = wxFrame:center(Dialog),
ok = wxStyledTextCtrl:setFocus(AddressTx),
ok =
case wxDialog:showModal(Dialog) of
?wxID_OK ->
case wxTextCtrl:getValue(AddressTx) of
"" -> ok;
Address -> add_node2(Address, PortCtrls)
end;
?wxID_CANCEL ->
ok
end,
ok = wxDialog:destroy(Dialog),
State.
add_node2(Address, PortCtrls) ->
IP =
case inet:parse_address(Address) of
{ok, N} -> N;
{error, einval} -> Address
end,
[E, I, R, C, M] = lists:map(fun numerify_port/1, PortCtrls),
New = #node{ip = IP, external = E, internal = I, rosetta = R, channel = C, mdw = M},
gmc_con:add_node(New).
numerify_port(Ctrl) ->
case wxTextCtrl:getValue(Ctrl) of
"" -> none;
String ->
case s_to_i(String) of
{ok, PortNum} -> PortNum;
error -> none
end
end.
s_to_i(S) ->
try
I = list_to_integer(S),
{ok, I}
catch error:badarg ->
error
end.
drop_network(State = #s{nets = Nets, book = Book}) ->
ok =
case wxNotebook:getSelection(Book) of
?wxNOT_FOUND ->
ok;
Index ->
#net{id = ID} = lists:nth(Index + 1, Nets),
gmc_con:drop_network(ID)
end,
State.