Fix daemonapi and examples.

This commit is contained in:
cheatfate 2019-03-05 01:57:18 +02:00
parent 77f34af737
commit d7d9d7bd40
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
3 changed files with 30 additions and 6 deletions

View File

@ -1,6 +1,5 @@
import chronos, nimcrypto, strutils import chronos, nimcrypto, strutils
import ../libp2p/daemon/daemonapi import ../libp2p/daemon/daemonapi
import ../libp2p/[base58, multicodec, multiaddress, peer]
import hexdump import hexdump
const const
@ -12,6 +11,18 @@ proc dumpSubscribedPeers(api: DaemonAPI) {.async.} =
for item in peers: for item in peers:
echo item.pretty() 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.} = proc main() {.async.} =
echo "= Starting P2P bootnode" echo "= Starting P2P bootnode"
var api = await newDaemonApi({DHTFull, PSGossipSub}) var api = await newDaemonApi({DHTFull, PSGossipSub})
@ -24,6 +35,8 @@ proc main() {.async.} =
if item.protoCode() == mcip4 or item.protoCode() == mcip6: if item.protoCode() == mcip4 or item.protoCode() == mcip6:
echo $item & "/ipfs/" & id.peer.pretty() echo $item & "/ipfs/" & id.peer.pretty()
asyncCheck monitor(api)
proc pubsubLogger(api: DaemonAPI, proc pubsubLogger(api: DaemonAPI,
ticket: PubsubTicket, ticket: PubsubTicket,
message: PubSubMessage): Future[bool] {.async.} = message: PubSubMessage): Future[bool] {.async.} =

View File

@ -1,5 +1,8 @@
import chronos, nimcrypto, strutils, os 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.} = proc main(bn: string) {.async.} =
echo "= Starting P2P node" echo "= Starting P2P node"
@ -22,11 +25,18 @@ proc main(bn: string) {.async.} =
echo strdata echo strdata
result = true 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 data = "HELLO\r\n"
var msgData = cast[seq[byte]](data) var msgData = cast[seq[byte]](data)
await api.pubsubPublish("test-net", msgData) await api.pubsubPublish(PubSubTopic, msgData)
when isMainModule: when isMainModule:
if paramCount() != 1: if paramCount() != 1:

View File

@ -13,6 +13,8 @@ import chronos
import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer
import ../wire, ../protobuf/minprotobuf import ../wire, ../protobuf/minprotobuf
export peer, multiaddress, multicodec, multihash, cid
when not defined(windows): when not defined(windows):
import posix import posix
@ -147,7 +149,6 @@ type
DaemonRemoteError* = object of Exception DaemonRemoteError* = object of Exception
DaemonLocalError* = object of Exception DaemonLocalError* = object of Exception
var daemonsCount {.threadvar.}: int var daemonsCount {.threadvar.}: int
proc requestIdentity(): ProtoBuffer = proc requestIdentity(): ProtoBuffer =
@ -661,7 +662,7 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
raise newException(DaemonLocalError, "Could not find daemon executable!") raise newException(DaemonLocalError, "Could not find daemon executable!")
# Starting daemon process # Starting daemon process
# echo "Starting ", cmd, " ", args.join(" ") echo "Starting ", cmd, " ", args.join(" ")
api.process = startProcess(cmd, "", args, env, {poStdErrToStdOut}) api.process = startProcess(cmd, "", args, env, {poStdErrToStdOut})
# Waiting until daemon will not be bound to control socket. # Waiting until daemon will not be bound to control socket.
while true: while true: