improved exporter

This commit is contained in:
Djeeberjr 2023-06-26 19:06:24 +02:00
parent 4dcc060e9a
commit 7bfe594b4b
7 changed files with 118 additions and 82 deletions

View File

@ -1,67 +0,0 @@
package bin;
import lib.exporter.Request;
import kernel.ps.ProcessHandle;
import lib.RessourceNames;
import lib.exporter.Export;
import kernel.ps.Process;
import kernel.log.Log;
import kernel.net.Package;
import kernel.net.Net;
import kernel.net.Package.GenericPackage;
using tink.CoreApi;
class ResManager implements Process {
private var handle:ProcessHandle;
private final exports:Map<String,Export> = [];
public function new() {}
public function run(handle:ProcessHandle) {
this.handle = handle;
Net.instance.registerProto("res",handlePackage);
}
public function register(id: String, export: Export): Future<Bool>{
if (exports.exists(id)){
handle.writeLine("Ressource already exists: " + id);
return Future.sync(false);
}
return registerName(id).map((success)->{
if (success){
exports.set(id,export);
}
return success;
});
}
private function handlePackage(pack: GenericPackage){
Log.debug("Handling ressource request" + pack);
var requestPack: Package<Request> = cast pack;
var id = requestPack.data.id;
if (!exports.exists(id)){
requestPack.respond(lib.exporter.Response.NotFound);
return;
}
var export = exports.get(id);
var response = export.handleRequest(requestPack.data);
requestPack.respond(response);
}
private function registerName(id: String){
return RessourceNames.register(id, Net.instance.networkID).map((res)->{
switch (res) {
case Success(data):
return data;
case Failure(err):
Log.error("Failed to register ressource: " + id + " " + err);
return false;
}
});
}
}

View File

@ -1,4 +1,4 @@
package bin;
package bin.exporter;
import lib.exporter.Export;
import lib.exporter.IExportable;
@ -35,7 +35,7 @@ class Res extends CLIAppBase {
return false;
}
var perf: IExportable = Peripheral.instance.getRedstone(addr);
var perf: kernel.peripherals.Redstone = Peripheral.instance.getRedstone(addr);
if (perf == null) {
handle.writeLine("Error: peripheral not found");
@ -43,13 +43,15 @@ class Res extends CLIAppBase {
}
return srv.register(name,new Export(perf)).map((res)->{
if (res) {
switch (res){
case Success(_):
handle.writeLine("Success");
} else {
handle.writeLine("Error");
return true;
case Failure(err):
handle.writeLine("Error: ");
handle.writeLine(Std.string(err));
return false;
}
return res;
});
},"<addr> <name>");
}

View File

@ -0,0 +1,91 @@
package bin.exporter;
import kernel.peripherals.Peripherals.Peripheral;
import lib.KVStore;
import lib.exporter.Request;
import kernel.ps.ProcessHandle;
import lib.RessourceNames;
import lib.exporter.Export;
import kernel.ps.Process;
import kernel.net.Package;
import kernel.net.Net;
import kernel.net.Package.GenericPackage;
using tink.CoreApi;
class ResManager implements Process {
private var handle:ProcessHandle;
private var exports:Map<String,Export> = [];
public function new() {}
public function run(handle:ProcessHandle) {
this.handle = handle;
Net.instance.registerProto("res",handlePackage);
load();
}
public function register(id: String, export: Export): Promise<Noise>{
if (exports.exists(id)){
return Promise.reject(new Error("Ressource already exists: " + id));
}
return registerName(id).next((success)->{
exports.set(id,export);
persist();
return null;
});
}
private function handlePackage(pack: GenericPackage){
var requestPack: Package<Request> = cast pack;
var id = requestPack.data.id;
if (!exports.exists(id)){
requestPack.respond(lib.exporter.Response.NotFound);
return;
}
var export = exports.get(id);
var response = export.handleRequest(requestPack.data);
requestPack.respond(response);
}
private function registerName(id: String){
return RessourceNames.register(id, Net.instance.networkID);
}
private function persist(){
var store = new KVStore("export");
var saveExports: Array<{name: String, addr: String, type: String}> =
[for (k => v in this.exports) {name: k, addr: v.getAddr(), type: v.getType()}];
store.set("exports",saveExports);
store.save();
}
private function load(){
var store = new KVStore("export");
var savedExports: Array<{name: String, addr: String, type: String}> = store.get("exports",[]);
for (export in savedExports){
var perph = Peripheral.instance.getFromType(export.addr,export.type);
if (perph == null){
handle.writeLine('Could not load export: ${export.name} on ${export.addr}');
continue;
}
// I dont know if cast is the best way to do this
// But since we know that this is a IExportable we can do this (I think)
exports.set(export.name, new Export(cast perph));
handle.writeLine('Loaded export: ${export.name} on ${export.addr}');
}
}
}

View File

@ -20,8 +20,8 @@ class DCEHack {
new bin.srsc.CLI(),
new bin.Perf(),
new bin.KSettings(),
new bin.ResManager(),
new bin.Res(),
new bin.exporter.ResManager(),
new bin.exporter.Res(),
];
}
}

View File

@ -1,7 +1,7 @@
package kernel.binstore;
import bin.Res;
import bin.ResManager;
import bin.exporter.Res;
import bin.exporter.ResManager;
import bin.KSettings;
import bin.Perf;
import bin.srsc.CLI;

View File

@ -1,14 +1,17 @@
package lib.exporter;
import kernel.peripherals.IPeripheral;
import kernel.log.Log;
using tink.CoreApi;
class Export {
private final exportConfig: ExportConfig;
private final peripheral: IPeripheral;
public function new(exportConfig: IExportable) {
this.exportConfig = exportConfig.export();
public function new<T: IExportable & IPeripheral>(exportPerph: T) {
this.peripheral = exportPerph;
this.exportConfig = exportPerph.export();
}
public function handleRequest(req: Request): Response {
@ -32,4 +35,12 @@ class Export {
return Get(value);
}
public function getType(): String {
return this.peripheral.getType();
}
public function getAddr(): String {
return this.peripheral.getAddr();
}
}

View File

@ -3,7 +3,6 @@ package lib.exporter;
import lib.exporter.Response;
typedef ExportConfig = {
type: String,
getDelegates: Map<String, Null<Int>->ValueType>,
// setDelegates: Map<String, (ValueType, Null<Int>)->ValueType>,
}