Fix daemonapi connect/streamOpen to support timeouts.

Add more commands to chat example.
This commit is contained in:
cheatfate 2018-11-27 14:33:41 +02:00
parent dba3e9ed7c
commit b4e9a7b8a6
2 changed files with 26 additions and 4 deletions

View File

@ -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:

View File

@ -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):