implmented basic ressource exporting

This commit is contained in:
Djeeberjr 2023-06-25 19:05:40 +02:00
parent 7996dd062d
commit e686afb298
11 changed files with 277 additions and 0 deletions

56
src/bin/Res.hx Normal file
View File

@ -0,0 +1,56 @@
package bin;
import lib.exporter.Export;
import lib.exporter.IExportable;
import kernel.peripherals.Peripherals.Peripheral;
import kernel.service.ServiceManager;
import lib.exporter.Import;
import lib.CLIAppBase;
class Res extends CLIAppBase {
public function new() {
registerAsyncSubcommand("get", (args)->{
var url = args[0];
return Import.get(url).map((res)->{
switch (res){
case Success(data):
handle.writeLine(Std.string(data));
case Failure(err):
handle.writeLine("Error: ");
handle.writeLine(Std.string(err));
}
return true;
});
},"<url>");
registerAsyncSubcommand("register",(args)->{
var srv: Null<ResManager> = ServiceManager.instance.get("resmgr");
var addr = args[0];
var name = args[1];
if (srv == null) {
handle.writeLine("Error: resmgr not found");
return false;
}
var perf: IExportable = Peripheral.instance.getRedstone(addr);
if (perf == null) {
handle.writeLine("Error: peripheral not found");
return false;
}
return srv.register(name,new Export(perf)).map((res)->{
if (res) {
handle.writeLine("Success");
} else {
handle.writeLine("Error");
}
return res;
});
},"<addr> <name>");
}
}

67
src/bin/ResManager.hx Normal file
View File

@ -0,0 +1,67 @@
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

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

View File

@ -1,5 +1,7 @@
package kernel.binstore; package kernel.binstore;
import bin.Res;
import bin.ResManager;
import bin.KSettings; import bin.KSettings;
import bin.Perf; import bin.Perf;
import bin.srsc.CLI; import bin.srsc.CLI;
@ -36,6 +38,8 @@ class BinStore {
{c: CLI, name: "SRSC CLI", aliases: ["srsc-cli"]}, {c: CLI, name: "SRSC CLI", aliases: ["srsc-cli"]},
{c: Perf, name: "Perf", aliases: ["perf"]}, {c: Perf, name: "Perf", aliases: ["perf"]},
{c: KSettings, name: "KSettings", aliases: ["ksettings","ks"]}, {c: KSettings, name: "KSettings", aliases: ["ksettings","ks"]},
{c: ResManager, name: "ResManager", aliases: ["resmanager","resmgr"]},
{c: Res, name: "Res", aliases: ["res"]}
]; ];
@:allow(kernel.Init) @:allow(kernel.Init)

View File

@ -0,0 +1,35 @@
package lib.exporter;
import kernel.log.Log;
using tink.CoreApi;
class Export {
private final exportConfig: ExportConfig;
public function new(exportConfig: IExportable) {
this.exportConfig = exportConfig.export();
}
public function handleRequest(req: Request): Response {
switch (req.operation){
case Get:
return handleGet(req);
case Set(value):
//TODO: implement
return NotFound;
}
}
private function handleGet(request: Request): Response {
if (!this.exportConfig.getDelegates.exists(request.field)){
Log.warn('Requested get field ${request.field} does not exist in ??');
return NotFound;
}
var delegate = this.exportConfig.getDelegates.get(request.field);
var value = delegate(request.index);
return Get(value);
}
}

View File

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

View File

@ -0,0 +1,5 @@
package lib.exporter;
interface IExportable {
public function export(): ExportConfig;
}

View File

@ -0,0 +1,28 @@
package lib.exporter;
import kernel.net.Net;
import kernel.net.Package.NetworkID;
using tink.CoreApi;
class Import {
public static function get(ressourceLocator: String): Promise<Response> {
var request = Request.fromString(ressourceLocator);
return RessourceNames.get(request.id).next((response)->{
return performRequest(response,request);
});
}
private static function performRequest(netID: NetworkID, request: Request): Promise<Response> {
return Net.instance.sendAndAwait(netID,"res",request).map((response)->{
switch (response){
case Success(data):
return Success(cast (data.data, Response));
case Failure(error):
return Failure(error);
}
});
}
}

View File

@ -0,0 +1,6 @@
package lib.exporter;
enum Operation {
Get;
Set(value: Dynamic);
}

View File

@ -0,0 +1,51 @@
package lib.exporter;
import lua.TableTools;
import lua.NativeStringTools;
class Request {
public final id:String;
public final field:String;
public final index: Null<Int>;
public final operation: Operation;
public function new(id:String, field:String, index:Null<Int>, operation:Operation) {
this.id = id;
this.field = field;
this.index = index;
this.operation = operation;
}
/**
Example:
"myfield[2]@myid"
"myfield@myid"
**/
public static function fromString(locator: String): Request {
if (StringTools.contains(locator,"[")){
var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)%[([%d]+)%]@(%a+)")());
var field = f[1];
var index = Std.parseInt(f[2]);
var id = f[3];
return new Request(id, field, index, Get);
}else{
var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)@(%a+)")());
var field = f[1];
var id = f[2];
return new Request(id, field, null, Get);
}
}
public function toString() {
if (index == null){
return field + "@" + id;
}else{
return field + "[" + index + "]@" + id;
}
}
}

View File

@ -0,0 +1,14 @@
package lib.exporter;
enum Response {
NotFound;
Set;
NotSet;
Get(value: ValueType);
}
enum ValueType {
Number(value: Int);
String(value: String);
Bool(value: Bool);
}