From 7bfe594b4bc12cb5f2ed35cd242ec913d9982efc Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 26 Jun 2023 19:06:24 +0200 Subject: [PATCH] improved exporter --- src/bin/ResManager.hx | 67 ----------------------- src/bin/{ => exporter}/Res.hx | 18 ++++--- src/bin/exporter/ResManager.hx | 91 ++++++++++++++++++++++++++++++++ src/kernel/DCEHack.hx | 4 +- src/kernel/binstore/BinStore.hx | 4 +- src/lib/exporter/Export.hx | 15 +++++- src/lib/exporter/ExportConfig.hx | 1 - 7 files changed, 118 insertions(+), 82 deletions(-) delete mode 100644 src/bin/ResManager.hx rename src/bin/{ => exporter}/Res.hx (74%) create mode 100644 src/bin/exporter/ResManager.hx diff --git a/src/bin/ResManager.hx b/src/bin/ResManager.hx deleted file mode 100644 index 4ceb2bb..0000000 --- a/src/bin/ResManager.hx +++ /dev/null @@ -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 = []; - - public function new() {} - - public function run(handle:ProcessHandle) { - this.handle = handle; - Net.instance.registerProto("res",handlePackage); - } - - public function register(id: String, export: Export): Future{ - 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 = 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; - } - }); - } -} diff --git a/src/bin/Res.hx b/src/bin/exporter/Res.hx similarity index 74% rename from src/bin/Res.hx rename to src/bin/exporter/Res.hx index b453490..82a9107 100644 --- a/src/bin/Res.hx +++ b/src/bin/exporter/Res.hx @@ -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) { - handle.writeLine("Success"); - } else { - handle.writeLine("Error"); + switch (res){ + case Success(_): + handle.writeLine("Success"); + return true; + case Failure(err): + handle.writeLine("Error: "); + handle.writeLine(Std.string(err)); + return false; } - - return res; }); }," "); } diff --git a/src/bin/exporter/ResManager.hx b/src/bin/exporter/ResManager.hx new file mode 100644 index 0000000..3027b31 --- /dev/null +++ b/src/bin/exporter/ResManager.hx @@ -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 = []; + + 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{ + 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 = 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}'); + } + + } +} diff --git a/src/kernel/DCEHack.hx b/src/kernel/DCEHack.hx index 19fbd8c..2a64a20 100644 --- a/src/kernel/DCEHack.hx +++ b/src/kernel/DCEHack.hx @@ -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(), ]; } } diff --git a/src/kernel/binstore/BinStore.hx b/src/kernel/binstore/BinStore.hx index 6b68496..eedacdf 100644 --- a/src/kernel/binstore/BinStore.hx +++ b/src/kernel/binstore/BinStore.hx @@ -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; diff --git a/src/lib/exporter/Export.hx b/src/lib/exporter/Export.hx index 81d3da6..718e5ba 100644 --- a/src/lib/exporter/Export.hx +++ b/src/lib/exporter/Export.hx @@ -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(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(); + } } diff --git a/src/lib/exporter/ExportConfig.hx b/src/lib/exporter/ExportConfig.hx index 7380deb..3346ae3 100644 --- a/src/lib/exporter/ExportConfig.hx +++ b/src/lib/exporter/ExportConfig.hx @@ -3,7 +3,6 @@ package lib.exporter; import lib.exporter.Response; typedef ExportConfig = { - type: String, getDelegates: Map->ValueType>, // setDelegates: Map)->ValueType>, }