77 lines
1.6 KiB
Haxe
77 lines
1.6 KiB
Haxe
package bin.srsc;
|
|
|
|
import macros.rpc.RPC;
|
|
import lib.KVStore;
|
|
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 {
|
|
private var handle:ProcessHandle;
|
|
private var ressources:Map<String, NetworkID>;
|
|
|
|
public function new() {}
|
|
|
|
public function run(handle:ProcessHandle) {
|
|
this.handle = handle;
|
|
|
|
load();
|
|
|
|
RPC.generateRPCPackageHandle();
|
|
}
|
|
|
|
@rpc
|
|
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;
|
|
}
|
|
|
|
@rpc
|
|
public function unregister(name:String):Void {
|
|
if (ressources.exists(name)) {
|
|
ressources.remove(name);
|
|
}
|
|
save();
|
|
}
|
|
|
|
@rpc
|
|
public function get(name:String):Null<NetworkID> {
|
|
return ressources.get(name);
|
|
}
|
|
|
|
@rpc
|
|
public function list():Array<String> {
|
|
return [for (k in ressources.keys()) k];
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|