From 54a3dc7de93dbba8b5df6ae568f120fe226e7c64 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Tue, 16 Sep 2025 16:10:57 +0200 Subject: [PATCH] Add spr --- examples/golang/codex.go | 37 +++++++++++++++++++ .../requests/node_info_request.nim | 20 ++++++++-- library/libcodex.h | 5 +++ library/libcodex.nim | 17 +++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/examples/golang/codex.go b/examples/golang/codex.go index ea95e9e0..9a60935d 100644 --- a/examples/golang/codex.go +++ b/examples/golang/codex.go @@ -88,6 +88,10 @@ package main return codex_debug(codexCtx, (CodexCallback) callback, resp); } + static int cGoCodexSpr(void* codexCtx, void* resp) { + return codex_spr(codexCtx, (CodexCallback) callback, resp); + } + static int cGoCodexStart(void* codexCtx, void* resp) { return codex_start(codexCtx, (CodexCallback) callback, resp); } @@ -161,6 +165,16 @@ const ( LevelDb RepoKind = "leveldb" ) +type Spr string + +type SprJson struct { + Spr string `json:"spr"` +} + +func (s Spr) Json() SprJson { + return SprJson{Spr: string(s)} +} + type CodexConfig struct { LogLevel LogLevel `json:"log-level,omitempty"` LogFormat LogFormat `json:"log-format,omitempty"` @@ -375,6 +389,22 @@ func (self *CodexNode) CodexDebug() (CodexDebugInfo, error) { return info, err } +func (self *CodexNode) CodexSpr() (Spr, error) { + bridge := newBridgeCtx() + defer bridge.free() + + if C.cGoCodexSpr(self.ctx, bridge.resp) != C.RET_OK { + return "", bridge.CallError("cGoCodexSpr") + } + + value, err := bridge.wait() + if err != nil { + return "", err + } + + return Spr(value), nil +} + func (self *CodexNode) CodexStart() error { bridge := newBridgeCtx() defer bridge.free() @@ -501,6 +531,13 @@ func main() { log.Println(string(pretty)) + spr, err := node.CodexSpr() + if err != nil { + log.Fatal("Error happened:", err.Error()) + } + + log.Println("Codex SPR:", spr.Json()) + // Wait for a SIGINT or SIGTERM signal ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) diff --git a/library/codex_thread_requests/requests/node_info_request.nim b/library/codex_thread_requests/requests/node_info_request.nim index cece26da..51e558cd 100644 --- a/library/codex_thread_requests/requests/node_info_request.nim +++ b/library/codex_thread_requests/requests/node_info_request.nim @@ -14,13 +14,12 @@ from ../../../codex/codex import CodexServer, config, node type NodeInfoMsgType* = enum REPO DEBUG + SPR type NodeInfoRequest* = object operation: NodeInfoMsgType -proc createShared*( - T: type NodeInfoRequest, op: NodeInfoMsgType, configJson: cstring = "" -): ptr type T = +proc createShared*(T: type NodeInfoRequest, op: NodeInfoMsgType): ptr type T = var ret = createShared(T) ret[].operation = op return ret @@ -51,6 +50,15 @@ proc getDebug( return ok($json) +proc getSpr( + codex: ptr CodexServer +): Future[Result[string, string]] {.async: (raises: []).} = + let spr = codex[].node.discovery.dhtRecord + if spr.isNone: + return err("No SPR record found") + + return ok(spr.get.toURI) + proc process*( self: ptr NodeInfoRequest, codex: ptr CodexServer ): Future[Result[string, string]] {.async: (raises: []).} = @@ -70,5 +78,11 @@ proc process*( error "DEBUG failed", error = res.error return err($res.error) return res + of SPR: + let res = (await getSpr(codex)) + if res.isErr: + error "DEBUG failed", error = res.error + return err($res.error) + return res return ok("") diff --git a/library/libcodex.h b/library/libcodex.h index e8dd5a25..c3a16ce9 100644 --- a/library/libcodex.h +++ b/library/libcodex.h @@ -50,6 +50,11 @@ int codex_debug( CodexCallback callback, void* userData); +int codex_spr( + void* ctx, + CodexCallback callback, + void* userData); + int codex_start(void* ctx, CodexCallback callback, void* userData); diff --git a/library/libcodex.nim b/library/libcodex.nim index 6627672c..a74ee08e 100644 --- a/library/libcodex.nim +++ b/library/libcodex.nim @@ -163,6 +163,23 @@ proc codex_debug( return RET_OK +proc codex_spr( + ctx: ptr CodexContext, callback: CodexCallback, userData: pointer +): cint {.dynlib, exportc.} = + initializeLibrary() + checkLibcodexParams(ctx, callback, userData) + + let reqContent = NodeInfoRequest.createShared(NodeInfoMsgType.SPR) + + codex_context.sendRequestToCodexThread( + ctx, RequestType.INFO, reqContent, callback, userData + ).isOkOr: + let msg = "libcodex error: " & $error + callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) + return RET_ERR + + return RET_OK + proc codex_destroy( ctx: ptr CodexContext, callback: CodexCallback, userData: pointer ): cint {.dynlib, exportc.} =