From 8c041766c95a29685807551227cefccbc5bbf13b Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 31 May 2023 19:12:44 +0200 Subject: [PATCH] refactored Net bin --- src/bin/Net.hx | 139 ++++++++++++++++++------------------------------- 1 file changed, 51 insertions(+), 88 deletions(-) diff --git a/src/bin/Net.hx b/src/bin/Net.hx index 0026948..9cfa223 100644 --- a/src/bin/Net.hx +++ b/src/bin/Net.hx @@ -1,102 +1,65 @@ package bin; -import kernel.ps.ProcessHandle; -import kernel.ps.Process; +import lib.CLIAppBase; import kernel.peripherals.Peripherals.Peripheral; import kernel.net.Routing; -import haxe.ds.ReadOnlyArray; using tink.CoreApi; -class Net implements Process { - private var handle:ProcessHandle; +class Net extends CLIAppBase { + public function new() { + registerSyncSubcommand("route", (args)->{ + var routes = Routing.instance.getRouteTable(); - public function new() {} - - public function run(handle:ProcessHandle):Void { - this.handle = handle; - - var subcommand = handle.args[0]; - var subcommand_args = handle.args.slice(1); - - switch (subcommand) { - case "route": - route(subcommand_args); - return handle.close(); - case "iface": - iface(subcommand_args); - return handle.close(); - case "help": - printHelp(); - return handle.close(); - case "ping": - ping(subcommand_args); - // Closes itself - case "proto": - protos(); - return handle.close(); - default: - handle.write("Unknown subcommand: " + subcommand); - printHelp(); - return handle.close(false); - } - } - - private function printHelp() { - handle.write("net route"); - handle.write("net iface"); - handle.write("net help"); - handle.write("net proto"); - } - - private function route(args:ReadOnlyArray):Void { - var routes = Routing.instance.getRouteTable(); - - for(k => v in routes) { - handle.write('${k} => ${v.interf.name()}(${v.cost})'); - } - } - - private function iface(args:ReadOnlyArray):Bool { - var modems = Peripheral.instance.getModems(); - - for (modem in modems) { - handle.write(modem.name()); - } - - return true; - } - - function ping(args:ReadOnlyArray): Void { - if (args.length != 1) { - handle.write("Usage: net ping id"); - return handle.close(false); - } - - var toID:Null = Std.parseInt(args[0]); - - if (toID == null) { - handle.write("Invalid ID"); - return handle.close(false); - } - - kernel.net.Net.instance.ping(toID).handle(result -> { - switch (result){ - case Success(_): - handle.write("Ping succeeded"); - return handle.close(); - case Failure(failure): - handle.write("Ping failed: " + failure); - return handle.close(false); + for(k => v in routes) { + handle.writeLine('${k} => ${v.interf.name()}(${v.cost})'); } + + return true; }); - } - function protos():Void { - var protos = kernel.net.Net.instance.getActiveProtocols(); + registerSyncSubcommand("iface", (args)->{ + var modems = Peripheral.instance.getModems(); - for (proto in protos) { - handle.write(proto); - } + for (modem in modems) { + handle.writeLine(modem.name()); + } + + return true; + }); + + registerSyncSubcommand("proto",(args)->{ + var protos = kernel.net.Net.instance.getActiveProtocols(); + + for (proto in protos) { + handle.writeLine(proto); + } + + return true; + }); + + registerAsyncSubcommand("ping",(args)->{ + if (args.length < 1) { + return Future.sync(false); + } + + var toID:Null = Std.parseInt(args[0]); + + if (toID == null) { + handle.write("Invalid ID"); + return Future.sync(false); + } + + return kernel.net.Net.instance.ping(toID).map(result -> { + switch (result){ + case Success(_): + handle.write("Ping succeeded"); + case Failure(failure): + handle.write("Ping failed: " + failure); + } + + return true; + }); + },""); } }