Skip to content

Commit

Permalink
Merge pull request #4 from kaltura/FEC-11495
Browse files Browse the repository at this point in the history
FEC-11495 Returning CDNList information containing cdn code from executor
  • Loading branch information
GouravSna authored Sep 9, 2021
2 parents a9ed412 + 18bf288 commit 97ac6e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ open class SmartSwitchEvent(type: Type?): PKEvent {
}

companion object {
@JvmField
var error = ErrorEvent::class.java
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ internal class SmartSwitchExecutor {

private val smartSwitchExecutor: ExecutorService = Executors.newSingleThreadExecutor()

@Nullable
fun sendRequestToYoubora(accountCode: String, originCode: String,
resourceUrl: String?,
@Nullable optionalParams: HashMap<String, String>?,
@Nullable smartSwitchUrl: String): Future<Pair<String, String?>?>? {
@Nullable smartSwitchUrl: String): Future<Any?>? {
val sendConfigToYoubora = SendConfigToYoubora(smartSwitchUrl, accountCode, originCode, resourceUrl, optionalParams)
return smartSwitchExecutor.submit(sendConfigToYoubora)
}
Expand All @@ -44,7 +45,7 @@ internal class SmartSwitchExecutor {
val accountCode: String,
val originCode: String,
var resourceUrl: String?,
val optionalParams: HashMap<String, String>?): Callable<Pair<String, String?>?> {
val optionalParams: HashMap<String, String>?): Callable<Any?> {

private val log: PKLog = PKLog.get("SmartSwitchExecutor")

Expand All @@ -56,8 +57,9 @@ internal class SmartSwitchExecutor {
private val resourceKey = "resource"
private val originCodeKey = "originCode"
private var errorMessage = "Invalid Response"
private var cdnList: CDNList? = null

override fun call(): Pair<String, String?> {
override fun call(): Any {
var connection: HttpURLConnection? = null
var inputStream: InputStream? = null
var bufferedReader: BufferedReader? = null
Expand Down Expand Up @@ -85,14 +87,13 @@ internal class SmartSwitchExecutor {
log.d("SmartSwitch Response: ${stringBuilder}")
val smartSwitchParser: SmartSwitchParser? = Gson().fromJson(stringBuilder.toString(), SmartSwitchParser::class.java)
if (smartSwitchParser?.smartSwitch != null) {
val cdnList: CDNList? = parseSmartSwitchResponse(smartSwitchParser)
cdnList = parseSmartSwitchResponse(smartSwitchParser)
if (cdnList != null) {
resourceUrl = cdnList.URL
log.d("Success response CDN_URL: ${cdnList.URL} CDN_NAME: ${cdnList.CDN_NAME}")
log.d("Success response CDN_CODE: ${cdnList.CDN_CODE} CDN_SCORE: ${cdnList.CDN_SCORE}")
log.d("Success response CDN_URL: ${cdnList!!.url} CDN_NAME: ${cdnList!!.cdnName}")
log.d("Success response CDN_CODE: ${cdnList!!.cdnCode} CDN_SCORE: ${cdnList!!.cdnScore}")
} else {
errorMessage = "CDNList is empty"
return error(resourceUrl, errorMessage)
return errorMessage
}
} else {
val smartSwitchError: SmartSwitchErrorResponse? = Gson().fromJson(stringBuilder.toString(), SmartSwitchErrorResponse::class.java)
Expand All @@ -107,34 +108,34 @@ internal class SmartSwitchExecutor {
}
}
}
return error(resourceUrl, errorMessage)
return errorMessage
}
} else {
errorMessage = connection.responseMessage
log.e("connection.responseMessage: $errorMessage")
log.e("connection.responseCode: ${connection.responseCode}")
return error(resourceUrl, errorMessage)
return errorMessage
}
} catch (malformedUrlException: MalformedURLException) {
log.e("SmartSwitch MalformedURLException: ${malformedUrlException.message}")
malformedUrlException.message?.let {
errorMessage = "SmartSwitch MalformedURLException: $it"
}
return error(resourceUrl, errorMessage)
return errorMessage
} catch (exception: IOException) {
log.e("SmartSwitch IOException: ${exception.message}")
exception.message?.let {
errorMessage = "SmartSwitch IOException: $it"
}
return error(resourceUrl, errorMessage)
return errorMessage
} finally {
bufferedReader?.close()
inputStream?.close()
connection?.disconnect()
log.d("Connection resources has been cleaned.")
log.d("Connection resources have been cleaned.")
}

return Pair(resourceUrl!!, null)
return cdnList!!
}

/**
Expand Down Expand Up @@ -179,9 +180,5 @@ internal class SmartSwitchExecutor {
}
return listOfCdn
}

private fun error(url: String?, errorMessage: String): Pair<String, String> {
return Pair(url!!, errorMessage)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kaltura.playkit.plugins.smartswitch

import android.content.Context
import com.kaltura.playkit.*
import com.kaltura.playkit.plugins.smartswitch.pluginconfig.CDNList
import com.kaltura.playkit.plugins.smartswitch.pluginconfig.SmartSwitchConfig
import com.kaltura.tvplayer.PKMediaEntryInterceptor
import java.util.concurrent.Future
Expand Down Expand Up @@ -45,14 +46,23 @@ class SmartSwitchPlugin: PKPlugin(), PKMediaEntryInterceptor {
val sourceUrl = mediaSource.url
if (!sourceUrl.isNullOrEmpty()) {
smartSwitchExecutor = SmartSwitchExecutor()
val sendRequestToYoubora: Future<Pair<String, String?>?>? = smartSwitchExecutor?.sendRequestToYoubora(accountCode!!, originCode!!, sourceUrl, optionalParams, smartSwitchUrl!!)
val responsePair = sendRequestToYoubora?.get() as Pair
val isErrorResponse = responsePair.second
val url = responsePair.first
if (isErrorResponse == null) {
mediaSource.url = url
} else {
errorMessage = isErrorResponse
val sendRequestToYoubora: Future<Any?>? = smartSwitchExecutor?.sendRequestToYoubora(accountCode!!, originCode!!, sourceUrl, optionalParams, smartSwitchUrl!!)
val response = sendRequestToYoubora?.get()
response?.let { res ->
when (res) {
is CDNList -> {
mediaSource.url = res.url
messageBus?.post(InterceptorEvent.CdnSwitchedEvent(
InterceptorEvent.Type.CDN_SWITCHED,
res.cdnCode))
}
is String -> {
errorMessage = res
}
else -> {
errorMessage = "Unknown error in SmartSwitch response."
}
}
}
smartSwitchExecutor?.terminateService()
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kaltura.playkit.plugins.smartswitch.pluginconfig

import androidx.annotation.Nullable
import com.google.gson.annotations.SerializedName

class SmartSwitchParser {
@Nullable
Expand All @@ -18,13 +19,17 @@ class SmartSwitch {

class CDNList {
@Nullable
var CDN_NAME: String? = null
@SerializedName("CDN_NAME")
var cdnName: String? = null
@Nullable
var CDN_CODE: String? = null
@SerializedName("CDN_CODE")
var cdnCode: String? = null
@Nullable
var URL: String? = null
@SerializedName("URL")
var url: String? = null
@Nullable
var CDN_SCORE: Float? = null
@SerializedName("CDN_SCORE")
var cdnScore: Float? = null
}

class SmartSwitchErrorResponse {
Expand Down

0 comments on commit 97ac6e4

Please sign in to comment.