Some fixes to chat, and one more test for bad cid provide.

This commit is contained in:
cheatfate 2018-11-22 17:35:44 +02:00
parent 10548f9b1b
commit 9edc823b6c
2 changed files with 29 additions and 2 deletions

View File

@ -25,6 +25,14 @@ proc serveThread(server: StreamServer,
## This procedure perform readin on local unix domain socket and ## This procedure perform readin on local unix domain socket and
## sends data to remote clients. ## sends data to remote clients.
var udata = getUserData[CustomData](server) var udata = getUserData[CustomData](server)
proc remoteReader(transp: StreamTransport) {.async.} =
while true:
var line = await transp.readLine()
if len(line) == 0:
break
echo ">> ", line
while true: while true:
try: try:
var line = await transp.readLine() var line = await transp.readLine()
@ -34,15 +42,19 @@ proc serveThread(server: StreamServer,
var address = fromHex(parts[1]) var address = fromHex(parts[1])
echo "= Searching for peer ", toHex(address) echo "= Searching for peer ", toHex(address)
var id = await udata.api.dhtFindPeer(address) var id = await udata.api.dhtFindPeer(address)
echo "==="
echo repr id
echo "==="
echo "= Connecting to peer ", toHex(address) echo "= Connecting to peer ", toHex(address)
await udata.api.connect(id.peer, id.addresses) await udata.api.connect(id.peer, id.addresses)
echo "= Opening stream to peer chat ", toHex(address) echo "= Opening stream to peer chat ", toHex(address)
var stream = await udata.api.openStream(id.peer, ServerProtocols) var stream = await udata.api.openStream(id.peer, ServerProtocols)
udata.remotes.add(transp) udata.remotes.add(stream.transp)
echo "= Connected to peer chat ", toHex(address) echo "= Connected to peer chat ", toHex(address)
asyncCheck remoteReader(stream.transp)
else: else:
var msg = line & "\r\n" var msg = line & "\r\n"
echo "<< ", msg echo "<< ", line
var pending = newSeq[Future[int]]() var pending = newSeq[Future[int]]()
for item in udata.remotes: for item in udata.remotes:
pending.add(item.write(msg)) pending.add(item.write(msg))
@ -68,8 +80,11 @@ proc main() {.async.} =
proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} = proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} =
echo "= Peer ", toHex(stream.peer), " joined chat" echo "= Peer ", toHex(stream.peer), " joined chat"
data.remotes.add(stream.transp)
while true: while true:
var line = await stream.transp.readLine() var line = await stream.transp.readLine()
if len(line) == 0:
break
echo ">> ", line echo ">> ", line
await data.api.addHandler(ServerProtocols, streamHandler) await data.api.addHandler(ServerProtocols, streamHandler)

View File

@ -35,6 +35,15 @@ proc connectStreamTest(): Future[bool] {.async.} =
await api2.close() await api2.close()
result = true result = true
proc provideBadCidTest(): Future[bool] {.async.} =
var cid = newSeq[byte](10)
var api = await newDaemonApi({DHTFull})
try:
await api.dhtProvide(cid)
result = false
except DaemonRemoteError:
result = true
when isMainModule: when isMainModule:
suite "libp2p-daemon test suite": suite "libp2p-daemon test suite":
test "Simple spawn and get identity test": test "Simple spawn and get identity test":
@ -43,3 +52,6 @@ when isMainModule:
test "Connect/Accept peer/stream test": test "Connect/Accept peer/stream test":
check: check:
waitFor(connectStreamTest()) == true waitFor(connectStreamTest()) == true
test "DHT provide bad CID test":
check:
waitFor(provideTestBadCid()) == true