made SRSC a rpc thing

This commit is contained in:
Djeeberjr 2023-07-30 23:34:35 +02:00
parent da3f00acb6
commit 0fc2d4d397
6 changed files with 40 additions and 167 deletions

View File

@ -1,10 +1,10 @@
package bin.exporter; package bin.exporter;
import kernel.KernelSettings;
import kernel.peripherals.Peripherals.Peripheral; import kernel.peripherals.Peripherals.Peripheral;
import lib.KVStore; import lib.KVStore;
import lib.exporter.Request; import lib.exporter.Request;
import kernel.ps.ProcessHandle; import kernel.ps.ProcessHandle;
import lib.RessourceNames;
import lib.exporter.Export; import lib.exporter.Export;
import kernel.ps.Process; import kernel.ps.Process;
import kernel.net.Package; import kernel.net.Package;
@ -53,7 +53,8 @@ class ResManager implements Process {
} }
private function registerName(id:String) { 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() { private function persist() {

View File

@ -1,7 +1,7 @@
package bin.srsc; package bin.srsc;
import kernel.KernelSettings;
import kernel.net.Package.NetworkID; import kernel.net.Package.NetworkID;
import lib.RessourceNames;
import lib.CLIAppBase; import lib.CLIAppBase;
using tink.CoreApi; using tink.CoreApi;
@ -14,7 +14,9 @@ class CLI extends CLIAppBase {
return Future.sync(false); 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) { switch (res) {
case Success(data): case Success(data):
if (data == null) { if (data == null) {
@ -41,7 +43,9 @@ class CLI extends CLIAppBase {
return Future.sync(false); 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) { switch (res) {
case Success(data): case Success(data):
handle.writeLine("Success"); handle.writeLine("Success");
@ -59,20 +63,26 @@ class CLI extends CLIAppBase {
return Future.sync(false); return Future.sync(false);
} }
return RessourceNames.unregister(args[0]).map((res) -> { var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController);
switch (res) {
case Success(data):
handle.writeLine("Success");
case Failure(error):
handle.writeLine("Error: " + error);
}
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;
// });
}, "<name>"); }, "<name>");
registerAsyncSubcommand("list", (args) -> { registerAsyncSubcommand("list", (args) -> {
return RessourceNames.list().map((res) -> { var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController);
return rpc.list().map((res) -> {
switch (res) { switch (res) {
case Success(data): case Success(data):
for (name in data) { for (name in data) {

View File

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

View File

@ -1,19 +1,20 @@
package bin.srsc; package bin.srsc;
import macros.rpc.RPC;
import lib.KVStore; import lib.KVStore;
import bin.srsc.PackageTypes;
import kernel.net.Package; import kernel.net.Package;
import kernel.ps.ProcessHandle; import kernel.ps.ProcessHandle;
import kernel.ps.Process; import kernel.ps.Process;
using tink.CoreApi;
/** /**
The SiteRessourceController is responsible for the management of ressources on the site. 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 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. It also keeps track of the responsible NetID for each ressource. Kinda like a DNS.
**/ **/
@:build(macros.rpc.RPC.buildRPC())
class SiteRessourceController implements Process { class SiteRessourceController implements Process {
public static inline final SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO:String = "srsc";
private var handle:ProcessHandle; private var handle:ProcessHandle;
private var ressources:Map<String, NetworkID>; private var ressources:Map<String, NetworkID>;
@ -24,47 +25,10 @@ class SiteRessourceController implements Process {
load(); load();
// Register proto RPC.generateRPCPackageHandle();
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
public function register(name:String, netID:NetworkID):Bool { public function register(name:String, netID:NetworkID):Bool {
if (ressources.exists(name) && ressources.get(name) != netID) { if (ressources.exists(name) && ressources.get(name) != netID) {
return false; return false;
@ -75,18 +39,21 @@ class SiteRessourceController implements Process {
return true; return true;
} }
public function unregister(name:String) { @rpc
public function unregister(name:String):Void {
if (ressources.exists(name)) { if (ressources.exists(name)) {
ressources.remove(name); ressources.remove(name);
} }
save(); save();
} }
@rpc
public function get(name:String):Null<NetworkID> { public function get(name:String):Null<NetworkID> {
return ressources.get(name); return ressources.get(name);
} }
private inline function list():Array<String> { @rpc
public function list():Array<String> {
return [for (k in ressources.keys()) k]; return [for (k in ressources.keys()) k];
} }

View File

@ -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<Null<NetworkID>> {
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<Bool> {
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<Noise> {
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<Array<String>> {
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);
}
});
}
}

View File

@ -1,5 +1,6 @@
package lib.exporter; package lib.exporter;
import kernel.KernelSettings;
import kernel.net.Net; import kernel.net.Net;
import kernel.net.Package.NetworkID; import kernel.net.Package.NetworkID;
@ -8,8 +9,9 @@ using tink.CoreApi;
class Import { class Import {
public static function get(ressourceLocator:String):Promise<Response> { public static function get(ressourceLocator:String):Promise<Response> {
var request = Request.fromString(ressourceLocator); 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); return performRequest(response, request);
}); });
} }