|
| 1 | +package nl.tudelft.ipv8.jvm.demo.examples.force_walk |
| 2 | + |
| 3 | +import kotlinx.coroutines.* |
| 4 | +import mu.KotlinLogging |
| 5 | +import nl.tudelft.ipv8.* |
| 6 | +import nl.tudelft.ipv8.keyvault.JavaCryptoProvider |
| 7 | +import nl.tudelft.ipv8.messaging.EndpointAggregator |
| 8 | +import nl.tudelft.ipv8.messaging.udp.UdpEndpoint |
| 9 | +import java.net.InetAddress |
| 10 | +import java.util.* |
| 11 | +import kotlin.math.roundToInt |
| 12 | + |
| 13 | +class Application { |
| 14 | + |
| 15 | + private val scope = CoroutineScope(Dispatchers.Default) |
| 16 | + private val logger = KotlinLogging.logger {} |
| 17 | + lateinit var ipv8: IPv8 |
| 18 | + |
| 19 | + fun run() { |
| 20 | + startIpv8() |
| 21 | + } |
| 22 | + |
| 23 | + private fun createConnectionCommunity(): OverlayConfiguration<ConnectionCommunity> { |
| 24 | + // Add no walkers on purpose. |
| 25 | + return OverlayConfiguration( |
| 26 | + Overlay.Factory(ConnectionCommunity::class.java), listOf() |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + private fun startIpv8() { |
| 31 | + val myKey = JavaCryptoProvider.generateKey() |
| 32 | + val myPeer = Peer(myKey) |
| 33 | + val udpEndpoint = UdpEndpoint(8090, InetAddress.getByName("0.0.0.0")) |
| 34 | + val endpoint = EndpointAggregator(udpEndpoint, null) |
| 35 | + |
| 36 | + val config = IPv8Configuration( |
| 37 | + overlays = listOf( |
| 38 | + createConnectionCommunity() |
| 39 | + ), walkerInterval = 1.0 |
| 40 | + ) |
| 41 | + |
| 42 | + this.ipv8 = IPv8(endpoint, config, myPeer) |
| 43 | + this.ipv8.start() |
| 44 | + |
| 45 | + scope.launch { |
| 46 | + while (true) { |
| 47 | + for ((_, overlay) in ipv8.overlays) { |
| 48 | + printPeersInfo(overlay) |
| 49 | + } |
| 50 | + logger.info("===") |
| 51 | + delay(5000) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + while (ipv8.isStarted()) { |
| 56 | + Thread.sleep(1000) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private fun printPeersInfo(overlay: Overlay) { |
| 61 | + val peers = overlay.getPeers() |
| 62 | + logger.info(overlay::class.simpleName + ": ${peers.size} peers") |
| 63 | + for (peer in peers) { |
| 64 | + val avgPing = peer.getAveragePing() |
| 65 | + val lastRequest = peer.lastRequest |
| 66 | + val lastResponse = peer.lastResponse |
| 67 | + |
| 68 | + val lastRequestStr = |
| 69 | + if (lastRequest != null) "" + ((Date().time - lastRequest.time) / 1000.0).roundToInt() + " s" else "?" |
| 70 | + |
| 71 | + val lastResponseStr = |
| 72 | + if (lastResponse != null) "" + ((Date().time - lastResponse.time) / 1000.0).roundToInt() + " s" else "?" |
| 73 | + |
| 74 | + val avgPingStr = if (!avgPing.isNaN()) "" + (avgPing * 1000).roundToInt() + " ms" else "? ms" |
| 75 | + logger.info("${peer.mid} (S: ${lastRequestStr}, R: ${lastResponseStr}, ${avgPingStr})") |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fun main(): Unit = runBlocking { |
| 81 | + // Start two peers |
| 82 | + val peer0 = Application() |
| 83 | + val peer1 = Application() |
| 84 | + |
| 85 | + val scope = CoroutineScope(Dispatchers.Default) |
| 86 | + |
| 87 | + scope.launch { peer0.run() } |
| 88 | + scope.launch { peer1.run() } |
| 89 | + |
| 90 | + // Wait for the peers to start |
| 91 | + delay(1000) |
| 92 | + |
| 93 | + // Walk to each other |
| 94 | + val peer0Address = peer0.ipv8.getOverlay<ConnectionCommunity>()!!.myEstimatedLan |
| 95 | + val peer1Address = peer1.ipv8.getOverlay<ConnectionCommunity>()!!.myEstimatedLan |
| 96 | + |
| 97 | + peer0.ipv8.getOverlay<ConnectionCommunity>()!!.walkTo(peer1Address) |
| 98 | + peer1.ipv8.getOverlay<ConnectionCommunity>()!!.walkTo(peer0Address) |
| 99 | + |
| 100 | + // Wait forever |
| 101 | + delay(Long.MAX_VALUE) |
| 102 | +} |
0 commit comments