Fix daemonapi and examples.
This commit is contained in:
parent
77f34af737
commit
d7d9d7bd40
|
@ -1,6 +1,5 @@
|
|||
import chronos, nimcrypto, strutils
|
||||
import ../libp2p/daemon/daemonapi
|
||||
import ../libp2p/[base58, multicodec, multiaddress, peer]
|
||||
import hexdump
|
||||
|
||||
const
|
||||
|
@ -12,6 +11,18 @@ proc dumpSubscribedPeers(api: DaemonAPI) {.async.} =
|
|||
for item in peers:
|
||||
echo item.pretty()
|
||||
|
||||
proc dumpAllPeers(api: DaemonAPI) {.async.} =
|
||||
var peers = await api.listPeers()
|
||||
echo "Current connected peers count = ", len(peers)
|
||||
for item in peers:
|
||||
echo item.peer.pretty()
|
||||
|
||||
proc monitor(api: DaemonAPI) {.async.} =
|
||||
while true:
|
||||
echo "Dumping all peers"
|
||||
await dumpAllPeers(api)
|
||||
await sleepAsync(5000)
|
||||
|
||||
proc main() {.async.} =
|
||||
echo "= Starting P2P bootnode"
|
||||
var api = await newDaemonApi({DHTFull, PSGossipSub})
|
||||
|
@ -24,6 +35,8 @@ proc main() {.async.} =
|
|||
if item.protoCode() == mcip4 or item.protoCode() == mcip6:
|
||||
echo $item & "/ipfs/" & id.peer.pretty()
|
||||
|
||||
asyncCheck monitor(api)
|
||||
|
||||
proc pubsubLogger(api: DaemonAPI,
|
||||
ticket: PubsubTicket,
|
||||
message: PubSubMessage): Future[bool] {.async.} =
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import chronos, nimcrypto, strutils, os
|
||||
import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress, peer]
|
||||
import ../libp2p/daemon/daemonapi
|
||||
|
||||
const
|
||||
PubSubTopic = "test-net"
|
||||
|
||||
proc main(bn: string) {.async.} =
|
||||
echo "= Starting P2P node"
|
||||
|
@ -22,11 +25,18 @@ proc main(bn: string) {.async.} =
|
|||
echo strdata
|
||||
result = true
|
||||
|
||||
var ticket = await api.pubsubSubscribe("test-net", pubsubLogger)
|
||||
var ticket = await api.pubsubSubscribe(PubSubTopic, pubsubLogger)
|
||||
|
||||
# Waiting for gossipsub interval
|
||||
while true:
|
||||
var peers = await api.pubsubListPeers(PubSubTopic)
|
||||
if len(peers) > 0:
|
||||
break
|
||||
await sleepAsync(1000)
|
||||
|
||||
var data = "HELLO\r\n"
|
||||
var msgData = cast[seq[byte]](data)
|
||||
await api.pubsubPublish("test-net", msgData)
|
||||
await api.pubsubPublish(PubSubTopic, msgData)
|
||||
|
||||
when isMainModule:
|
||||
if paramCount() != 1:
|
||||
|
|
|
@ -13,6 +13,8 @@ import chronos
|
|||
import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer
|
||||
import ../wire, ../protobuf/minprotobuf
|
||||
|
||||
export peer, multiaddress, multicodec, multihash, cid
|
||||
|
||||
when not defined(windows):
|
||||
import posix
|
||||
|
||||
|
@ -147,7 +149,6 @@ type
|
|||
DaemonRemoteError* = object of Exception
|
||||
DaemonLocalError* = object of Exception
|
||||
|
||||
|
||||
var daemonsCount {.threadvar.}: int
|
||||
|
||||
proc requestIdentity(): ProtoBuffer =
|
||||
|
@ -661,7 +662,7 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
|
|||
raise newException(DaemonLocalError, "Could not find daemon executable!")
|
||||
|
||||
# Starting daemon process
|
||||
# echo "Starting ", cmd, " ", args.join(" ")
|
||||
echo "Starting ", cmd, " ", args.join(" ")
|
||||
api.process = startProcess(cmd, "", args, env, {poStdErrToStdOut})
|
||||
# Waiting until daemon will not be bound to control socket.
|
||||
while true:
|
||||
|
|
Loading…
Reference in New Issue