implmented basic ressource exporting
This commit is contained in:
56
src/bin/Res.hx
Normal file
56
src/bin/Res.hx
Normal 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
67
src/bin/ResManager.hx
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user