refactored Net bin

This commit is contained in:
Djeeberjr 2023-05-31 19:12:44 +02:00
parent 779933be32
commit 8c041766c9

View File

@ -1,102 +1,65 @@
package bin; package bin;
import kernel.ps.ProcessHandle; import lib.CLIAppBase;
import kernel.ps.Process;
import kernel.peripherals.Peripherals.Peripheral; import kernel.peripherals.Peripherals.Peripheral;
import kernel.net.Routing; import kernel.net.Routing;
import haxe.ds.ReadOnlyArray;
using tink.CoreApi; using tink.CoreApi;
class Net implements Process { class Net extends CLIAppBase {
private var handle:ProcessHandle; public function new() {
registerSyncSubcommand("route", (args)->{
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<String>):Void {
var routes = Routing.instance.getRouteTable(); var routes = Routing.instance.getRouteTable();
for(k => v in routes) { for(k => v in routes) {
handle.write('${k} => ${v.interf.name()}(${v.cost})'); handle.writeLine('${k} => ${v.interf.name()}(${v.cost})');
}
}
private function iface(args:ReadOnlyArray<String>):Bool {
var modems = Peripheral.instance.getModems();
for (modem in modems) {
handle.write(modem.name());
} }
return true; return true;
});
registerSyncSubcommand("iface", (args)->{
var modems = Peripheral.instance.getModems();
for (modem in modems) {
handle.writeLine(modem.name());
} }
function ping(args:ReadOnlyArray<String>): Void { return true;
if (args.length != 1) { });
handle.write("Usage: net ping id");
return handle.close(false); 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<Int> = Std.parseInt(args[0]); var toID:Null<Int> = Std.parseInt(args[0]);
if (toID == null) { if (toID == null) {
handle.write("Invalid ID"); handle.write("Invalid ID");
return handle.close(false); return Future.sync(false);
} }
kernel.net.Net.instance.ping(toID).handle(result -> { return kernel.net.Net.instance.ping(toID).map(result -> {
switch (result){ switch (result){
case Success(_): case Success(_):
handle.write("Ping succeeded"); handle.write("Ping succeeded");
return handle.close();
case Failure(failure): case Failure(failure):
handle.write("Ping failed: " + failure); handle.write("Ping failed: " + failure);
return handle.close(false);
} }
return true;
}); });
} },"<id>");
function protos():Void {
var protos = kernel.net.Net.instance.getActiveProtocols();
for (proto in protos) {
handle.write(proto);
}
} }
} }