Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case for acronyms #37

Merged
merged 5 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dependencyReport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ permissions:
contents: write
jobs:
library:
runs-on: ubuntu-22.04
runs-on: macos-13
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
Expand Down
11 changes: 5 additions & 6 deletions .scripts/github/dependency_report_as_github_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ JSON=$(jq --null-input \
--arg RUN_ID "$GITHUB_RUN_ID" \
--arg HTML_URL "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" \
--arg DETECTOR_NAME "$GITHUB_REPOSITORY" \
--arg DETECTOR_VERSION 1 \
--arg DETECTOR_VERSION 2 \
--arg DETECTOR_URL "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
--arg SCANNED "$SCANNED_AT" \
--arg MANIFEST_NAME "$MANIFEST_NAME" \
Expand Down Expand Up @@ -80,9 +80,8 @@ JSON=$(jq --null-input \
}
')

for LINE in $(cat $INPUT_FILE)
do
JSON=$(jq '.manifests.'$MANIFEST_NAME'.resolved += {"'$LINE'": {"package_url": "pkg:/maven/'$LINE'"}}' <<< $JSON)
done
while IFS= read -r LINE
do JSON=$(jq '.manifests.'"$MANIFEST_NAME"'.resolved += {"'"$LINE"'": {"package_url": "https://central.sonatype.com/artifact/'"$(echo "$LINE" | tr ':' '/')"'"}}' <<< "$JSON")
done < "$INPUT_FILE"

jq -r tostring <<< $JSON
jq -r tostring <<< "$JSON"
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tidal.networktime.internal

import com.tidal.networktime.AddressFamily

internal actual class AddressResolver {
actual operator fun invoke(
address: String,
addressFamilies: Array<out AddressFamily>,
includeIPv4: Boolean,
includeIPv6: Boolean,
): Iterable<String> = emptyList() // TODO
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tidal.networktime.internal

internal actual class NtpUdpSocketOperations {
internal actual class NTPUDPSocketOperations {
actual fun prepareSocket(timeoutMilliseconds: Long) {}

actual fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: Int) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import kotlin.time.Duration.Companion.seconds
* @param waitBetweenResolvedAddressQueries The amount of time to wait before consecutive requests
* to the same resolved address.
* @param ntpVersion The version number to write in packets.
* @param maxRootDelay The maximum delay to accept a packet. Packets with a root delay higher than
* this will be discarded.
* @param maxRootDispersion The maximum root dispersion to accept a packet. Packets with a root
* dispersion higher than this will be discarded.
*/
class NTPServer(
val name: String,
Expand All @@ -24,4 +28,6 @@ class NTPServer(
val queriesPerResolvedAddress: Short = 3,
val waitBetweenResolvedAddressQueries: Duration = 2.seconds,
val ntpVersion: NTPVersion = NTPVersion.FOUR,
val maxRootDelay: Duration = Duration.INFINITE,
val maxRootDispersion: Duration = Duration.INFINITE,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.tidal.networktime.internal

import com.tidal.networktime.AddressFamily

internal expect class AddressResolver() {
operator fun invoke(address: String, addressFamilies: Array<out AddressFamily>): Iterable<String>
operator fun invoke(address: String, includeIPv4: Boolean, includeIPv6: Boolean): Iterable<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import kotlin.time.Duration.Companion.milliseconds

@JvmInline
internal value class EpochTimestamp(val epochTime: Duration) {
val asNtpTimestamp: NtpTimestamp
val asNTPTimestamp: NTPTimestamp
get() {
val millis = epochTime.inWholeMilliseconds
val useBase1 = millis < NtpPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
val useBase1 = millis < NTPPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
val baseTimeMillis = millis -
if (useBase1) {
NtpPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_1_MILLISECONDS
NTPPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_1_MILLISECONDS
} else {
NtpPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
NTPPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
}
var seconds = baseTimeMillis / 1_000
if (useBase1) {
seconds = seconds or 0x80000000L
}
val fraction = baseTimeMillis % 1_000 * 0x100000000L / 1_000
return NtpTimestamp((seconds shl 32 or fraction).milliseconds)
return NTPTimestamp((seconds shl 32 or fraction).milliseconds)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ package com.tidal.networktime.internal
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

internal data class NtpExchangeResult(
internal data class NTPExchangeResult(
val returnTime: Duration,
val ntpPacket: NtpPacket,
val ntpPacket: NTPPacket,
) {
val roundTripDelay: Duration
get() = ntpPacket.run {
val originEpochMillis = originateEpochTimestamp.asEpochTimestamp.epochTime.inWholeMilliseconds
val receiveNtpMillis = receiveEpochTimestamp.ntpTime.inWholeMilliseconds
val receiveNTPMillis = receiveEpochTimestamp.ntpTime.inWholeMilliseconds
val receiveEpochMillis = receiveEpochTimestamp.asEpochTimestamp.epochTime.inWholeMilliseconds
val transmitNtpMillis = transmitEpochTimestamp.ntpTime.inWholeMilliseconds
val transmitNTPMillis = transmitEpochTimestamp.ntpTime.inWholeMilliseconds
val transmitEpochMillis = transmitEpochTimestamp.asEpochTimestamp
.epochTime
.inWholeMilliseconds
val returnTimeMillis = returnTime.inWholeMilliseconds
if (receiveNtpMillis == 0L || transmitNtpMillis == 0L) {
if (receiveNTPMillis == 0L || transmitNTPMillis == 0L) {
return@run if (returnTimeMillis >= originEpochMillis) {
(returnTimeMillis - originEpochMillis).milliseconds
} else {
Expand All @@ -40,26 +40,26 @@ internal data class NtpExchangeResult(

val clockOffset: Duration
get() = ntpPacket.run {
val originNtpMillis = originateEpochTimestamp.ntpTime.inWholeMilliseconds
val originNTPMillis = originateEpochTimestamp.ntpTime.inWholeMilliseconds
val originEpochMillis = originateEpochTimestamp.asEpochTimestamp.epochTime.inWholeMilliseconds
val receiveNtpMillis = receiveEpochTimestamp.ntpTime.inWholeMilliseconds
val receiveNTPMillis = receiveEpochTimestamp.ntpTime.inWholeMilliseconds
val receiveEpochMillis = receiveEpochTimestamp.asEpochTimestamp.epochTime.inWholeMilliseconds
val transmitNtpMillis = transmitEpochTimestamp.ntpTime.inWholeMilliseconds
val transmitNTPMillis = transmitEpochTimestamp.ntpTime.inWholeMilliseconds
val transmitEpochMillis = transmitEpochTimestamp.asEpochTimestamp
.epochTime
.inWholeMilliseconds
val returnTimeMillis = returnTime.inWholeMilliseconds
if (originNtpMillis == 0L) {
if (transmitNtpMillis != 0L) {
if (originNTPMillis == 0L) {
if (transmitNTPMillis != 0L) {
return@run (transmitEpochMillis - returnTimeMillis).milliseconds
}
return@run Duration.INFINITE
}
if (receiveNtpMillis == 0L || transmitNtpMillis == 0L) {
if (receiveNtpMillis != 0L) {
if (receiveNTPMillis == 0L || transmitNTPMillis == 0L) {
if (receiveNTPMillis != 0L) {
return@run (receiveEpochMillis - originEpochMillis).milliseconds
}
if (transmitNtpMillis != 0L) {
if (transmitNTPMillis != 0L) {
return@run (transmitEpochMillis - returnTimeMillis).milliseconds
}
return@run Duration.INFINITE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ package com.tidal.networktime.internal

import kotlin.time.Duration

internal class NtpExchanger(
internal class NTPExchanger(
private val referenceClock: KotlinXDateTimeSystemClock,
private val ntpPacketSerializer: NtpPacketSerializer,
private val ntpPacketDeserializer: NtpPacketDeserializer,
private val ntpPacketSerializer: NTPPacketSerializer,
private val ntpPacketDeserializer: NTPPacketDeserializer,
) {
operator fun invoke(
address: String,
queryTimeout: Duration,
ntpVersion: UByte,
): NtpExchangeResult? {
val ntpUdpSocketOperations = NtpUdpSocketOperations()
): NTPExchangeResult? {
val ntpUdpSocketOperations = NTPUDPSocketOperations()
return try {
ntpUdpSocketOperations.prepareSocket(queryTimeout.inWholeMilliseconds)
val ntpPacket = NtpPacket(versionNumber = ntpVersion.toInt(), mode = NTP_MODE_CLIENT)
val ntpPacket = NTPPacket(versionNumber = ntpVersion.toInt(), mode = NTP_MODE_CLIENT)
val requestTime = referenceClock.referenceEpochTime
ntpPacket.transmitEpochTimestamp = EpochTimestamp(requestTime).asNtpTimestamp
ntpPacket.transmitEpochTimestamp = EpochTimestamp(requestTime).asNTPTimestamp
val buffer = ntpPacketSerializer(ntpPacket)
ntpUdpSocketOperations.exchangePacketInPlace(
buffer,
address,
NTP_PORT_NUMBER,
)
val returnTime = referenceClock.referenceEpochTime
ntpPacketDeserializer(buffer)?.let { NtpExchangeResult(returnTime, it) }
ntpPacketDeserializer(buffer)?.let { NTPExchangeResult(returnTime, it) }
} catch (_: Throwable) {
null
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tidal.networktime.internal

import kotlin.time.Duration

internal data class NtpPacket(
internal data class NTPPacket(
val leapIndicator: Int = 0,
val versionNumber: Int,
val mode: Int,
Expand All @@ -12,11 +12,11 @@ internal data class NtpPacket(
val rootDelay: Duration = Duration.INFINITE,
val rootDispersion: Duration = Duration.INFINITE,
val referenceIdentifier: String = "",
val referenceEpochTimestamp: NtpTimestamp = NtpTimestamp(Duration.ZERO),
val originateEpochTimestamp: NtpTimestamp = NtpTimestamp(Duration.ZERO),
val receiveEpochTimestamp: NtpTimestamp = NtpTimestamp(Duration.ZERO),
val referenceEpochTimestamp: NTPTimestamp = NTPTimestamp(Duration.ZERO),
val originateEpochTimestamp: NTPTimestamp = NTPTimestamp(Duration.ZERO),
val receiveEpochTimestamp: NTPTimestamp = NTPTimestamp(Duration.ZERO),
/** Keep this mutable to minimize delay (avoids an allocation) **/
var transmitEpochTimestamp: NtpTimestamp = NtpTimestamp(Duration.ZERO),
var transmitEpochTimestamp: NTPTimestamp = NTPTimestamp(Duration.ZERO),
) {
companion object {
const val NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS = 2085978496000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

internal class NtpPacketDeserializer {
operator fun invoke(bytes: ByteArray): NtpPacket? {
internal class NTPPacketDeserializer {
operator fun invoke(bytes: ByteArray): NTPPacket? {
var index = 0
val leapIndicator = (bytes[index].toInt() shr 6) and 0b11
if (leapIndicator == LEAP_INDICATOR_CLOCK_UNSYNCHRONIZED) {
Expand All @@ -24,20 +24,20 @@ internal class NtpPacketDeserializer {
}
val poll = bytes[index++].asSignedIntToThePowerOf2.seconds
val precision = bytes[index++].asSignedIntToThePowerOf2.milliseconds
val rootDelay = bytes.sliceArray(index until index + 4).asNtpIntervalToInterval
val rootDelay = bytes.sliceArray(index until index + 4).asNTPIntervalToInterval
index += 4
val rootDispersion = bytes.sliceArray(index until index + 4).asNtpIntervalToInterval
val rootDispersion = bytes.sliceArray(index until index + 4).asNTPIntervalToInterval
index += 4
val referenceIdentifier = bytes.sliceArray(index until index + 4).decodeToString()
index += 4
val reference = bytes.sliceArray(index until index + 8).asNtpTimestamp
val reference = bytes.sliceArray(index until index + 8).asNTPTimestamp
index += 8
val originate = bytes.sliceArray(index until index + 8).asNtpTimestamp
val originate = bytes.sliceArray(index until index + 8).asNTPTimestamp
index += 8
val receive = bytes.sliceArray(index until index + 8).asNtpTimestamp
val receive = bytes.sliceArray(index until index + 8).asNTPTimestamp
index += 8
val transmit = bytes.sliceArray(index until index + 8).asNtpTimestamp
return NtpPacket(
val transmit = bytes.sliceArray(index until index + 8).asNTPTimestamp
return NTPPacket(
leapIndicator,
versionNumber,
mode,
Expand All @@ -60,7 +60,7 @@ internal class NtpPacketDeserializer {
private val Byte.asUnsignedInt: Int
get() = toUByte().toInt()

private val ByteArray.asNtpIntervalToInterval: Duration
private val ByteArray.asNTPIntervalToInterval: Duration
get() {
var index = 0
val seconds = (this[index++].asUnsignedInt shl 8) + this[index++].asUnsignedInt
Expand All @@ -72,7 +72,7 @@ internal class NtpPacketDeserializer {
private val Byte.asUnsignedLong: Long
get() = toUByte().toLong()

private val ByteArray.asNtpTimestamp: NtpTimestamp
private val ByteArray.asNTPTimestamp: NTPTimestamp
get() {
var index = 0
val ntpMillis = (this[index++].asUnsignedLong shl 56) or
Expand All @@ -83,7 +83,7 @@ internal class NtpPacketDeserializer {
(this[index++].asUnsignedLong shl 16) or
(this[index++].asUnsignedLong shl 8) or
this[index].asUnsignedLong
return NtpTimestamp(ntpMillis.milliseconds)
return NTPTimestamp(ntpMillis.milliseconds)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.tidal.networktime.internal

import kotlin.time.Duration

internal class NtpPacketSerializer {
operator fun invoke(ntpPacket: NtpPacket) = ntpPacket.run {
internal class NTPPacketSerializer {
operator fun invoke(ntpPacket: NTPPacket) = ntpPacket.run {
ByteArray(48).apply {
set(0, ((0 shl 6) or (versionNumber shl 3) or mode).toByte())
transmitEpochTimestamp.ntpTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.time.Duration.Companion.milliseconds
* non-computed property.
*/
@JvmInline
internal value class NtpTimestamp(val ntpTime: Duration) {
internal value class NTPTimestamp(val ntpTime: Duration) {
val asEpochTimestamp: EpochTimestamp
get() {
val ntpTimeValue = ntpTime.inWholeMilliseconds
Expand All @@ -18,9 +18,9 @@ internal value class NtpTimestamp(val ntpTime: Duration) {
val mostSignificantBit = seconds and 0x80000000L
return (
if (mostSignificantBit == 0L) {
NtpPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
NTPPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_0_MILLISECONDS
} else {
NtpPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_1_MILLISECONDS
NTPPacket.NTP_TIMESTAMP_BASE_WITH_EPOCH_MSB_1_MILLISECONDS
} + seconds * 1000 + fraction
).milliseconds.let { EpochTimestamp(it) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tidal.networktime.internal

internal expect class NtpUdpSocketOperations() {
internal expect class NTPUDPSocketOperations() {
fun prepareSocket(timeoutMilliseconds: Long)

fun exchangePacketInPlace(buffer: ByteArray, address: String, portNumber: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ internal class SyncPeriodic(
private val syncInterval: Duration,
private val referenceClock: KotlinXDateTimeSystemClock,
private val mutableState: MutableState,
private val ntpExchanger: NtpExchanger = NtpExchanger(
private val ntpExchanger: NTPExchanger = NTPExchanger(
referenceClock,
NtpPacketSerializer(),
NtpPacketDeserializer(),
NTPPacketSerializer(),
NTPPacketDeserializer(),
),
) {
suspend operator fun invoke() {
Expand Down
Loading
Loading