diff --git a/src/bin/ns/NameSystem.hx b/src/bin/ns/NameSystem.hx new file mode 100644 index 0000000..16aa7fe --- /dev/null +++ b/src/bin/ns/NameSystem.hx @@ -0,0 +1,71 @@ +package bin.ns; + +import lib.KVStore; +import haxe.ds.StringMap; +import kernel.net.Package.NetworkID; +import bin.ns.NameSystemRPC.INameSystemRPC; +import kernel.ps.ProcessHandle; +import kernel.ps.IProcess; + +using Lambda; +using tink.CoreApi; + +@:build(macros.Binstore.includeBin("NameSystem", ["ns", "namesystem"])) +class NameSystem implements IProcess implements INameSystemRPC { + private var handle:ProcessHandle; + + private var idRecords:StringMap = new StringMap(); + + public function new() {} + + public function run(handle:ProcessHandle) { + this.handle = handle; + + this.load(); + + kernel.net.Net.registerProto("ns", (p) -> { + NameSystemRPC.handlePackage(this, p); + }); + } + + private function load() { + var kv = new KVStore("NameSystem"); + kv.load(); + + this.idRecords = kv.get("idRecords", new StringMap()); + } + + private function save() { + var kv = new KVStore("NameSystem"); + + kv.set("idRecords", this.idRecords); + + kv.save(); + } + + public function setIDRecord(name:String, id:NetworkID):Noise { + this.idRecords.set(name, id); + this.save(); + return Noise; + } + + public function getIDRecord(name:String):Null { + return this.idRecords.get(name); + } + + public function removeIDRecord(name:String):Noise { + this.idRecords.remove(name); + this.save(); + return Noise; + } + + public function listIDRecords():Array<{name:String, id:NetworkID}> { + var rtn = []; + + for (k => v in this.idRecords) { + rtn.push({name: k, id: v}); + } + + return rtn; + } +} diff --git a/src/bin/ns/NameSystemCLI.hx b/src/bin/ns/NameSystemCLI.hx new file mode 100644 index 0000000..4b2452b --- /dev/null +++ b/src/bin/ns/NameSystemCLI.hx @@ -0,0 +1,89 @@ +package bin.ns; + +import lib.CLIAppBase; + +using tink.CoreApi; + +@:build(macros.Binstore.includeBin("NameSystemCLI", ["ns-cli"])) +class NameSystemCLI extends CLIAppBase { + public function new() { + registerAsyncSubcommand("get", (args) -> { + if (args.length < 1) { + return Future.sync(false); + } + + var ns = NameSystemRPC.getDefault(); + + return ns.getIDRecord(args[0]).map((r) -> { + switch r { + case Success(data): + handle.writeLine('Resolved: $data'); + case Failure(failure): + handle.writeLine('Failed: $failure'); + } + + return true; + }); + }, ""); + + registerAsyncSubcommand("register", (args) -> { + if (args.length < 2) { + return Future.sync(false); + } + + var id = Std.parseInt(args[1]); + + if (id == null) { + handle.writeLine("Not a valid id"); + return Future.sync(false); + } + + var ns = NameSystemRPC.getDefault(); + + return ns.setIDRecord(args[0], id).map((r) -> { + switch r { + case Success(_): + handle.writeLine('Set'); + case Failure(failure): + handle.writeLine('Failed: $failure'); + } + + return true; + }); + }, " "); + + registerAsyncSubcommand("list", (args) -> { + var ns = NameSystemRPC.getDefault(); + + return ns.listIDRecords().map((r) -> { + switch r { + case Success(data): + handle.writeLine('List: $data'); + case Failure(failure): + handle.writeLine('Failed: $failure'); + } + + return true; + }); + }); + + registerAsyncSubcommand("unregister", (args) -> { + if (args.length < 1) { + return Future.sync(false); + } + + var ns = NameSystemRPC.getDefault(); + + return ns.removeIDRecord(args[0]).map((r) -> { + switch r { + case Success(_): + handle.writeLine('Unregisterd'); + case Failure(failure): + handle.writeLine('Failed: $failure'); + } + + return true; + }); + }, ""); + } +} diff --git a/src/bin/ns/NameSystemRPC.hx b/src/bin/ns/NameSystemRPC.hx new file mode 100644 index 0000000..5faccb1 --- /dev/null +++ b/src/bin/ns/NameSystemRPC.hx @@ -0,0 +1,31 @@ +package bin.ns; + +import kernel.KernelSettings; +import macros.rpc.RPCBase; +import kernel.net.Package.NetworkID; + +using tink.CoreApi; + +interface INameSystemRPC { + function setIDRecord(name:String, id:NetworkID):Noise; + function getIDRecord(name:String):Null; + function removeIDRecord(name:String):Noise; + function listIDRecords():Array<{name:String, id:NetworkID}>; +} + +@:build(macros.rpc.RPC.buildRPC(INameSystemRPC)) +class NameSystemRPC extends RPCBase { + public static function getDefault():NameSystemRPC { + return new NameSystemRPC(KernelSettings.nameServer, "ns"); + } + + public static function resolve(input:String):Promise> { + if (input == "12345") { + return Promise.resolve(Std.parseInt(input)); + } + + var ns = getDefault(); + + return ns.getIDRecord(input); + } +}