Skip to content

Commit 3ebf263

Browse files
authored
Merge pull request #90 from Tribler/examples/force_walk
Examples/force walk
2 parents 476dc36 + d790e0d commit 3ebf263

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package nl.tudelft.ipv8.jvm.demo.examples.force_walk
2+
3+
import nl.tudelft.ipv8.Community
4+
import nl.tudelft.ipv8.IPv4Address
5+
6+
class ConnectionCommunity : Community() {
7+
override val serviceId = "02313685c1912a141279f8248fc8db5899c5df5b"
8+
9+
override fun walkTo(address: IPv4Address) {
10+
val packet = createIntroductionRequest(address)
11+
this.endpoint.send(address, packet)
12+
}
13+
14+
}
15+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

ipv8/src/main/java/nl/tudelft/ipv8/Community.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package nl.tudelft.ipv8
22

3-
import kotlinx.coroutines.*
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.Job
6+
import kotlinx.coroutines.SupervisorJob
47
import mu.KotlinLogging
58
import nl.tudelft.ipv8.exception.PacketDecodingException
69
import nl.tudelft.ipv8.keyvault.PrivateKey
@@ -182,7 +185,7 @@ abstract class Community : Overlay {
182185
/**
183186
* Introduction and puncturing requests creation
184187
*/
185-
internal fun createIntroductionRequest(
188+
fun createIntroductionRequest(
186189
socketAddress: IPv4Address,
187190
extraBytes: ByteArray = byteArrayOf()
188191
): ByteArray {

0 commit comments

Comments
 (0)