Skip to content

Commit

Permalink
Merge pull request #1 from Eragoneq/ipv8_integration
Browse files Browse the repository at this point in the history
Ipv8 integration
  • Loading branch information
Eragoneq authored Apr 8, 2024
2 parents 22c2199 + a6adba0 commit 854892c
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import nl.tudelft.ipv8.attestation.wallet.cryptography.bonehexact.BonehPrivateKe
import nl.tudelft.ipv8.keyvault.PrivateKey
import nl.tudelft.ipv8.keyvault.defaultCryptoProvider
import nl.tudelft.ipv8.messaging.EndpointAggregator
import nl.tudelft.ipv8.messaging.utp.UtpEndpoint
import nl.tudelft.ipv8.peerdiscovery.Network
import nl.tudelft.ipv8.util.defaultEncodingUtils
import java.net.InetAddress
Expand Down Expand Up @@ -110,10 +109,6 @@ object IPv8Android {
connectivityManager,
)

val utpEndpoint = UtpEndpoint(
13377,
InetAddress.getByName("0.0.0.0")
)

val bluetoothManager =
application.getSystemService<BluetoothManager>()
Expand Down Expand Up @@ -151,7 +146,6 @@ object IPv8Android {
EndpointAggregator(
udpEndpoint,
bluetoothEndpoint,
utpEndpoint,
)

return IPv8(endpointAggregator, configuration, myPeer, network)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import nl.tudelft.ipv8.Peer
import nl.tudelft.ipv8.messaging.bluetooth.BluetoothAddress
import nl.tudelft.ipv8.messaging.bluetooth.BluetoothEndpoint
import nl.tudelft.ipv8.messaging.udp.UdpEndpoint
import nl.tudelft.ipv8.messaging.utp.UtpEndpoint
import java.util.*

/**
Expand All @@ -14,7 +13,6 @@ import java.util.*
class EndpointAggregator(
val udpEndpoint: UdpEndpoint?,
val bluetoothEndpoint: BluetoothEndpoint?,
val utpEndpoint: UtpEndpoint? = null,
) : Endpoint<Peer>(), EndpointListener {
private var isOpen: Boolean = false

Expand Down Expand Up @@ -60,9 +58,6 @@ class EndpointAggregator(
bluetoothEndpoint?.addListener(this)
bluetoothEndpoint?.open()

utpEndpoint?.addListener(this)
utpEndpoint?.open()

isOpen = true
}

Expand All @@ -73,9 +68,6 @@ class EndpointAggregator(
bluetoothEndpoint?.removeListener(this)
bluetoothEndpoint?.close()

utpEndpoint?.removeListener(this)
utpEndpoint?.close()

isOpen = false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package nl.tudelft.ipv8.messaging.payload

import nl.tudelft.ipv8.messaging.Deserializable
import nl.tudelft.ipv8.messaging.SERIALIZED_LONG_SIZE
import nl.tudelft.ipv8.messaging.SERIALIZED_UINT_SIZE
import nl.tudelft.ipv8.messaging.Serializable
import nl.tudelft.ipv8.messaging.deserializeLong
import nl.tudelft.ipv8.messaging.deserializeUInt
import nl.tudelft.ipv8.messaging.serializeLong
import nl.tudelft.ipv8.messaging.serializeUChar
import nl.tudelft.ipv8.messaging.serializeUInt

data class TransferRequestPayload(
val port: Int,
val status: TransferStatus,
val dataSize: Int = 0
): Serializable {
override fun serialize(): ByteArray {
return serializeLong(port.toLong()) + serializeUInt(status.ordinal.toUInt()) + serializeLong(dataSize.toLong())
}

companion object Deserializer : Deserializable<TransferRequestPayload> {
override fun deserialize(
buffer: ByteArray,
offset: Int
): Pair<TransferRequestPayload, Int> {
var localOffset = offset

val port = deserializeLong(buffer, localOffset)
localOffset += SERIALIZED_LONG_SIZE
val status = deserializeUInt(buffer, localOffset)
localOffset += SERIALIZED_UINT_SIZE
val dataSize = deserializeLong(buffer, localOffset)
localOffset += SERIALIZED_LONG_SIZE

return Pair(TransferRequestPayload(port.toInt(),
TransferStatus.entries[status.toInt()], dataSize.toInt()), localOffset)
}
}

enum class TransferStatus {
REQUEST,
ACCEPT,
DECLINE
}

enum class TransferType {
FILE,
RANDOM_DATA
}
}
18 changes: 17 additions & 1 deletion ipv8/src/main/java/nl/tudelft/ipv8/messaging/udp/UdpEndpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import nl.tudelft.ipv8.messaging.Endpoint
import nl.tudelft.ipv8.messaging.EndpointListener
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.ipv8.messaging.tftp.TFTPEndpoint
import nl.tudelft.ipv8.messaging.utp.UtpIPv8Endpoint
import java.io.IOException
import java.net.*

Expand All @@ -18,6 +19,7 @@ open class UdpEndpoint(
private val port: Int,
private val ip: InetAddress,
private val tftpEndpoint: TFTPEndpoint = TFTPEndpoint(),
val utpIPv8Endpoint: UtpIPv8Endpoint = UtpIPv8Endpoint(),
) : Endpoint<Peer>() {
private var socket: DatagramSocket? = null

Expand Down Expand Up @@ -88,13 +90,23 @@ open class UdpEndpoint(
}
}

fun sendUtp(
address: IPv4Address,
data: ByteArray,
) {
utpIPv8Endpoint.send(address, data)
}

override fun open() {
val socket = getDatagramSocket()
this.socket = socket

tftpEndpoint.socket = socket
tftpEndpoint.open()

utpIPv8Endpoint.udpSocket = socket
utpIPv8Endpoint.open()

logger.info { "Opened UDP socket on port ${socket.localPort}" }

startLanEstimation()
Expand Down Expand Up @@ -126,6 +138,7 @@ open class UdpEndpoint(
bindJob = null

tftpEndpoint.close()
utpIPv8Endpoint.close()

socket?.close()
socket = null
Expand Down Expand Up @@ -185,7 +198,7 @@ open class UdpEndpoint(
"${receivePacket.address.hostAddress}:${receivePacket.port}",
)

// Check whether prefix is IPv8 or TFTP
// Check whether prefix is IPv8 or TFTP or UTP
when (receivePacket.data[0]) {
Community.PREFIX_IPV8 -> {
val sourceAddress =
Expand All @@ -201,6 +214,9 @@ open class UdpEndpoint(
TFTPEndpoint.PREFIX_TFTP -> {
tftpEndpoint.onPacket(receivePacket)
}
UtpIPv8Endpoint.PREFIX_UTP -> {
utpIPv8Endpoint.onPacket(receivePacket)
}
else -> {
logger.warn { "Invalid packet prefix" }
}
Expand Down
11 changes: 11 additions & 0 deletions ipv8/src/main/java/nl/tudelft/ipv8/messaging/utp/UtpCommunity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nl.tudelft.ipv8.messaging.utp

import nl.tudelft.ipv8.Community

class UtpCommunity : Community() {
override val serviceId = "450ded7389134595dadb6b2549f431ad60156931"




}
122 changes: 0 additions & 122 deletions ipv8/src/main/java/nl/tudelft/ipv8/messaging/utp/UtpEndpoint.kt

This file was deleted.

Loading

0 comments on commit 854892c

Please sign in to comment.