From 0fc2d4d3975af18fe4bbc3157923a9b07baf9dff Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 30 Jul 2023 23:34:35 +0200 Subject: [PATCH] made SRSC a rpc thing --- src/bin/exporter/ResManager.hx | 5 +- src/bin/srsc/CLI.hx | 36 ++++++++----- src/bin/srsc/PackageTypes.hx | 36 ------------- src/bin/srsc/SiteRessourceController.hx | 55 ++++--------------- src/lib/RessourceNames.hx | 71 ------------------------- src/lib/exporter/Import.hx | 4 +- 6 files changed, 40 insertions(+), 167 deletions(-) delete mode 100644 src/bin/srsc/PackageTypes.hx delete mode 100644 src/lib/RessourceNames.hx diff --git a/src/bin/exporter/ResManager.hx b/src/bin/exporter/ResManager.hx index 319ecaa..7e4f51f 100644 --- a/src/bin/exporter/ResManager.hx +++ b/src/bin/exporter/ResManager.hx @@ -1,10 +1,10 @@ package bin.exporter; +import kernel.KernelSettings; 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; @@ -53,7 +53,8 @@ class ResManager implements Process { } private function registerName(id:String) { - return RessourceNames.register(id, Net.networkID); + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); + return rpc.register(id, Net.networkID); } private function persist() { diff --git a/src/bin/srsc/CLI.hx b/src/bin/srsc/CLI.hx index 8a27fa8..8c3d748 100644 --- a/src/bin/srsc/CLI.hx +++ b/src/bin/srsc/CLI.hx @@ -1,7 +1,7 @@ package bin.srsc; +import kernel.KernelSettings; import kernel.net.Package.NetworkID; -import lib.RessourceNames; import lib.CLIAppBase; using tink.CoreApi; @@ -14,7 +14,9 @@ class CLI extends CLIAppBase { return Future.sync(false); } - return RessourceNames.get(args[0]).map((res) -> { + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); + + return rpc.get(args[0]).map((res) -> { switch (res) { case Success(data): if (data == null) { @@ -41,7 +43,9 @@ class CLI extends CLIAppBase { return Future.sync(false); } - return RessourceNames.register(args[0], id).map((res) -> { + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); + + return rpc.register(args[0], id).map((res) -> { switch (res) { case Success(data): handle.writeLine("Success"); @@ -59,20 +63,26 @@ class CLI extends CLIAppBase { return Future.sync(false); } - return RessourceNames.unregister(args[0]).map((res) -> { - switch (res) { - case Success(data): - handle.writeLine("Success"); - case Failure(error): - handle.writeLine("Error: " + error); - } + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); - return true; - }); + // FIXME + return Future.sync(true); + // return rpc.unregister(args[0]).map((res) -> { + // switch (res) { + // case Success(_): + // handle.writeLine("Success"); + // case Failure(error): + // handle.writeLine("Error: " + error); + // } + + // return true; + // }); }, ""); registerAsyncSubcommand("list", (args) -> { - return RessourceNames.list().map((res) -> { + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); + + return rpc.list().map((res) -> { switch (res) { case Success(data): for (name in data) { diff --git a/src/bin/srsc/PackageTypes.hx b/src/bin/srsc/PackageTypes.hx deleted file mode 100644 index eb27dd9..0000000 --- a/src/bin/srsc/PackageTypes.hx +++ /dev/null @@ -1,36 +0,0 @@ -package bin.srsc; - -import kernel.net.Package.NetworkID; - -typedef GetRequest = { - public var ?type:String; - public var name:String; -} - -typedef GetResponse = { - public var success:Bool; - public var netID:NetworkID; -} - -typedef RegisterRequest = { - public var ?type:String; - public var name:String; - public var netID:NetworkID; -} - -typedef RegisterResponse = { - public var success:Bool; -} - -typedef UnregisterRequest = { - public var ?type:String; - public var name:String; -} - -typedef UnregisterResponse = { - public var success:Bool; -} - -typedef ListRequest = { - public var ?type:String; -} diff --git a/src/bin/srsc/SiteRessourceController.hx b/src/bin/srsc/SiteRessourceController.hx index 29e7eb2..816430e 100644 --- a/src/bin/srsc/SiteRessourceController.hx +++ b/src/bin/srsc/SiteRessourceController.hx @@ -1,19 +1,20 @@ package bin.srsc; +import macros.rpc.RPC; import lib.KVStore; -import bin.srsc.PackageTypes; import kernel.net.Package; import kernel.ps.ProcessHandle; import kernel.ps.Process; +using tink.CoreApi; + /** The SiteRessourceController is responsible for the management of ressources on the site. It makes sure that ressources are have a unique id and that they are not duplicated. It also keeps track of the responsible NetID for each ressource. Kinda like a DNS. **/ +@:build(macros.rpc.RPC.buildRPC()) class SiteRessourceController implements Process { - public static inline final SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO:String = "srsc"; - private var handle:ProcessHandle; private var ressources:Map; @@ -24,47 +25,10 @@ class SiteRessourceController implements Process { load(); - // Register proto - kernel.net.Net.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg); - } - - private function handleMsg(pkg:GenericPackage) { - /* - There are 3 types of messages: - - Register a new ressource name - - Unregister a ressource name - - Get the responsible NetID for a ressource name - */ - - switch (pkg.data.type) { - case "register": - pkg.respond(handleRegister(cast pkg.data)); - case "unregister": - pkg.respond(handleUnregister(cast pkg.data)); - case "get": - pkg.respond(handleGet(cast pkg.data)); - case "list": - pkg.respond(list()); - default: - handle.writeLine("Unknown message type: " + pkg.data.type); - } - } - - private function handleGet(data:GetRequest):GetResponse { - var res = get(data.name); - return {success: res != null, netID: res}; - } - - private function handleRegister(data:RegisterRequest):RegisterResponse { - var res = register(data.name, data.netID); - return {success: res}; - } - - private function handleUnregister(data:UnregisterRequest):UnregisterResponse { - unregister(data.name); - return {success: true}; + RPC.generateRPCPackageHandle(); } + @rpc public function register(name:String, netID:NetworkID):Bool { if (ressources.exists(name) && ressources.get(name) != netID) { return false; @@ -75,18 +39,21 @@ class SiteRessourceController implements Process { return true; } - public function unregister(name:String) { + @rpc + public function unregister(name:String):Void { if (ressources.exists(name)) { ressources.remove(name); } save(); } + @rpc public function get(name:String):Null { return ressources.get(name); } - private inline function list():Array { + @rpc + public function list():Array { return [for (k in ressources.keys()) k]; } diff --git a/src/lib/RessourceNames.hx b/src/lib/RessourceNames.hx deleted file mode 100644 index 998ca6c..0000000 --- a/src/lib/RessourceNames.hx +++ /dev/null @@ -1,71 +0,0 @@ -package lib; - -import bin.srsc.PackageTypes.ListRequest; -import kernel.KernelSettings; -import bin.srsc.PackageTypes.UnregisterRequest; -import bin.srsc.PackageTypes.RegisterRequest; -import bin.srsc.PackageTypes.GetRequest; -import bin.srsc.SiteRessourceController; -import kernel.net.Net; -import kernel.net.Package.NetworkID; - -using tink.CoreApi; - -class RessourceNames { - public static function get(name:String, controllerID:NetworkID = -1):Promise> { - if (controllerID == -1) - controllerID = KernelSettings.siteController; - - var payload:GetRequest = {name: name, type: "get"}; - - return Net.sendAndAwait(controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload).map((res) -> { - switch (res) { - case Success(pkg): - return Success(pkg.data.netID); - case Failure(error): - return Failure(error); - } - }); - } - - public static function register(name:String, netID:NetworkID, controllerID:NetworkID = -1):Promise { - if (controllerID == -1) - controllerID = KernelSettings.siteController; - - var payload:RegisterRequest = {name: name, netID: netID, type: "register"}; - - return Net.sendAndAwait(controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload).map((res) -> { - switch (res) { - case Success(pkg): - return Success(pkg.data.success); - case Failure(error): - return Failure(error); - } - }); - } - - public static function unregister(name:String, controllerID:NetworkID = -1):Promise { - if (controllerID == -1) - controllerID = KernelSettings.siteController; - - var payload:UnregisterRequest = {name: name, type: "unregister"}; - - return Net.sendAndAwait(controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload); - } - - public static function list(controllerID:NetworkID = -1):Promise> { - if (controllerID == -1) - controllerID = KernelSettings.siteController; - - var payload:ListRequest = {type: "list"}; - - return Net.sendAndAwait(controllerID, SiteRessourceController.SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, payload).map(res -> { - switch (res) { - case Success(pkg): - return Success(pkg.data); - case Failure(error): - return Failure(error); - } - }); - } -} diff --git a/src/lib/exporter/Import.hx b/src/lib/exporter/Import.hx index 5915b86..e153736 100644 --- a/src/lib/exporter/Import.hx +++ b/src/lib/exporter/Import.hx @@ -1,5 +1,6 @@ package lib.exporter; +import kernel.KernelSettings; import kernel.net.Net; import kernel.net.Package.NetworkID; @@ -8,8 +9,9 @@ using tink.CoreApi; class Import { public static function get(ressourceLocator:String):Promise { var request = Request.fromString(ressourceLocator); + var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); - return RessourceNames.get(request.id).next((response) -> { + return rpc.get(request.id).next((response) -> { return performRequest(response, request); }); }