Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacakakpo1 committed Mar 13, 2024
1 parent b5d48b8 commit 01feb0f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 24 additions & 3 deletions iosApp/iosApp/TestJanusClasses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -52,10 +68,10 @@ open class JanusBase(
fun decodeJanusMessage(message: String, callback: (JanusEvent, JanusBase) -> Unit) {
val janusBase = json.decodeFromString<JanusBase>(message)
when (janusBase.janus) {
JanusEvent.MESSAGE.value() -> {
JanusEventType.MESSAGE.value -> {

}
JanusEvent.EVENT.value() -> {
JanusEventType.EVENT.value -> {
val event = json.decodeFromString<EventBase>(message)

if (event.plugindata?.data?.result?.event == JanusEvent.REGISTERING.value()) {
Expand All @@ -66,14 +82,14 @@ fun decodeJanusMessage(message: String, callback: (JanusEvent, JanusBase) -> Uni
callback(JanusEvent.REGISTERED,registerSuccess)
}
}
JanusEvent.SUCCESS.value() -> {
val success = json.decodeFromString<TransactionSuccess>(message)
JanusEventType.SUCCESS.value -> {
val result = json.decodeFromString<TransactionSuccess>(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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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(
Expand All @@ -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
)
}
}

0 comments on commit 01feb0f

Please sign in to comment.