This commit is contained in:
Arnaud 2025-09-16 16:10:57 +02:00 committed by Eric
parent 13111095b0
commit d9866ecb26
No known key found for this signature in database
4 changed files with 76 additions and 3 deletions

View File

@ -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)

View File

@ -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("")

View File

@ -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);

View File

@ -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.} =