mirror of https://github.com/vacp2p/nim-libp2p.git
Fix daemonapi connect/streamOpen to support timeouts.
Add more commands to chat example.
This commit is contained in:
parent
dba3e9ed7c
commit
b4e9a7b8a6
|
@ -54,6 +54,24 @@ proc serveThread(server: StreamServer,
|
||||||
udata.remotes.add(stream.transp)
|
udata.remotes.add(stream.transp)
|
||||||
echo "= Connected to peer chat ", parts[1]
|
echo "= Connected to peer chat ", parts[1]
|
||||||
asyncCheck remoteReader(stream.transp)
|
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"):
|
elif line.startsWith("/exit"):
|
||||||
quit(0)
|
quit(0)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -518,11 +518,13 @@ proc identity*(api: DaemonAPI): Future[PeerInfo] {.async.} =
|
||||||
api.pool.release(transp)
|
api.pool.release(transp)
|
||||||
|
|
||||||
proc connect*(api: DaemonAPI, peer: PeerID,
|
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``.
|
## Connect to remote peer with id ``peer`` and addresses ``addresses``.
|
||||||
var transp = await api.pool.acquire()
|
var transp = await api.pool.acquire()
|
||||||
try:
|
try:
|
||||||
var pb = await transp.transactMessage(requestConnect(peer, addresses))
|
var pb = await transp.transactMessage(requestConnect(peer, addresses,
|
||||||
|
timeout))
|
||||||
pb.withMessage() do:
|
pb.withMessage() do:
|
||||||
discard
|
discard
|
||||||
finally:
|
finally:
|
||||||
|
@ -539,13 +541,15 @@ proc disconnect*(api: DaemonAPI, peer: PeerID) {.async.} =
|
||||||
api.pool.release(transp)
|
api.pool.release(transp)
|
||||||
|
|
||||||
proc openStream*(api: DaemonAPI, peer: PeerID,
|
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
|
## Open new stream to peer ``peer`` using one of the protocols in
|
||||||
## ``protocols``. Returns ``StreamTransport`` for the stream.
|
## ``protocols``. Returns ``StreamTransport`` for the stream.
|
||||||
var transp = await connect(api.address)
|
var transp = await connect(api.address)
|
||||||
var stream = new P2PStream
|
var stream = new P2PStream
|
||||||
try:
|
try:
|
||||||
var pb = await transp.transactMessage(requestStreamOpen(peer, protocols))
|
var pb = await transp.transactMessage(requestStreamOpen(peer, protocols,
|
||||||
|
timeout))
|
||||||
pb.withMessage() do:
|
pb.withMessage() do:
|
||||||
var res = pb.enterSubmessage()
|
var res = pb.enterSubmessage()
|
||||||
if res == cast[int](ResponseType.STREAMINFO):
|
if res == cast[int](ResponseType.STREAMINFO):
|
||||||
|
|
Loading…
Reference in New Issue