mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-05 06:53:06 +00:00
Add peer id and add debug request
This commit is contained in:
parent
d9866ecb26
commit
314fad51ee
@ -92,6 +92,10 @@ package main
|
||||
return codex_spr(codexCtx, (CodexCallback) callback, resp);
|
||||
}
|
||||
|
||||
static int cGoCodexPeerId(void* codexCtx, void* resp) {
|
||||
return codex_peer_id(codexCtx, (CodexCallback) callback, resp);
|
||||
}
|
||||
|
||||
static int cGoCodexStart(void* codexCtx, void* resp) {
|
||||
return codex_start(codexCtx, (CodexCallback) callback, resp);
|
||||
}
|
||||
@ -165,16 +169,6 @@ 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"`
|
||||
@ -389,7 +383,7 @@ func (self *CodexNode) CodexDebug() (CodexDebugInfo, error) {
|
||||
return info, err
|
||||
}
|
||||
|
||||
func (self *CodexNode) CodexSpr() (Spr, error) {
|
||||
func (self *CodexNode) CodexSpr() (string, error) {
|
||||
bridge := newBridgeCtx()
|
||||
defer bridge.free()
|
||||
|
||||
@ -397,12 +391,18 @@ func (self *CodexNode) CodexSpr() (Spr, error) {
|
||||
return "", bridge.CallError("cGoCodexSpr")
|
||||
}
|
||||
|
||||
value, err := bridge.wait()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return bridge.wait()
|
||||
}
|
||||
|
||||
func (self *CodexNode) CodexPeerId() (string, error) {
|
||||
bridge := newBridgeCtx()
|
||||
defer bridge.free()
|
||||
|
||||
if C.cGoCodexPeerId(self.ctx, bridge.resp) != C.RET_OK {
|
||||
return "", bridge.CallError("cGoCodexPeerId")
|
||||
}
|
||||
|
||||
return Spr(value), nil
|
||||
return bridge.wait()
|
||||
}
|
||||
|
||||
func (self *CodexNode) CodexStart() error {
|
||||
@ -536,7 +536,14 @@ func main() {
|
||||
log.Fatal("Error happened:", err.Error())
|
||||
}
|
||||
|
||||
log.Println("Codex SPR:", spr.Json())
|
||||
log.Println("Codex SPR:", spr)
|
||||
|
||||
peerId, err := node.CodexPeerId()
|
||||
if err != nil {
|
||||
log.Fatal("Error happened:", err.Error())
|
||||
}
|
||||
|
||||
log.Println("Codex Peer Id:", peerId)
|
||||
|
||||
// Wait for a SIGINT or SIGTERM signal
|
||||
ch := make(chan os.Signal, 1)
|
||||
|
||||
@ -8,12 +8,14 @@ import chronos
|
||||
import ../ffi_types
|
||||
import ./requests/node_lifecycle_request
|
||||
import ./requests/node_info_request
|
||||
import ./requests/node_debug_request
|
||||
|
||||
from ../../codex/codex import CodexServer
|
||||
|
||||
type RequestType* {.pure.} = enum
|
||||
LIFECYCLE
|
||||
INFO
|
||||
DEBUG
|
||||
|
||||
type CodexThreadRequest* = object
|
||||
reqType: RequestType
|
||||
@ -84,6 +86,8 @@ proc process*(
|
||||
cast[ptr NodeLifecycleRequest](request[].reqContent).process(codex)
|
||||
of INFO:
|
||||
cast[ptr NodeInfoRequest](request[].reqContent).process(codex)
|
||||
of RequestType.DEBUG:
|
||||
cast[ptr NodeDebugRequest](request[].reqContent).process(codex)
|
||||
|
||||
handleRes(await retFut, request)
|
||||
|
||||
|
||||
@ -0,0 +1,60 @@
|
||||
## This file contains the lifecycle request type that will be handled.
|
||||
|
||||
import std/[options]
|
||||
import chronos
|
||||
import chronicles
|
||||
# import confutils
|
||||
import codexdht/discv5/spr
|
||||
# import ../../../codex/conf
|
||||
import ../../../codex/rest/json
|
||||
import ../../../codex/node
|
||||
|
||||
from ../../../codex/codex import CodexServer, node
|
||||
|
||||
type NodeDebugMsgType* = enum
|
||||
DEBUG
|
||||
|
||||
type NodeDebugRequest* = object
|
||||
operation: NodeDebugMsgType
|
||||
|
||||
proc createShared*(T: type NodeDebugRequest, op: NodeDebugMsgType): ptr type T =
|
||||
var ret = createShared(T)
|
||||
ret[].operation = op
|
||||
return ret
|
||||
|
||||
proc destroyShared(self: ptr NodeDebugRequest) =
|
||||
deallocShared(self)
|
||||
|
||||
proc getDebug(
|
||||
codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
let node = codex[].node
|
||||
let table = RestRoutingTable.init(node.discovery.protocol.routingTable)
|
||||
|
||||
let json =
|
||||
%*{
|
||||
"id": $node.switch.peerInfo.peerId,
|
||||
"addrs": node.switch.peerInfo.addrs.mapIt($it),
|
||||
"spr":
|
||||
if node.discovery.dhtRecord.isSome: node.discovery.dhtRecord.get.toURI else: "",
|
||||
"announceAddresses": node.discovery.announceAddrs,
|
||||
"table": table,
|
||||
}
|
||||
|
||||
return ok($json)
|
||||
|
||||
proc process*(
|
||||
self: ptr NodeDebugRequest, codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
defer:
|
||||
destroyShared(self)
|
||||
|
||||
case self.operation
|
||||
of NodeDebugMsgType.DEBUG:
|
||||
let res = (await getDebug(codex))
|
||||
if res.isErr:
|
||||
error "DEBUG failed", error = res.error
|
||||
return err($res.error)
|
||||
return res
|
||||
|
||||
return ok("")
|
||||
@ -13,8 +13,8 @@ from ../../../codex/codex import CodexServer, config, node
|
||||
|
||||
type NodeInfoMsgType* = enum
|
||||
REPO
|
||||
DEBUG
|
||||
SPR
|
||||
PEERID
|
||||
|
||||
type NodeInfoRequest* = object
|
||||
operation: NodeInfoMsgType
|
||||
@ -32,24 +32,6 @@ proc getRepo(
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
return ok($(codex[].config.dataDir))
|
||||
|
||||
proc getDebug(
|
||||
codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
let node = codex[].node
|
||||
let table = RestRoutingTable.init(node.discovery.protocol.routingTable)
|
||||
|
||||
let json =
|
||||
%*{
|
||||
"id": $node.switch.peerInfo.peerId,
|
||||
"addrs": node.switch.peerInfo.addrs.mapIt($it),
|
||||
"spr":
|
||||
if node.discovery.dhtRecord.isSome: node.discovery.dhtRecord.get.toURI else: "",
|
||||
"announceAddresses": node.discovery.announceAddrs,
|
||||
"table": table,
|
||||
}
|
||||
|
||||
return ok($json)
|
||||
|
||||
proc getSpr(
|
||||
codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
@ -59,6 +41,11 @@ proc getSpr(
|
||||
|
||||
return ok(spr.get.toURI)
|
||||
|
||||
proc getPeerId(
|
||||
codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
return ok($codex[].node.switch.peerInfo.peerId)
|
||||
|
||||
proc process*(
|
||||
self: ptr NodeInfoRequest, codex: ptr CodexServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
@ -72,16 +59,16 @@ proc process*(
|
||||
error "REPO failed", error = res.error
|
||||
return err($res.error)
|
||||
return res
|
||||
of DEBUG:
|
||||
let res = (await getDebug(codex))
|
||||
if res.isErr:
|
||||
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
|
||||
error "SPR failed", error = res.error
|
||||
return err($res.error)
|
||||
return res
|
||||
of PEERID:
|
||||
let res = (await getPeerId(codex))
|
||||
if res.isErr:
|
||||
error "PEERID failed", error = res.error
|
||||
return err($res.error)
|
||||
return res
|
||||
|
||||
|
||||
@ -55,6 +55,11 @@ int codex_spr(
|
||||
CodexCallback callback,
|
||||
void* userData);
|
||||
|
||||
int codex_peer_id(
|
||||
void* ctx,
|
||||
CodexCallback callback,
|
||||
void* userData);
|
||||
|
||||
int codex_start(void* ctx,
|
||||
CodexCallback callback,
|
||||
void* userData);
|
||||
|
||||
@ -31,6 +31,7 @@ import ./codex_context
|
||||
import ./codex_thread_requests/codex_thread_request
|
||||
import ./codex_thread_requests/requests/node_lifecycle_request
|
||||
import ./codex_thread_requests/requests/node_info_request
|
||||
import ./codex_thread_requests/requests/node_debug_request
|
||||
import ./ffi_types
|
||||
|
||||
from ../codex/conf import codexVersion
|
||||
@ -152,10 +153,10 @@ proc codex_debug(
|
||||
initializeLibrary()
|
||||
checkLibcodexParams(ctx, callback, userData)
|
||||
|
||||
let reqContent = NodeInfoRequest.createShared(NodeInfoMsgType.DEBUG)
|
||||
let reqContent = NodeDebugRequest.createShared(NodeDebugMsgType.DEBUG)
|
||||
|
||||
codex_context.sendRequestToCodexThread(
|
||||
ctx, RequestType.INFO, reqContent, callback, userData
|
||||
ctx, RequestType.DEBUG, reqContent, callback, userData
|
||||
).isOkOr:
|
||||
let msg = "libcodex error: " & $error
|
||||
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
||||
@ -180,6 +181,23 @@ proc codex_spr(
|
||||
|
||||
return RET_OK
|
||||
|
||||
proc codex_peer_id(
|
||||
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
|
||||
): cint {.dynlib, exportc.} =
|
||||
initializeLibrary()
|
||||
checkLibcodexParams(ctx, callback, userData)
|
||||
|
||||
let reqContent = NodeInfoRequest.createShared(NodeInfoMsgType.PEERID)
|
||||
|
||||
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.} =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user