Compare commits

..

7 Commits

Author SHA1 Message Date
c86d0e1d8b refactored Redstone bin 2023-05-31 19:19:17 +02:00
8c041766c9 refactored Net bin 2023-05-31 19:12:44 +02:00
779933be32 refactored GPS bin 2023-05-31 18:57:10 +02:00
06b3e37138 refactored Disk bin to use CLIBaseApp 2023-05-31 18:40:14 +02:00
2a7f02c5e3 fixed null synaopsis 2023-05-31 18:39:36 +02:00
e11a9383ee added callback dispose in ps handle 2023-05-31 15:17:19 +02:00
182a2bce7d fixed termial newline handle 2023-05-29 22:10:25 +02:00
7 changed files with 261 additions and 315 deletions

View File

@@ -1,24 +1,14 @@
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;
using tink.CoreApi; using tink.CoreApi;
using Lambda; using Lambda;
class Disk implements Process { class Disk extends CLIAppBase {
private var handle:ProcessHandle; public function new() {
registerSyncSubcommand("ls", (args)->{
public function new() {}
public function run(handle:ProcessHandle):Void {
this.handle = handle;
var subcommand = handle.args[0];
var driveAddr:Null<String> = handle.args[1];
switch (subcommand) {
case "ls":
Peripheral.instance.getDrives().foreach(drive -> { Peripheral.instance.getDrives().foreach(drive -> {
var addr = drive.getAddr(); var addr = drive.getAddr();
var label = drive.getDiskLabel(); var label = drive.getDiskLabel();
@@ -36,54 +26,58 @@ class Disk implements Process {
return true; return true;
}); });
case "play": });
var drive = Peripheral.instance.getDrive(driveAddr);
registerSyncSubcommand("play", (args)->{
if (drive == null){ if (args.length < 1){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Missing drive address");
} return false;
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr); return audioDiskPlayPause(args[0], true);
} },"<drive>");
if (!drive.hasAudio()){ registerSyncSubcommand("stop", (args) -> {
handle.writeLine("Disk in drive " + driveAddr + " does not have audio"); if (args.length < 1){
} handle.writeLine("Missing drive address");
return false;
drive.playAudio(); }
case "stop":
var drive = Peripheral.instance.getDrive(driveAddr); return audioDiskPlayPause(args[0], false);
});
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); registerSyncSubcommand("eject", (args)->{
} if (args.length < 1){
handle.writeLine("Missing drive address");
if (!drive.isDiskPresent()){ return false;
handle.writeLine("No disk in drive: " + driveAddr); }
}
var driveAddr = args[0];
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
}
drive.stopAudio();
case "eject":
var drive = Peripheral.instance.getDrive(driveAddr); var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){ if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Drive not found: " + driveAddr);
return false;
} }
if (!drive.isDiskPresent()){ if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr); handle.writeLine("No disk in drive: " + driveAddr);
return false;
} }
drive.ejectDisk(); drive.ejectDisk();
case "lable": return true;
},"<drive>");
registerSyncSubcommand("lable",(args) -> {
if (args.length < 1){
handle.writeLine("Missing drive address");
return false;
}
var driveAddr = args[0];
var drive = Peripheral.instance.getDrive(driveAddr); var drive = Peripheral.instance.getDrive(driveAddr);
var label:String = handle.args[2]; var label:String = args[1];
if (drive == null){ if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Drive not found: " + driveAddr);
@@ -99,26 +93,38 @@ class Disk implements Process {
var err = drive.setDiskLabel(label); var err = drive.setDiskLabel(label);
if (err != null){ if (err != null){
handle.writeLine("Failed to set lable"); handle.writeLine("Failed to set lable");
return false;
} }
} }
case "help":
case null:
printHelp();
default:
handle.writeLine("Unknown subcommand: " + subcommand);
printHelp();
}
return handle.close(true); return true;
},"<drive> [label]");
} }
private function printHelp() { private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool {
handle.writeLine("Usage: disk <subcommand> [args]"); var drive = Peripheral.instance.getDrive(driveAddr);
handle.writeLine("Subcommands:");
handle.writeLine(" ls"); if (drive == null){
handle.writeLine(" play <drive>"); handle.writeLine("Drive not found: " + driveAddr);
handle.writeLine(" stop <drive>"); return false;
handle.writeLine(" eject <drive>"); }
handle.writeLine(" label <drive> [label]");
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
return false;
}
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
return false;
}
if (play){
drive.playAudio();
}else{
drive.stopAudio();
}
return true;
} }
} }

View File

@@ -1,60 +1,15 @@
package bin; package bin;
import kernel.ps.ProcessHandle; import lib.CLIAppBase;
import kernel.ps.Process;
import kernel.gps.INS; import kernel.gps.INS;
import lib.Pos3; import lib.Pos3;
import lib.Vec.Vec3; import lib.Vec.Vec3;
using tink.CoreApi; using tink.CoreApi;
class GPS implements Process { class GPS extends CLIAppBase {
private var handle:ProcessHandle; public function new() {
registerSyncSubcommand("set", (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 "set":
handle.close(setManuelPos(subcommand_args));
case "status":
handle.close(getGPSStatus());
case "locate":
kernel.gps.GPS.instance.locate().handle((pos)->{
if (pos != null) {
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
handle.close(true);
} else {
handle.writeLine("Position not available");
handle.close(false);
}
});
case "ins":
INS.instance.align().handle(()->{
handle.writeLine("INS aligned");
handle.close(true);
});
default:
handle.writeLine("Unknown subcommand: " + subcommand);
printHelp();
handle.close(false);
}
}
private function printHelp(){
handle.writeLine("GPS commands:");
handle.writeLine("set <x> <y> <z> - set manual position");
handle.writeLine("status - get current position and accuracy");
handle.writeLine("locate - get current position");
handle.writeLine("ins - align INS");
}
private function setManuelPos(args: Array<String>): Bool {
var x: Float = Std.parseFloat(args[0]); var x: Float = Std.parseFloat(args[0]);
var y: Float = Std.parseFloat(args[1]); var y: Float = Std.parseFloat(args[1]);
var z: Float = Std.parseFloat(args[2]); var z: Float = Std.parseFloat(args[2]);
@@ -64,10 +19,9 @@ class GPS implements Process {
kernel.gps.GPS.instance.setManualPosition(pos); kernel.gps.GPS.instance.setManualPosition(pos);
return true; return true;
} },"<x> <y> <z>");
private function getGPSStatus(): Bool {
registerSyncSubcommand("status",(args)->{
var pos = kernel.gps.GPS.instance.getPosition(); var pos = kernel.gps.GPS.instance.getPosition();
if (pos != null) { if (pos != null) {
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
@@ -93,5 +47,24 @@ class GPS implements Process {
} }
return true; return true;
});
registerAsyncSubcommand("locate",(args)->{
return kernel.gps.GPS.instance.locate().map((pos)->{
if (pos != null) {
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
} else {
handle.writeLine("Position not available");
}
return true;
});
});
registerAsyncSubcommand("ins",(args)->{
return INS.instance.align().map((_)->{
handle.writeLine("INS aligned");
return true;
});
});
} }
} }

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);
}
} }
} }

View File

@@ -1,44 +1,26 @@
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.peripherals.Side;
using tink.CoreApi; using tink.CoreApi;
class Redstone implements Process { class Redstone extends CLIAppBase{
public function new() {} public function new() {
registerSyncSubcommand("on", (args)-> {
Peripheral.instance.getRedstone(args[0]).setOutput(true);
return true;
});
public function run(handle: ProcessHandle):Void { registerSyncSubcommand("off", (args)-> {
var subcommand = handle.args[0]; Peripheral.instance.getRedstone(args[0]).setOutput(false);
return true;
});
if (subcommand == null) { registerSyncSubcommand("get", (args)-> {
handle.write("Usage: redstone <on|off|get> <side>"); var value = Peripheral.instance.getRedstone(args[0]).getAnalogInput();
return handle.close(false);
}
var side:Null<Side> = handle.args[1];
if (side == null) {
handle.write("Invalid side");
return handle.close(false);
}
switch (subcommand) {
case "on":
Peripheral.instance.getRedstone(side).setOutput(true);
case "off":
Peripheral.instance.getRedstone(side).setOutput(false);
case "get":
var value = Peripheral.instance.getRedstone(side).getAnalogInput();
handle.write("Analog input: " + value); handle.write("Analog input: " + value);
case "help": return true;
handle.write("Usage: redstone <on|off|get> <side>"); });
default:
handle.write("Invalid subcommand");
return handle.close(false);
}
return handle.close();
} }
} }

View File

@@ -117,11 +117,24 @@ class Terminal implements Process {
ProcessManager.run(ps,{ ProcessManager.run(ps,{
args: commandArgs, args: commandArgs,
onWrite: (s:String) -> { onWrite: (s:String) -> {
if (s == "") {
return;
}
if (!hadInput) { if (!hadInput) {
// Add a new line, so that the input is not on the same line as the command
this.backlog.push(""); this.backlog.push("");
hadInput = true; hadInput = true;
} }
for (line in s.split("\n")) {
if (line == ""){
this.backlog.push("");
} else {
this.backlog[this.backlog.length - 1] += s; this.backlog[this.backlog.length - 1] += s;
}
}
this.requestRender(); this.requestRender();
}, },
onExit: (success:Bool) -> { onExit: (success:Bool) -> {

View File

@@ -20,6 +20,7 @@ class ProcessHandle {
private final closeFuture: Future<Bool>; private final closeFuture: Future<Bool>;
private var closeFutureResolev: Bool -> Void; private var closeFutureResolev: Bool -> Void;
private final windowContexts: Array<WindowContext> = []; private final windowContexts: Array<WindowContext> = [];
private final cbLinks:Array<CallbackLink> = [];
@:allow(kernel.ps.ProcessManager) @:allow(kernel.ps.ProcessManager)
private function new(config: HandleConfig,pid: PID) { private function new(config: HandleConfig,pid: PID) {
@@ -34,8 +35,6 @@ class ProcessHandle {
if (this.config.onExit != null) { if (this.config.onExit != null) {
this.closeFuture.handle(this.config.onExit); this.closeFuture.handle(this.config.onExit);
} }
this.closeFuture.eager();
} }
public function onExit(): Future<Bool> { public function onExit(): Future<Bool> {
@@ -75,7 +74,17 @@ class ProcessHandle {
} }
private function dispose() { private function dispose() {
// TODO for (link in this.cbLinks) {
link.cancel();
}
}
public function getPid(): PID {
return this.pid;
}
public function addCallbackLink(link: CallbackLink) {
this.cbLinks.push(link);
} }
function get_args():ReadOnlyArray<String> { function get_args():ReadOnlyArray<String> {

View File

@@ -40,12 +40,12 @@ abstract class CLIAppBase implements Process {
private function registerSyncSubcommand(command: String, callback: (Array<String>) -> Bool, synopsis: String = null) { private function registerSyncSubcommand(command: String, callback: (Array<String>) -> Bool, synopsis: String = null) {
_subcommandsSync.set(command, callback); _subcommandsSync.set(command, callback);
_subcommandsSynopsis.push(command + " " + synopsis); _subcommandsSynopsis.push(command + " " + (synopsis ?? ""));
} }
private function registerAsyncSubcommand(command: String, callback: (Array<String>) -> Future<Bool>, synopsis: String = null) { private function registerAsyncSubcommand(command: String, callback: (Array<String>) -> Future<Bool>, synopsis: String = null) {
_subcommandsAsync.set(command, callback); _subcommandsAsync.set(command, callback);
_subcommandsSynopsis.push(command + " " + synopsis); _subcommandsSynopsis.push(command + " " + (synopsis ?? ""));
} }
private function printHelp() { private function printHelp() {