2019-03-04 18:22:38 +00:00
|
|
|
import chronos, nimcrypto, strutils, os
|
2020-03-16 07:43:08 +00:00
|
|
|
import ../../libp2p/daemon/daemonapi
|
2019-03-04 23:57:18 +00:00
|
|
|
|
|
|
|
const PubSubTopic = "test-net"
|
2018-12-18 14:31:58 +00:00
|
|
|
|
|
|
|
proc main(bn: string) {.async.} =
|
|
|
|
echo "= Starting P2P node"
|
|
|
|
var bootnodes = bn.split(",")
|
|
|
|
var api = await newDaemonApi(
|
|
|
|
{DHTFull, PSGossipSub, WaitBootstrap}, bootstrapNodes = bootnodes, peersRequired = 1
|
|
|
|
)
|
|
|
|
var id = await api.identity()
|
2019-03-04 18:28:59 +00:00
|
|
|
echo "= P2P node ", id.peer.pretty(), " started:"
|
2018-12-18 14:31:58 +00:00
|
|
|
for item in id.addresses:
|
|
|
|
echo item
|
|
|
|
|
|
|
|
proc pubsubLogger(
|
|
|
|
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
|
|
|
|
): Future[bool] {.async.} =
|
|
|
|
let msglen = len(message.data)
|
2019-03-04 18:51:10 +00:00
|
|
|
echo "= Recieved pubsub message with length ",
|
|
|
|
msglen, " bytes from peer ", message.peer.pretty(), ": "
|
|
|
|
var strdata = cast[string](message.data)
|
|
|
|
echo strdata
|
2018-12-18 14:31:58 +00:00
|
|
|
result = true
|
|
|
|
|
2019-03-04 23:57:18 +00:00
|
|
|
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)
|
2018-12-18 14:31:58 +00:00
|
|
|
|
|
|
|
var data = "HELLO\r\n"
|
|
|
|
var msgData = cast[seq[byte]](data)
|
2019-03-04 23:57:18 +00:00
|
|
|
await api.pubsubPublish(PubSubTopic, msgData)
|
2018-12-18 14:31:58 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
if paramCount() != 1:
|
|
|
|
echo "Please supply bootnodes!"
|
|
|
|
else:
|
|
|
|
waitFor(main(paramStr(1)))
|
|
|
|
while true:
|
|
|
|
poll()
|