Skip to content

Commit

Permalink
Use Konnection library for device network info (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanLongstaff authored Nov 28, 2024
1 parent 452e033 commit 94f66db
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.walkertribe.ian.protocol.udp
import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.declaration.KoEnumConstantDeclaration
import com.lemonappdev.konsist.api.ext.list.enumConstants
import com.lemonappdev.konsist.api.ext.list.withName
import com.lemonappdev.konsist.api.ext.list.withRepresentedTypeOf
import com.lemonappdev.konsist.api.verify.assertTrue
import io.kotest.core.spec.style.DescribeSpec
Expand All @@ -30,12 +29,23 @@ private enum class NamingConventionTest(val testName: String) {
type.assertTrue { it.hasNameEndingWith("BIT_BLOCK") }
}
},
BROADCAST_ADDRESS("Broadcast address property follows naming patterns") {
override fun test(type: KoEnumConstantDeclaration) {
val prefix = type.name.substringBeforeLast("BLOCK")
type.assertTrue {
it.hasVariable { prop ->
prop.name == "broadcastAddress" && prop.text.contains("${prefix}BROADCAST")
}
}
}
},
CONSTRAINTS("Constraints property follows naming patterns") {
override fun test(type: KoEnumConstantDeclaration) {
val prefix = type.name.substringBeforeLast("BLOCK")
val constraints = type.variables.withName("constraints")
constraints.first().assertTrue { prop ->
prop.text.contains("${prefix}CONSTRAINTS")
type.assertTrue {
it.hasVariable { prop ->
prop.name == "constraints" && prop.text.contains("${prefix}CONSTRAINTS")
}
}
}
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ package com.walkertribe.ian.protocol.udp
enum class PrivateNetworkType {
TWENTY_FOUR_BIT_BLOCK {
// 10.x.x.x
override val broadcastAddress: String get() = TWENTY_FOUR_BIT_BROADCAST
override val constraints: Array<ByteConstraint> get() = TWENTY_FOUR_BIT_CONSTRAINTS
},
TWENTY_BIT_BLOCK {
// 172.16.x.x - 172.31.x.x
override val broadcastAddress: String get() = TWENTY_BIT_BROADCAST
override val constraints: Array<ByteConstraint> get() = TWENTY_BIT_CONSTRAINTS
},
SIXTEEN_BIT_BLOCK {
// 192.168.x.x
override val broadcastAddress: String get() = SIXTEEN_BIT_BROADCAST
override val constraints: Array<ByteConstraint> get() = SIXTEEN_BIT_CONSTRAINTS
};

/**
* Returns true if the given address matches this private network type.
*/
abstract val broadcastAddress: String
internal abstract val constraints: Array<ByteConstraint>

internal fun match(address: ByteArray): Boolean = address.run {
size == Int.SIZE_BYTES && zip(constraints).all { (byte, cons) -> cons.check(byte) }
internal fun match(address: String): Boolean = address.split('.').run {
size == Int.SIZE_BYTES && zip(constraints).all { (byte, cons) -> cons.check(byte.toByte()) }
}

companion object {
private const val TWENTY_FOUR_BIT_BROADCAST = "10.255.255.255"
private const val TWENTY_BIT_BROADCAST = "172.31.255.255"
private const val SIXTEEN_BIT_BROADCAST = "192.168.255.255"

private val TWENTY_FOUR_BIT_CONSTRAINTS = arrayOf<ByteConstraint>(
ByteConstraint.Equals(10),
)
Expand All @@ -46,7 +51,6 @@ enum class PrivateNetworkType {
* Returns the private network address type that matches the given address, or null if it's
* not a private network address.
*/
operator fun invoke(address: ByteArray): PrivateNetworkType? =
entries.find { it.match(address) }
fun of(address: String): PrivateNetworkType? = entries.find { it.match(address) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import kotlinx.coroutines.withTimeoutOrNull
* continually poll for servers.
* @author rjwut
*/
class ServerDiscoveryRequester(
internal val broadcastAddress: String =
(PrivateNetworkAddress.guessBest() ?: PrivateNetworkAddress.DEFAULT).hostAddress,
private val listener: Listener,
private val timeoutMs: Long
) {
class ServerDiscoveryRequester(private val listener: Listener, private val timeoutMs: Long) {
/**
* Interface for an object which is notified when a server is discovered or the discovery
* process ends.
Expand All @@ -39,7 +34,7 @@ class ServerDiscoveryRequester(
suspend fun onQuit()
}

suspend fun run() {
suspend fun run(broadcastAddress: String) {
SelectorManager(Dispatchers.IO).use { selector ->
aSocket(selector).udp().bind {
broadcast = true
Expand Down Expand Up @@ -76,6 +71,7 @@ class ServerDiscoveryRequester(

companion object {
const val PORT = 3100
const val DEFAULT_BROADCAST_ADDRESS = "255.255.255.255"
}

init {
Expand Down

This file was deleted.

Loading

0 comments on commit 94f66db

Please sign in to comment.