refactored Net bin
This commit is contained in:
parent
779933be32
commit
8c041766c9
139
src/bin/Net.hx
139
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<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);
|
||||
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<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>");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user