diff --git a/include/gmc.hrl b/include/gmc.hrl index 5858f46..9cc2cff 100644 --- a/include/gmc.hrl +++ b/include/gmc.hrl @@ -1,7 +1,7 @@ % Node, Chain and Net represent the physical network. -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 internal = none :: none | inet:port_number(), % 3113 rosetta = none :: none | inet:port_number(), % 8080 @@ -10,9 +10,9 @@ -record(chain, - {id = <<"mint.devnet">> :: binary(), - coins = ["gaju"] :: [string()], - nodes = [#node{}] :: [#node{}]}). + {id = <<"groot.devnet">> :: binary(), + coins = ["gaju"] :: [string()], + nodes = [#node{}] :: [#node{}]}). -record(net, @@ -29,17 +29,17 @@ -record(coin, - {id = "gaju" :: string(), - mint = <<"mint.devnet">> :: binary(), - acs = [#ac{}] :: [#ac{}]}). + {id = "gaju" :: string(), + mint = <<"groot.devnet">> :: binary(), + acs = [#ac{}] :: [#ac{}]}). % Balance, POA, Key, TXs, all culminate in capturing a complete Wallet view. -record(balance, - {coin = "gaju" :: string(), - total = 0 :: non_neg_integer(), - dist = [{<<"mint.devnet">>, 0}] :: [{Chain :: binary(), non_neg_integer()}]}). + {coin = "gaju" :: string(), + total = 0 :: non_neg_integer(), + dist = [{<<"groot.devnet">>, 0}] :: [{Chain :: binary(), non_neg_integer()}]}). -record(poa, @@ -75,10 +75,10 @@ -record(wallet, - {version = 1 :: integer(), - poas = [] :: [#poa{}], - keys = [] :: [#key{}], - pass = none :: none | binary(), - chain_id = <<"mint.devnet">> :: binary(), - endpoint = #node{} :: #node{}, - chains = [#chain{}] :: [#chain{}]}). + {version = 1 :: integer(), + poas = [] :: [#poa{}], + keys = [] :: [#key{}], + pass = none :: none | binary(), + chain_id = <<"groot.devnet">> :: binary(), + endpoint = #node{} :: #node{}, + nets = [#net{}] :: [#net{}]}). diff --git a/src/gmc_con.erl b/src/gmc_con.erl index 7c7cc8a..8aec637 100644 --- a/src/gmc_con.erl +++ b/src/gmc_con.erl @@ -11,11 +11,12 @@ -license("GPL-3.0-or-later"). -behavior(gen_server). --export([open_wallet/2, close_wallet/0, password/2, - nonce/1, spend/2, +-export([show_ui/1, + 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]). -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, handle_call/3, handle_cast/2, handle_info/2]). -include("$zx_include/zx_logger.hrl"). @@ -26,20 +27,36 @@ %%% 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, {version = 1 :: integer(), window = none :: none | wx:wx_object(), + tasks = [] :: [#ui{}], wallet = #wallet{} :: #wallet{}, prefs = #{} :: #{atom() := term()}}). -type state() :: #s{}. +-type ui_name() :: gmc_v_netman + | gmc_v_conman. %% 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 when Path :: file:filename(), Password :: string(). @@ -79,6 +96,13 @@ 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 when Type :: {eddsa, ed25519}, Size :: 256, @@ -142,6 +166,14 @@ 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 @@ -202,6 +234,9 @@ read_prefs() -> handle_call({save, Prefs}, _, State) -> Response = do_save(State#s{prefs = Prefs}), {reply, Response, State}; +handle_call({save, Label, Pref}, _, State) -> + NewState = do_save(Label, Pref, State), + {reply, ok, NewState}; handle_call({mnemonic, ID}, _, State) -> Response = do_mnemonic(ID, State), {reply, Response, State}; @@ -218,6 +253,9 @@ handle_call(Unexpected, From, State) -> %% The gen_server:handle_cast/2 callback. %% 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) -> NewState = do_open_wallet(Path, Phrase, State), {noreply, NewState}; @@ -227,6 +265,9 @@ handle_cast({password, Old, New}, State) -> handle_cast({spend, KeyID, TX}, State) -> ok = do_spend(KeyID, TX, State), {noreply, State}; +handle_cast({chain, ID}, State) -> + NewState = do_chain(ID, State), + {noreply, NewState}; handle_cast({make_key, Name, Seed, Encoding, Transform}, State) -> NewState = do_make_key(Name, Seed, Encoding, Transform, State), {noreply, NewState}; @@ -255,11 +296,25 @@ handle_cast(Unexpected, State) -> %% The gen_server:handle_info/2 callback. %% 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) -> ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]), {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 %% 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) -> Bin = crypto:strong_rand_bytes(32), do_make_key2(Name, Bin, Transform, State); @@ -422,7 +498,8 @@ do_open_wallet(Path, none, State) -> State#s{wallet = Recovered}; Error -> ok = gmc_gui:trouble(Error), - State + New = default_wallet(), + State#s{wallet = New} end; do_open_wallet(Path, Phrase, State) -> Pass = pass(Phrase), @@ -433,9 +510,19 @@ do_open_wallet(Path, Phrase, State) -> State#s{wallet = Recovered}; Error -> ok = gmc_gui:trouble(Error), - State + New = default_wallet(), + State#s{wallet = New} 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) -> State; @@ -504,7 +591,14 @@ do_spend2(PrivKey, [{signatures, [Signature]}, {transaction, NetworkTX}], 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}) -> diff --git a/src/gmc_gui.erl b/src/gmc_gui.erl index 284f7f6..06756db 100644 --- a/src/gmc_gui.erl +++ b/src/gmc_gui.erl @@ -25,7 +25,7 @@ -record(w, {name = none :: atom(), id = 0 :: integer(), - wx = none :: wx:wx_object()}). + wx = none :: none | wx:wx_object()}). -record(s, {wx = none :: none | wx:wx_object(), @@ -290,6 +290,8 @@ handle_event(#wx{event = #wxCommand{type = command_button_clicked}, State = #s{buttons = Buttons}) -> NewState = 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 = recover} -> recover_key(State); #w{name = mnemonic} -> show_mnemonic(State); @@ -342,6 +344,41 @@ terminate(Reason, State) -> %%% 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}) -> Dialog = wxDialog:new(Frame, ?wxID_ANY, J("New Key")), Sizer = wxBoxSizer:new(?wxVERTICAL), diff --git a/src/gmc_v_netman.erl b/src/gmc_v_netman.erl new file mode 100644 index 0000000..90aa143 --- /dev/null +++ b/src/gmc_v_netman.erl @@ -0,0 +1,357 @@ +-module(gmc_v_netman). +-vsn("0.1.0"). +-author("Craig Everett "). +-copyright("QPQ AG "). +-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.