Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples/force walk #90

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nl.tudelft.ipv8.jvm.demo.examples.force_walk

import nl.tudelft.ipv8.Community
import nl.tudelft.ipv8.IPv4Address

class ConnectionCommunity : Community() {
override val serviceId = "02313685c1912a141279f8248fc8db5899c5df5b"

override fun walkTo(address: IPv4Address) {
val packet = createIntroductionRequest(address)
this.endpoint.send(address, packet)
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package nl.tudelft.ipv8.jvm.demo.examples.force_walk

import kotlinx.coroutines.*
import mu.KotlinLogging
import nl.tudelft.ipv8.*
import nl.tudelft.ipv8.keyvault.JavaCryptoProvider
import nl.tudelft.ipv8.messaging.EndpointAggregator
import nl.tudelft.ipv8.messaging.udp.UdpEndpoint
import java.net.InetAddress
import java.util.*
import kotlin.math.roundToInt

class Application {

private val scope = CoroutineScope(Dispatchers.Default)
private val logger = KotlinLogging.logger {}
lateinit var ipv8: IPv8

fun run() {
startIpv8()
}

private fun createConnectionCommunity(): OverlayConfiguration<ConnectionCommunity> {
// Add no walkers on purpose.
return OverlayConfiguration(
Overlay.Factory(ConnectionCommunity::class.java), listOf()
)
}

private fun startIpv8() {
val myKey = JavaCryptoProvider.generateKey()
val myPeer = Peer(myKey)
val udpEndpoint = UdpEndpoint(8090, InetAddress.getByName("0.0.0.0"))
val endpoint = EndpointAggregator(udpEndpoint, null)

val config = IPv8Configuration(
overlays = listOf(
createConnectionCommunity()
), walkerInterval = 1.0
)

this.ipv8 = IPv8(endpoint, config, myPeer)
this.ipv8.start()

scope.launch {
while (true) {
for ((_, overlay) in ipv8.overlays) {
printPeersInfo(overlay)
}
logger.info("===")
delay(5000)
}
}

while (ipv8.isStarted()) {
Thread.sleep(1000)
}
}

private fun printPeersInfo(overlay: Overlay) {
val peers = overlay.getPeers()
logger.info(overlay::class.simpleName + ": ${peers.size} peers")
for (peer in peers) {
val avgPing = peer.getAveragePing()
val lastRequest = peer.lastRequest
val lastResponse = peer.lastResponse

val lastRequestStr =
if (lastRequest != null) "" + ((Date().time - lastRequest.time) / 1000.0).roundToInt() + " s" else "?"

val lastResponseStr =
if (lastResponse != null) "" + ((Date().time - lastResponse.time) / 1000.0).roundToInt() + " s" else "?"

val avgPingStr = if (!avgPing.isNaN()) "" + (avgPing * 1000).roundToInt() + " ms" else "? ms"
logger.info("${peer.mid} (S: ${lastRequestStr}, R: ${lastResponseStr}, ${avgPingStr})")
}
}
}

fun main(): Unit = runBlocking {
// Start two peers
val peer0 = Application()
val peer1 = Application()

val scope = CoroutineScope(Dispatchers.Default)

scope.launch { peer0.run() }
scope.launch { peer1.run() }

// Wait for the peers to start
delay(1000)

// Walk to each other
val peer0Address = peer0.ipv8.getOverlay<ConnectionCommunity>()!!.myEstimatedLan
val peer1Address = peer1.ipv8.getOverlay<ConnectionCommunity>()!!.myEstimatedLan

peer0.ipv8.getOverlay<ConnectionCommunity>()!!.walkTo(peer1Address)
peer1.ipv8.getOverlay<ConnectionCommunity>()!!.walkTo(peer0Address)

// Wait forever
delay(Long.MAX_VALUE)
}
7 changes: 5 additions & 2 deletions ipv8/src/main/java/nl/tudelft/ipv8/Community.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package nl.tudelft.ipv8

import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import mu.KotlinLogging
import nl.tudelft.ipv8.exception.PacketDecodingException
import nl.tudelft.ipv8.keyvault.PrivateKey
Expand Down Expand Up @@ -182,7 +185,7 @@ abstract class Community : Overlay {
/**
* Introduction and puncturing requests creation
*/
internal fun createIntroductionRequest(
fun createIntroductionRequest(
socketAddress: IPv4Address,
extraBytes: ByteArray = byteArrayOf()
): ByteArray {
Expand Down
Loading