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