Skip to content

Commit

Permalink
Fix ipv6 only mdns and fix adding mdns handler from java
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed May 22, 2024
1 parent 640cc5d commit a0710d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libp2p/src/main/kotlin/io/libp2p/discovery/MDnsDiscovery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class MDnsDiscovery(
newPeerFoundListeners.forEach { it(peerInfo) }
}

fun addHandler(h: PeerListener) {
newPeerFoundListeners += h
}

private fun ipfsDiscoveryInfo(): ServiceInfo {
return ServiceInfo.create(
serviceTag,
Expand Down Expand Up @@ -112,7 +116,7 @@ class MDnsDiscovery(
val aRecords = answers.filter { DNSRecordType.TYPE_A.equals(it.recordType) }
val aaaaRecords = answers.filter { DNSRecordType.TYPE_AAAA.equals(it.recordType) }

if (txtRecord == null || srvRecord == null || aRecords.isEmpty()) {
if (txtRecord == null || srvRecord == null || (aRecords.isEmpty() && aaaaRecords.isEmpty())) {
return // incomplete answers
}

Expand Down
22 changes: 22 additions & 0 deletions libp2p/src/test/kotlin/io/libp2p/discovery/MDnsDiscoveryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ class MDnsDiscoveryTest {
assertEquals(host.listenAddresses().size, peerInfo?.addresses?.size)
}

@Test
fun `start discovery and listen for self ipv6`() {
var peerInfo: PeerInfo? = null
val discoverer = MDnsDiscovery(hostIpv6, testServiceTag)

discoverer.newPeerFoundListeners += {
peerInfo = it
}

discoverer.start().get(1, TimeUnit.SECONDS)
for (i in 0..50) {
if (peerInfo != null) {
break
}
TimeUnit.MILLISECONDS.sleep(100)
}
discoverer.stop().get(5, TimeUnit.SECONDS)

assertEquals(hostIpv6.peerId, peerInfo?.peerId)
assertEquals(hostIpv6.listenAddresses().size, peerInfo?.addresses?.size)
}

@Test
fun `start discovery and listen for other`() {
var peerInfo: PeerInfo? = null
Expand Down

0 comments on commit a0710d5

Please sign in to comment.