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)->{
var routes = Routing.instance.getRouteTable();
public function new() {} for(k => v in routes) {
handle.writeLine('${k} => ${v.interf.name()}(${v.cost})');
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();
for(k => v in routes) {
handle.write('${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;
}
function ping(args:ReadOnlyArray<String>): Void {
if (args.length != 1) {
handle.write("Usage: net ping id");
return handle.close(false);
}
var toID:Null<Int> = 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);
} }
return true;
}); });
}
function protos():Void { registerSyncSubcommand("iface", (args)->{
var protos = kernel.net.Net.instance.getActiveProtocols(); var modems = Peripheral.instance.getModems();
for (proto in protos) { for (modem in modems) {
handle.write(proto); 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<Int> = 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;
});
},"<id>");
} }
} }