diff --git a/examples/chat.nim b/examples/chat.nim index 3ce9493ae..07432d4d2 100644 --- a/examples/chat.nim +++ b/examples/chat.nim @@ -54,6 +54,24 @@ proc serveThread(server: StreamServer, udata.remotes.add(stream.transp) echo "= Connected to peer chat ", parts[1] asyncCheck remoteReader(stream.transp) + elif line.startsWith("/search"): + var parts = line.split(" ") + if len(parts) == 2: + var peerId = Base58.decode(parts[1]) + echo "= Searching for peer ", parts[1] + var id = await udata.api.dhtFindPeer(peerId) + echo "Peer " & parts[1] & " found at addresses:" + for item in id.addresses: + echo $item + elif line.startsWith("/consearch"): + var parts = line.split(" ") + if len(parts) == 2: + var peerId = Base58.decode(parts[1]) + echo "= Searching for peers connected to peer ", parts[1] + var peers = await udata.api.dhtFindPeersConnectedToPeer(peerId) + echo "Found ", len(peers), " connected to peer ", parts[1] + for item in peers: + echo Base58.encode(item.peer) elif line.startsWith("/exit"): quit(0) else: diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index cdf0e62b6..cc9877fc1 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -518,11 +518,13 @@ proc identity*(api: DaemonAPI): Future[PeerInfo] {.async.} = api.pool.release(transp) proc connect*(api: DaemonAPI, peer: PeerID, - addresses: seq[MultiAddress]) {.async.} = + addresses: seq[MultiAddress], + timeout = 0) {.async.} = ## Connect to remote peer with id ``peer`` and addresses ``addresses``. var transp = await api.pool.acquire() try: - var pb = await transp.transactMessage(requestConnect(peer, addresses)) + var pb = await transp.transactMessage(requestConnect(peer, addresses, + timeout)) pb.withMessage() do: discard finally: @@ -539,13 +541,15 @@ proc disconnect*(api: DaemonAPI, peer: PeerID) {.async.} = api.pool.release(transp) proc openStream*(api: DaemonAPI, peer: PeerID, - protocols: seq[string]): Future[P2PStream] {.async.} = + protocols: seq[string], + timeout = 0): Future[P2PStream] {.async.} = ## Open new stream to peer ``peer`` using one of the protocols in ## ``protocols``. Returns ``StreamTransport`` for the stream. var transp = await connect(api.address) var stream = new P2PStream try: - var pb = await transp.transactMessage(requestStreamOpen(peer, protocols)) + var pb = await transp.transactMessage(requestStreamOpen(peer, protocols, + timeout)) pb.withMessage() do: var res = pb.enterSubmessage() if res == cast[int](ResponseType.STREAMINFO):