From 01feb0f7dd18e099c751c5c1d27077fa0385b385 Mon Sep 17 00:00:00 2001 From: isaacakakpo1 Date: Wed, 13 Mar 2024 11:47:21 +0000 Subject: [PATCH] Tests --- .../janusmessagesdk/android/MainActivity.kt | 1 - iosApp/iosApp/TestJanusClasses.swift | 27 +++++++++++-- .../janus/CreateTransaction.kt | 4 +- .../com/telnyx/janusmessagesdk/janus/Janus.kt | 38 +++++++++++++------ .../telnyx/janusmessagesdk/janus/Register.kt | 36 ++++++++++++++++-- 5 files changed, 85 insertions(+), 21 deletions(-) diff --git a/androidApp/src/main/java/com/telnyx/janusmessagesdk/android/MainActivity.kt b/androidApp/src/main/java/com/telnyx/janusmessagesdk/android/MainActivity.kt index 785bed5..0ca0510 100644 --- a/androidApp/src/main/java/com/telnyx/janusmessagesdk/android/MainActivity.kt +++ b/androidApp/src/main/java/com/telnyx/janusmessagesdk/android/MainActivity.kt @@ -1,6 +1,5 @@ package com.telnyx.janusmessagesdk.android -import android.os.Bundle import com.telnyx.janusmessagesdk.janus.JanusEvent import com.telnyx.janusmessagesdk.janus.RegisterSuccess import com.telnyx.janusmessagesdk.janus.decodeJanusMessage diff --git a/iosApp/iosApp/TestJanusClasses.swift b/iosApp/iosApp/TestJanusClasses.swift index e392f49..5b3021e 100644 --- a/iosApp/iosApp/TestJanusClasses.swift +++ b/iosApp/iosApp/TestJanusClasses.swift @@ -13,9 +13,30 @@ func testJanus(){ //CreateTransaction(janus: String, transaction: <#T##String#>) let createTransaction = CreateTransaction(transaction: "").default() //let result = try! JanusUtils().encode(cls: createTransaction) - JanusKt.decodeJanusMessage(message: "") { method, base in - method == JanusMethod.attach - base as! CreateTransaction + JanusKt.decodeJanusMessage(message: "{\n" + + " \"janus\": \"event\",\n" + + " \"plugindata\": {\n" + + " \"data\": {\n" + + " \"result\": {\n" + + " \"event\": \"registered\",\n" + + " \"headers\": {},\n" + + " \"master_id\": 2656454203,\n" + + " \"register_sent\": true,\n" + + " \"username\": \"isaac28055\"\n" + + " },\n" + + " \"sip\": \"event\"\n" + + " },\n" + + " \"plugin\": \"janus.plugin.sip\"\n" + + " },\n" + + " \"sender\": 485245946375374,\n" + + " \"session_id\": 2496119861992003\n" + + "}") { method, base in + if(method == JanusEvent.registered){ + var mom = base as! RegisterSuccess + print("Transaction => \(mom.sessionId)" ) + + } + print("method : \(method)") } print("Transaction => " + createTransaction.encode() ) diff --git a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/CreateTransaction.kt b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/CreateTransaction.kt index 6f31a2d..9800340 100644 --- a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/CreateTransaction.kt +++ b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/CreateTransaction.kt @@ -15,7 +15,7 @@ data class CreateTransaction( //for ios objective-c does not support default values in parameters fun default(): CreateTransaction { return CreateTransaction(transaction = UUID().toString()).apply { - janus = JanusEvent.CREATE.value() + janus = Janus.CREATE.value } } @@ -38,7 +38,7 @@ data class TransactionSuccess( val transaction: String, @SerialName("session_id") val sessionId: Long = 0L, -) : JanusBase(JanusEvent.SUCCESS.value()) { +) : JanusBase(Janus.SUCCESS.value) { fun encode(): String { return json.encodeToString(this) } diff --git a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Janus.kt b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Janus.kt index 3077469..aa9a319 100644 --- a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Janus.kt +++ b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Janus.kt @@ -9,15 +9,31 @@ val json = Json { encodeDefaults = true } +enum class JanusEventType(val value: String){ + SUCCESS("success"), + ERROR("error"), + EVENT("event"), + MESSAGE("message") +} - -enum class JanusEvent(val value: String) { - CREATE("create"), +enum class Janus(val value: String) { + ATTACH("attach"), + DETACH("detach"), + DESTROY("destroy"), KEEP_ALIVE("keepalive"), - SUCCESS("success"), + REGISTER("register"), + UNREGISTER("unregister"), + CREATE("create"), MESSAGE("message"), EVENT("event"), - ATTACH("attach"), + SUCCESS("success"), + ERROR("error") +} + +enum class JanusEvent(val value: String) { + SEEION_CREATED("create"), + KEEP_ALIVE("keepalive"), + PLUGIN_ATTACHED("attach"), REGISTERED("registered"), REGISTERING("registering"), UNREGISTER("unregister"), @@ -52,10 +68,10 @@ open class JanusBase( fun decodeJanusMessage(message: String, callback: (JanusEvent, JanusBase) -> Unit) { val janusBase = json.decodeFromString(message) when (janusBase.janus) { - JanusEvent.MESSAGE.value() -> { + JanusEventType.MESSAGE.value -> { } - JanusEvent.EVENT.value() -> { + JanusEventType.EVENT.value -> { val event = json.decodeFromString(message) if (event.plugindata?.data?.result?.event == JanusEvent.REGISTERING.value()) { @@ -66,14 +82,14 @@ fun decodeJanusMessage(message: String, callback: (JanusEvent, JanusBase) -> Uni callback(JanusEvent.REGISTERED,registerSuccess) } } - JanusEvent.SUCCESS.value() -> { - val success = json.decodeFromString(message) + JanusEventType.SUCCESS.value -> { + val result = json.decodeFromString(message) if (message.contains("session_id")) { //from attach plugin - callback(JanusEvent.ATTACH,success) + callback(JanusEvent.PLUGIN_ATTACHED,result) } else { - callback(JanusEvent.CREATE,success) + callback(JanusEvent.SEEION_CREATED,result) } } } diff --git a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Register.kt b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Register.kt index 2beed1c..ab10e7c 100644 --- a/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Register.kt +++ b/shared/src/commonMain/kotlin/com/telnyx/janusmessagesdk/janus/Register.kt @@ -2,6 +2,7 @@ package com.telnyx.janusmessagesdk.janus import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.encodeToString import kotlinx.uuid.UUID @@ -11,13 +12,25 @@ data class Register( val body: Body, @SerialName("handle_id") val handleId: Long, - @SerialName("janus") - val janus: String, @SerialName("session_id") val sessionId: Long = UUID().timeStamp, @SerialName("transaction") val transaction: String = UUID().toString() -) +) : JanusBase() { + fun encode(): String { + return json.encodeToString(this) + } + + fun default(handleId: Long, body: Body): Register { + return Register( + body = body, + handleId = handleId, + ).apply { + janus = Janus.MESSAGE.value + } + } + +} @Serializable data class Body( @@ -33,5 +46,20 @@ data class Body( val login: String, @SerialName("username") val username: String = "sip:$login@sipdev.telnyx.com" -) +) { + + init { + + } + fun default(login: String, loginToken: String,displayName: String, ): Body { + return Body( + displayName = displayName, + loginToken = loginToken, + login = login, + proxy = "sip:sipdev.telnyx.com:5060", + username = "sip:$login@sipdev.telnyx.com", + request = Janus.REGISTER.value + ) + } +}