added SRSC

This commit is contained in:
2023-06-04 16:28:26 +02:00
parent cd6808e62c
commit f8b226bae5
6 changed files with 301 additions and 1 deletions

92
src/bin/srsc/CLI.hx Normal file
View File

@@ -0,0 +1,92 @@
package bin.srsc;
import kernel.net.Package.NetworkID;
import lib.RessourceNames;
import lib.CLIAppBase;
using tink.CoreApi;
class CLI extends CLIAppBase {
public function new() {
registerAsyncSubcommand("get", (args) -> {
if (args.length < 2) {
handle.writeLine("Not enough arguments");
return Future.sync(false);
}
var id:NetworkID = Std.parseInt(args[0]);
if (id == null) {
handle.writeLine("Invalid id");
return Future.sync(false);
}
return RessourceNames.get(args[1], id).map((res) -> {
switch (res) {
case Success(data):
if (data == null) {
handle.writeLine("Not found");
} else {
handle.writeLine("Found: " + data);
}
case Failure(error):
handle.writeLine("Error: " + error);
}
return true;
});
}, "<id> <name>");
registerAsyncSubcommand("register", (args) -> {
if (args.length < 3) {
handle.writeLine("Not enough arguments");
return Future.sync(false);
}
var id:NetworkID = Std.parseInt(args[0]);
if (id == null) {
handle.writeLine("Invalid id");
return Future.sync(false);
}
var id2:NetworkID = Std.parseInt(args[2]);
if (id2 == null) {
handle.writeLine("Invalid id");
return Future.sync(false);
}
return RessourceNames.register(args[1], id2, id).map((res) -> {
switch (res) {
case Success(data):
handle.writeLine("Success");
case Failure(error):
handle.writeLine("Error: " + error);
}
return true;
});
}, "<id> <name> <id>");
registerAsyncSubcommand("unregister", (args) -> {
if (args.length < 2) {
handle.writeLine("Not enough arguments");
return Future.sync(false);
}
var id:NetworkID = Std.parseInt(args[0]);
if (id == null) {
handle.writeLine("Invalid id");
return Future.sync(false);
}
return RessourceNames.unregister(args[1], id).map((res) -> {
switch (res) {
case Success(data):
handle.writeLine("Success");
case Failure(error):
handle.writeLine("Error: " + error);
}
return true;
});
}, "<id> <name>");
}
}

View File

@@ -0,0 +1,32 @@
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;
}

View File

@@ -0,0 +1,104 @@
package bin.srsc;
import kernel.log.Log;
import lib.KVStore;
import bin.srsc.PackageTypes;
import kernel.net.Package;
import kernel.ps.ProcessHandle;
import kernel.ps.Process;
/**
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.
**/
class SiteRessourceController implements Process {
public static inline final SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO:String = "srsc";
private var handle:ProcessHandle;
private var ressources:Map<String, NetworkID>;
public function new() {}
public function run(handle:ProcessHandle) {
this.handle = handle;
load();
// Register proto
kernel.net.Net.instance.registerProto(SITE_CONTROLLER_RESSOURCE_MANAGER_PROTO, this.handleMsg);
}
private function handleMsg(pkg:Package) {
/*
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(pkg.data));
case "unregister":
pkg.respond(handleUnregister(pkg.data));
case "get":
pkg.respond(handleGet(pkg.data));
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};
}
public function register(name:String, netID:NetworkID):Bool {
if (ressources.exists(name) && ressources.get(name) != netID) {
return false;
}
ressources.set(name, netID);
save();
return true;
}
public function unregister(name:String) {
if (ressources.exists(name)) {
ressources.remove(name);
}
save();
}
public function get(name:String):Null<NetworkID> {
return ressources.get(name);
}
private function load() {
var store = KVStore.getStoreForClass();
var data:Null<Map<String, NetworkID>> = store.get("ressources");
if (data != null) {
this.ressources = data;
} else {
this.ressources = new Map<String, NetworkID>();
}
}
private function save() {
var store = KVStore.getStoreForClass();
store.set("ressources", this.ressources);
store.save();
}
}