Skip to content

Commit 2f984a8

Browse files
committed
update ndt7 lib from gson to kotlin serialization
1 parent 6dee7ff commit 2f984a8

File tree

8 files changed

+147
-93
lines changed

8 files changed

+147
-93
lines changed

.github/workflows/android.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: LCL Measurement Tool CI
2+
3+
env:
4+
# The name of the main module repository
5+
main_project_module: app
6+
7+
on: [ push, pull_request ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v3
18+
with:
19+
java-version: '11'
20+
distribution: 'temurin'
21+
cache: gradle
22+
- name: Grant execute permission for gradlew
23+
run: chmod +x gradlew
24+
- name: Build with Gradle
25+
run: ./gradlew build
26+
- name: Generate APK Debug Build with Gradle
27+
if: startsWith(github.ref, 'refs/tags/')
28+
run: ./gradlew assembleDebug
29+
- name: Upload to Github Page
30+
uses: softprops/action-gh-release@v1
31+
if: startsWith(github.ref, 'refs/tags/')
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
with:
35+
files: ${{ env.main_project_module }}/build/outputs/apk/debug/app-debug.apk
36+
draft: true
37+
prerelease: true

libndt7/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: "com.android.library"
22
apply plugin: "kotlin-android"
33
apply plugin: "maven-publish"
4+
apply plugin: "kotlinx-serialization"
45

56
android {
67

@@ -50,7 +51,8 @@ android {
5051
dependencies {
5152
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
5253
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
53-
implementation "com.google.code.gson:gson:$gson_version"
54+
// implementation "com.google.code.gson:gson:$gson_version"
55+
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0"
5456

5557
// androidTestImplementation "androidx.test:runner:1.5.2"
5658
}

libndt7/src/main/java/net/measurementlab/ndt7/android/Downloader.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@file:JvmName("Downloader")
22
package net.measurementlab.ndt7.android
33

4-
import com.google.gson.Gson
4+
import kotlinx.serialization.decodeFromString
5+
import kotlinx.serialization.json.Json
56
import net.measurementlab.ndt7.android.models.CallbackRegistry
67
import net.measurementlab.ndt7.android.models.Measurement
78
import net.measurementlab.ndt7.android.utils.DataConverter.currentTimeInMicroseconds
@@ -26,7 +27,6 @@ class Downloader(
2627
private var startTime: Long = 0
2728
private var previous: Long = 0
2829
private var numBytes = 0.0
29-
private val gson = Gson()
3030
private var webSocket: WebSocket? = null
3131

3232
override fun onOpen(webSocket: WebSocket, response: Response) {
@@ -38,7 +38,7 @@ class Downloader(
3838
tryToUpdateClient()
3939

4040
try {
41-
val measurement = gson.fromJson(text, Measurement::class.java)
41+
val measurement = Json.decodeFromString<Measurement>(text)
4242
cbRegistry.measurementProgressCbk(measurement)
4343
} catch (e: Exception) {
4444
return

libndt7/src/main/java/net/measurementlab/ndt7/android/NDTTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@file:JvmName("NDTTest")
22
package net.measurementlab.ndt7.android
33

4-
import com.google.gson.Gson
4+
import kotlinx.serialization.decodeFromString
5+
import kotlinx.serialization.json.Json
56
import net.measurementlab.ndt7.android.models.CallbackRegistry
67
import net.measurementlab.ndt7.android.models.HostnameResponse
78
import net.measurementlab.ndt7.android.models.Urls
@@ -49,8 +50,8 @@ abstract class NDTTest(private var httpClient: OkHttpClient? = null) : DataPubli
4950

5051
override fun onResponse(call: Call, response: Response) {
5152
try {
52-
val hostInfo: HostnameResponse = Gson().fromJson(response.body?.string(), HostnameResponse::class.java)
53-
val numUrls = hostInfo.results?.size!!
53+
val hostInfo: HostnameResponse? = response.body?.string()?.let { Json.decodeFromString<HostnameResponse>(it) }
54+
val numUrls = hostInfo?.results?.size ?: return
5455
for (i in 0 until numUrls) {
5556
try {
5657
selectTestType(testType, hostInfo.results[i].urls, speedtestLock)

libndt7/src/main/java/net/measurementlab/ndt7/android/Uploader.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@file:JvmName("Uploader")
22
package net.measurementlab.ndt7.android
33

4-
import com.google.gson.Gson
4+
import kotlinx.serialization.decodeFromString
5+
import kotlinx.serialization.json.Json
56
import net.measurementlab.ndt7.android.NDTTest.TestType
67
import net.measurementlab.ndt7.android.models.CallbackRegistry
78
import net.measurementlab.ndt7.android.models.Measurement
@@ -29,12 +30,12 @@ class Uploader(
2930
private var startTime: Long = 0
3031
private var previous: Long = 0
3132
private var totalBytesSent = 0.0
32-
private val gson = Gson()
33+
// private val gson = Gson()
3334

3435
override fun onMessage(webSocket: WebSocket, text: String) {
3536

3637
try {
37-
val measurement = gson.fromJson(text, Measurement::class.java)
38+
val measurement = Json.decodeFromString<Measurement>(text)
3839
cbRegistry.measurementProgressCbk(measurement)
3940
} catch (e: Exception) {
4041
// we don't care that much if a single measurement has trouble
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package net.measurementlab.ndt7.android.models
22

3-
import com.google.gson.annotations.SerializedName
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
45

6+
@Serializable
57
data class ClientResponse(
6-
@SerializedName("AppInfo") val appInfo: AppInfo,
7-
@SerializedName("Origin") val origin: String = "client",
8-
@SerializedName("Test") val test: String
8+
@SerialName("AppInfo") val appInfo: AppInfo,
9+
@SerialName("Origin") val origin: String = "client",
10+
@SerialName("Test") val test: String
911
)
1012

13+
@Serializable
1114
data class AppInfo(
12-
@SerializedName("ElapsedTime") val elapsedTime: Long,
13-
@SerializedName("NumBytes") val numBytes: Double
15+
@SerialName("ElapsedTime") val elapsedTime: Long,
16+
@SerialName("NumBytes") val numBytes: Double
1417
)
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
@file:JvmName("HostnameResponse")
22
package net.measurementlab.ndt7.android.models
33

4-
import com.google.gson.annotations.SerializedName
4+
import kotlinx.serialization.Serializable
5+
import kotlinx.serialization.SerialName
56

7+
@Serializable
68
data class HostnameResponse(
7-
@SerializedName("results")
8-
val results: List<Result>?
9+
@SerialName("results")
10+
val results: List<Result>? = null
911
)
1012

13+
@Serializable
1114
data class Result(
12-
@SerializedName("location")
15+
@SerialName("location")
1316
val location: Location,
14-
@SerializedName("machine")
17+
@SerialName("machine")
1518
val machine: String,
16-
@SerializedName("urls")
19+
@SerialName("urls")
1720
val urls: Urls
1821
)
1922

23+
@Serializable
2024
data class Location(
21-
@SerializedName("city")
25+
@SerialName("city")
2226
val city: String,
23-
@SerializedName("country")
27+
@SerialName("country")
2428
val country: String
2529
)
2630

31+
@Serializable
2732
data class Urls(
28-
@SerializedName("ws:///ndt/v7/download")
33+
@SerialName("ws:///ndt/v7/download")
2934
val ndt7DownloadWS: String,
30-
@SerializedName("ws:///ndt/v7/upload")
35+
@SerialName("ws:///ndt/v7/upload")
3136
val ndt7UploadWS: String,
32-
@SerializedName("wss:///ndt/v7/download")
37+
@SerialName("wss:///ndt/v7/download")
3338
val ndt7DownloadWSS: String,
34-
@SerializedName("wss:///ndt/v7/upload")
39+
@SerialName("wss:///ndt/v7/upload")
3540
val ndt7UploadWSS: String
3641
)
Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
11
@file:JvmName("Measurement")
22
package net.measurementlab.ndt7.android.models
33

4-
import com.google.gson.annotations.SerializedName
4+
import kotlinx.serialization.SerialName
5+
import kotlinx.serialization.Serializable
56

7+
@Serializable
68
data class Measurement(
7-
@SerializedName("ConnectionInfo") val connectionInfo: ConnectionInfo,
8-
@SerializedName("BBRInfo") val bbrInfo: BBRInfo?,
9-
@SerializedName("TCPInfo") val tcpInfo: TCPInfo?
9+
@SerialName("ConnectionInfo") val connectionInfo: ConnectionInfo,
10+
@SerialName("BBRInfo") val bbrInfo: BBRInfo?,
11+
@SerialName("TCPInfo") val tcpInfo: TCPInfo?
1012
)
1113

14+
@Serializable
1215
data class ConnectionInfo(
13-
@SerializedName("Client") val client: String,
14-
@SerializedName("Server") val server: String,
15-
@SerializedName("UUID") val uuid: String
16+
@SerialName("Client") val client: String,
17+
@SerialName("Server") val server: String,
18+
@SerialName("UUID") val uuid: String
1619
)
1720

21+
@Serializable
1822
data class BBRInfo(
19-
@SerializedName("BW") val bw: Long?,
20-
@SerializedName("MinRTT") val minRtt: Long?,
21-
@SerializedName("PacingGain") val pacingGain: Long?,
22-
@SerializedName("CwndGain") val cwndGain: Long?,
23-
@SerializedName("ElapsedTime") val elapsedTime: Long?
23+
@SerialName("BW") val bw: Long?,
24+
@SerialName("MinRTT") val minRtt: Long?,
25+
@SerialName("PacingGain") val pacingGain: Long?,
26+
@SerialName("CwndGain") val cwndGain: Long?,
27+
@SerialName("ElapsedTime") val elapsedTime: Long?
2428
)
2529

30+
@Serializable
2631
data class TCPInfo(
27-
@SerializedName("State") var state: Long?,
28-
@SerializedName("CAState") val CaState: Long?,
29-
@SerializedName("Retransmits") val retransmits: Long?,
30-
@SerializedName("Probes") val probes: Long?,
31-
@SerializedName("Backoff") val backoff: Long?,
32-
@SerializedName("Options") val options: Long?,
33-
@SerializedName("WScale") val wScale: Long?,
34-
@SerializedName("AppLimited") val appLimited: Long?,
35-
@SerializedName("RTO") val rto: Long?,
36-
@SerializedName("ATO") val ato: Long?,
37-
@SerializedName("SndMSS") val sndMss: Long?,
38-
@SerializedName("RcvMSS") val rcvMss: Long?,
39-
@SerializedName("Unacked") val unacked: Long?,
40-
@SerializedName("Sacked") val sacked: Long?,
41-
@SerializedName("Lost") val lost: Long?,
42-
@SerializedName("Retrans") val retrans: Long?,
43-
@SerializedName("Fackets") val fackets: Long?,
44-
@SerializedName("LastDataSent") val lastDataSent: Long?,
45-
@SerializedName("LastAckSent") val lastAckSent: Long?,
46-
@SerializedName("LastDataRecv") val lastDataRecv: Long?,
47-
@SerializedName("LastAckRecv") val lastAckRecv: Long?,
48-
@SerializedName("PMTU") val pmtu: Long?,
49-
@SerializedName("RcvSsThresh") val rcvSsThresh: Long?,
50-
@SerializedName("RTT") val rtt: Long?,
51-
@SerializedName("RTTVar") val rttVar: Long?,
52-
@SerializedName("SndSsThresh") val sndSsThresth: Long?,
53-
@SerializedName("SndCwnd") val sndCwnd: Long?,
54-
@SerializedName("AdvMSS") val advMss: Long?,
55-
@SerializedName("Reordering") val reordering: Long?,
56-
@SerializedName("RcvRTT") val rcvRtt: Long?,
57-
@SerializedName("RcvSpace") val rcvSpace: Long?,
58-
@SerializedName("TotalRetrans") val totalRetrans: Long?,
59-
@SerializedName("PacingRate") val pacingRate: Long?,
60-
@SerializedName("MaxPacingRate") val maxPacingRate: Long?,
61-
@SerializedName("BytesAcked") val bytesAcked: Long?,
62-
@SerializedName("BytesReceived") val bytesReceived: Long?,
63-
@SerializedName("SegsOut") val segsOut: Long?,
64-
@SerializedName("SegsIn") val segsIn: Long?,
65-
@SerializedName("NotsentBytes") val notSentBytes: Long?,
66-
@SerializedName("MinRTT") val minRtt: Long?,
67-
@SerializedName("DataSegsIn") val dataSegsIn: Long?,
68-
@SerializedName("DataSegsOut") val dataSegsOut: Long?,
69-
@SerializedName("DeliveryRate") val deliveryRate: Long?,
70-
@SerializedName("BusyTime") val busyTime: Long?,
71-
@SerializedName("RWndLimited") val rWndLimited: Long?,
72-
@SerializedName("SndBufLimited") val sndBufLimited: Long?,
73-
@SerializedName("Delivered") val delivered: Long?,
74-
@SerializedName("DeliveredCE") val deliveredCE: Long?,
75-
@SerializedName("BytesSent") val bytesSent: Long?,
76-
@SerializedName("BytesRetrans") val bytesRetrans: Long?,
77-
@SerializedName("DSackDups") val dSackDups: Long?,
78-
@SerializedName("ReordSeen") val reordSeen: Long?,
79-
@SerializedName("ElapsedTime") val elapsedTime: Long?
32+
@SerialName("State") var state: Long?,
33+
@SerialName("CAState") val CaState: Long?,
34+
@SerialName("Retransmits") val retransmits: Long?,
35+
@SerialName("Probes") val probes: Long?,
36+
@SerialName("Backoff") val backoff: Long?,
37+
@SerialName("Options") val options: Long?,
38+
@SerialName("WScale") val wScale: Long?,
39+
@SerialName("AppLimited") val appLimited: Long?,
40+
@SerialName("RTO") val rto: Long?,
41+
@SerialName("ATO") val ato: Long?,
42+
@SerialName("SndMSS") val sndMss: Long?,
43+
@SerialName("RcvMSS") val rcvMss: Long?,
44+
@SerialName("Unacked") val unacked: Long?,
45+
@SerialName("Sacked") val sacked: Long?,
46+
@SerialName("Lost") val lost: Long?,
47+
@SerialName("Retrans") val retrans: Long?,
48+
@SerialName("Fackets") val fackets: Long?,
49+
@SerialName("LastDataSent") val lastDataSent: Long?,
50+
@SerialName("LastAckSent") val lastAckSent: Long?,
51+
@SerialName("LastDataRecv") val lastDataRecv: Long?,
52+
@SerialName("LastAckRecv") val lastAckRecv: Long?,
53+
@SerialName("PMTU") val pmtu: Long?,
54+
@SerialName("RcvSsThresh") val rcvSsThresh: Long?,
55+
@SerialName("RTT") val rtt: Long?,
56+
@SerialName("RTTVar") val rttVar: Long?,
57+
@SerialName("SndSsThresh") val sndSsThresth: Long?,
58+
@SerialName("SndCwnd") val sndCwnd: Long?,
59+
@SerialName("AdvMSS") val advMss: Long?,
60+
@SerialName("Reordering") val reordering: Long?,
61+
@SerialName("RcvRTT") val rcvRtt: Long?,
62+
@SerialName("RcvSpace") val rcvSpace: Long?,
63+
@SerialName("TotalRetrans") val totalRetrans: Long?,
64+
@SerialName("PacingRate") val pacingRate: Long?,
65+
@SerialName("MaxPacingRate") val maxPacingRate: Long?,
66+
@SerialName("BytesAcked") val bytesAcked: Long?,
67+
@SerialName("BytesReceived") val bytesReceived: Long?,
68+
@SerialName("SegsOut") val segsOut: Long?,
69+
@SerialName("SegsIn") val segsIn: Long?,
70+
@SerialName("NotsentBytes") val notSentBytes: Long?,
71+
@SerialName("MinRTT") val minRtt: Long?,
72+
@SerialName("DataSegsIn") val dataSegsIn: Long?,
73+
@SerialName("DataSegsOut") val dataSegsOut: Long?,
74+
@SerialName("DeliveryRate") val deliveryRate: Long?,
75+
@SerialName("BusyTime") val busyTime: Long?,
76+
@SerialName("RWndLimited") val rWndLimited: Long?,
77+
@SerialName("SndBufLimited") val sndBufLimited: Long?,
78+
@SerialName("Delivered") val delivered: Long?,
79+
@SerialName("DeliveredCE") val deliveredCE: Long?,
80+
@SerialName("BytesSent") val bytesSent: Long?,
81+
@SerialName("BytesRetrans") val bytesRetrans: Long?,
82+
@SerialName("DSackDups") val dSackDups: Long?,
83+
@SerialName("ReordSeen") val reordSeen: Long?,
84+
@SerialName("ElapsedTime") val elapsedTime: Long?
8085
)

0 commit comments

Comments
 (0)