From d7d9d7bd409c923e07bc99cab5551ec35af5f8b2 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 01:57:18 +0200 Subject: [PATCH] Fix daemonapi and examples. --- examples/bootstrap.nim | 15 ++++++++++++++- examples/node.nim | 16 +++++++++++++--- libp2p/daemon/daemonapi.nim | 5 +++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/examples/bootstrap.nim b/examples/bootstrap.nim index 0a4014c3d..1d2ffff15 100644 --- a/examples/bootstrap.nim +++ b/examples/bootstrap.nim @@ -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.} = diff --git a/examples/node.nim b/examples/node.nim index 8bab23edd..0a77a3828 100644 --- a/examples/node.nim +++ b/examples/node.nim @@ -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: diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index a70da119f..fce3dd8f8 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -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: