-
Notifications
You must be signed in to change notification settings - Fork 27
/
Application.kt
121 lines (105 loc) · 4.29 KB
/
Application.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package nl.tudelft.ipv8.jvm.demo
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver.Companion.IN_MEMORY
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import mu.KotlinLogging
import nl.tudelft.ipv8.*
import nl.tudelft.ipv8.attestation.trustchain.TrustChainCommunity
import nl.tudelft.ipv8.attestation.trustchain.TrustChainSettings
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainSQLiteStore
import nl.tudelft.ipv8.keyvault.JavaCryptoProvider
import nl.tudelft.ipv8.messaging.EndpointAggregator
import nl.tudelft.ipv8.messaging.udp.UdpEndpoint
import nl.tudelft.ipv8.peerdiscovery.DiscoveryCommunity
import nl.tudelft.ipv8.peerdiscovery.strategy.PeriodicSimilarity
import nl.tudelft.ipv8.peerdiscovery.strategy.RandomChurn
import nl.tudelft.ipv8.peerdiscovery.strategy.RandomWalk
import nl.tudelft.ipv8.sqldelight.Database
import java.net.InetAddress
import java.util.*
import kotlin.math.roundToInt
class Application {
private val scope = CoroutineScope(Dispatchers.Default)
private val logger = KotlinLogging.logger {}
fun run() {
startIpv8()
}
private fun createDiscoveryCommunity(): OverlayConfiguration<DiscoveryCommunity> {
val randomWalk = RandomWalk.Factory(timeout = 3.0, peers = 20)
val randomChurn = RandomChurn.Factory()
val periodicSimilarity = PeriodicSimilarity.Factory()
return OverlayConfiguration(
DiscoveryCommunity.Factory(),
listOf(randomWalk, randomChurn, periodicSimilarity)
)
}
private fun createTrustChainCommunity(): OverlayConfiguration<TrustChainCommunity> {
val settings = TrustChainSettings()
val driver: SqlDriver = JdbcSqliteDriver(IN_MEMORY)
Database.Schema.create(driver)
val database = Database(driver)
val store = TrustChainSQLiteStore(database)
val randomWalk = RandomWalk.Factory(timeout = 3.0, peers = 20)
return OverlayConfiguration(
TrustChainCommunity.Factory(settings, store),
listOf(randomWalk)
)
}
private fun createDemoCommunity(): OverlayConfiguration<DemoCommunity> {
val randomWalk = RandomWalk.Factory(timeout = 3.0, peers = 20)
return OverlayConfiguration(
Overlay.Factory(DemoCommunity::class.java),
listOf(randomWalk)
)
}
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(
createDiscoveryCommunity(),
createTrustChainCommunity(),
createDemoCommunity()
), walkerInterval = 1.0
)
val ipv8 = IPv8(endpoint, config, myPeer)
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() {
val app = Application()
app.run()
}