Skip to content

Commit

Permalink
Workaround for bug libp2p/go-libp2p-pubsub#130.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Dec 13, 2018
1 parent 39deeca commit 0b807e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
60 changes: 32 additions & 28 deletions libp2p/daemon/daemonapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -482,34 +482,38 @@ proc closeConnection*(api: DaemonAPI, transp: StreamTransport) {.async.} =
transp.close()
await transp.join()

proc socketExists(filename: string): bool =
var res: Stat
result = stat(filename, res) >= 0'i32

proc loggingHandler(api: DaemonAPI): Future[void] =
var retFuture = newFuture[void]("logging.handler")
var loop = getGlobalDispatcher()
let pfd = SocketHandle(api.process.outputHandle)
var fd = AsyncFD(pfd)
if not setSocketBlocking(pfd, false):
discard close(cint(pfd))
raiseOsError(osLastError())
register(AsyncFD(pfd))

proc readOutputLoop(udata: pointer) {.gcsafe.} =
var buffer: array[2048, char]
let res = posix.read(cint(fd), addr buffer[0], 2000)
if res == -1 or res == 0:
removeReader(fd)
retFuture.complete()
else:
var cstr = cast[cstring](addr buffer[0])
api.log.add(cstr)
# let offset = len(api.log)
# api.log.setLen(offset + res)
# copyMem(addr api.log[offset], addr buffer[0], res)
addReader(fd, readOutputLoop, nil)
result = retFuture
when not defined(windows):
proc socketExists(filename: string): bool =
var res: Stat
result = stat(filename, res) >= 0'i32

proc loggingHandler(api: DaemonAPI): Future[void] =
var retFuture = newFuture[void]("logging.handler")
var loop = getGlobalDispatcher()
let pfd = SocketHandle(api.process.outputHandle)
var fd = AsyncFD(pfd)
if not setSocketBlocking(pfd, false):
discard close(cint(pfd))
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))

proc readOutputLoop(udata: pointer) {.gcsafe.} =
var buffer: array[2048, char]
let res = posix.read(cint(fd), addr buffer[0], 2000)
if res == -1 or res == 0:
removeReader(fd)
retFuture.complete()
else:
var cstr = cast[cstring](addr buffer[0])
api.log.add(cstr)
register(AsyncFD(pfd))
addReader(fd, readOutputLoop, nil)
result = retFuture
else:
proc socketExists(filename: string): bool = false

proc loggingHandler(api: DaemonAPI): Future[void] =
# Not ready yet.
discard

proc newDaemonApi*(flags: set[P2PDaemonFlags] = {},
bootstrapNodes: seq[string] = @[],
Expand Down
33 changes: 16 additions & 17 deletions tests/testdaemon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ proc provideBadCidTest(): Future[bool] {.async.} =
finally:
await api.close()

# proc getOnlyIPv4Addresses(addresses: seq[MultiAddress]): seq[MultiAddress] =
# if len(addresses) > 0:
# result = newSeqOfCap[MultiAddress](len(addresses))
# let ip4 = multiCodec("ip4")
# for item in addresses:
# if item.protoCode() == ip4:
# result.add(item)
proc getOnlyOneIPv4Address(addresses: seq[MultiAddress]): seq[MultiAddress] =
## We doing this becuase of bug in `go-pubsub`
## https://github.com/libp2p/go-libp2p-pubsub/issues/130
if len(addresses) > 0:
result = newSeqOfCap[MultiAddress](len(addresses))
let ip4 = multiCodec("ip4")
for item in addresses:
if item.protoCode() == ip4:
result.add(item)
break

proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
var pubsubData = "TEST MESSAGE"
Expand All @@ -69,11 +72,6 @@ proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =

var resultsCount = 0

# var topics10 = await api1.pubsubGetTopics()
# var peers10 = await api1.pubsubListPeers("test-topic")
# var topics20 = await api2.pubsubGetTopics()
# var peers20 = await api2.pubsubListPeers("test-topic")

var handlerFuture1 = newFuture[void]()
var handlerFuture2 = newFuture[void]()

Expand All @@ -98,8 +96,9 @@ proc pubsubTest(f: set[P2PDaemonFlags]): Future[bool] {.async.} =
result = false

# Not subscribed to any topics everything must be 0.
await api1.connect(id2.peer, id2.addresses)
await api2.connect(id1.peer, id1.addresses)
# We are making only one connection, because of bug
# https://github.com/libp2p/go-libp2p-pubsub/issues/130
await api1.connect(id2.peer, getOnlyOneIPv4Address(id2.addresses))

var ticket1 = await api1.pubsubSubscribe("test-topic", pubsubHandler1)
var ticket2 = await api2.pubsubSubscribe("test-topic", pubsubHandler2)
Expand Down Expand Up @@ -143,6 +142,6 @@ when isMainModule:
test "GossipSub test":
check:
waitFor(pubsubTest({PSGossipSub})) == true
# test "FloodSub test":
# check:
# waitFor(pubsubTest({PSFloodSub})) == true
test "FloodSub test":
check:
waitFor(pubsubTest({PSFloodSub})) == true

0 comments on commit 0b807e7

Please sign in to comment.