From 75f968bda8e3f6fe4de87fc5d308641837e7ceef Mon Sep 17 00:00:00 2001 From: Gourav Saxena Date: Tue, 7 Sep 2021 17:02:50 +0530 Subject: [PATCH 1/5] - Returning CDNList information containing cdn code from executor --- .../smartswitch/SmartSwitchExecutor.kt | 33 +++++++++---------- .../plugins/smartswitch/SmartSwitchPlugin.kt | 26 ++++++++++----- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt index 6ae496b..28637b9 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt @@ -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?, - @Nullable smartSwitchUrl: String): Future?>? { + @Nullable smartSwitchUrl: String): Future? { val sendConfigToYoubora = SendConfigToYoubora(smartSwitchUrl, accountCode, originCode, resourceUrl, optionalParams) return smartSwitchExecutor.submit(sendConfigToYoubora) } @@ -44,7 +45,7 @@ internal class SmartSwitchExecutor { val accountCode: String, val originCode: String, var resourceUrl: String?, - val optionalParams: HashMap?): Callable?> { + val optionalParams: HashMap?): Callable { private val log: PKLog = PKLog.get("SmartSwitchExecutor") @@ -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 { + override fun call(): Any { var connection: HttpURLConnection? = null var inputStream: InputStream? = null var bufferedReader: BufferedReader? = null @@ -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!!.CDN_NAME}") + log.d("Success response CDN_CODE: ${cdnList!!.CDN_CODE} CDN_SCORE: ${cdnList!!.CDN_SCORE}") } else { errorMessage = "CDNList is empty" - return error(resourceUrl, errorMessage) + return errorMessage } } else { val smartSwitchError: SmartSwitchErrorResponse? = Gson().fromJson(stringBuilder.toString(), SmartSwitchErrorResponse::class.java) @@ -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!! } /** @@ -179,9 +180,5 @@ internal class SmartSwitchExecutor { } return listOfCdn } - - private fun error(url: String?, errorMessage: String): Pair { - return Pair(url!!, errorMessage) - } } } diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt index 51501cc..54f398c 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt @@ -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 @@ -45,14 +46,23 @@ class SmartSwitchPlugin: PKPlugin(), PKMediaEntryInterceptor { val sourceUrl = mediaSource.url if (!sourceUrl.isNullOrEmpty()) { smartSwitchExecutor = SmartSwitchExecutor() - val sendRequestToYoubora: Future?>? = 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? = 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.CDN_CODE)) + } + is String -> { + errorMessage = res + } + else -> { + errorMessage = "Unknown error in SmartSwitch response." + } + } } smartSwitchExecutor?.terminateService() } else { From 20a886d6fc282e6c746c101f696441220f50a786 Mon Sep 17 00:00:00 2001 From: Gourav Saxena Date: Thu, 9 Sep 2021 12:29:59 +0530 Subject: [PATCH 2/5] - Made field accessible in Java code --- .../com/kaltura/playkit/plugins/smartswitch/SmartSwitchEvent.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchEvent.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchEvent.kt index be04869..cf0398d 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchEvent.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchEvent.kt @@ -21,6 +21,7 @@ open class SmartSwitchEvent(type: Type?): PKEvent { } companion object { + @JvmField var error = ErrorEvent::class.java } } From bb4ee6563180bda63f8e6ec4f737939541d9a41b Mon Sep 17 00:00:00 2001 From: Gourav Saxena Date: Thu, 9 Sep 2021 13:01:08 +0530 Subject: [PATCH 3/5] - Parsing using Serialized name --- .../plugins/smartswitch/SmartSwitchExecutor.kt | 4 ++-- .../plugins/smartswitch/SmartSwitchPlugin.kt | 4 ++-- .../smartswitch/pluginconfig/SmartSwitchParser.kt | 13 +++++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt index 28637b9..ebc72ad 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchExecutor.kt @@ -89,8 +89,8 @@ internal class SmartSwitchExecutor { if (smartSwitchParser?.smartSwitch != null) { cdnList = parseSmartSwitchResponse(smartSwitchParser) if (cdnList != null) { - 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 errorMessage diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt index 54f398c..e729fe1 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt @@ -51,10 +51,10 @@ class SmartSwitchPlugin: PKPlugin(), PKMediaEntryInterceptor { response?.let { res -> when (res) { is CDNList -> { - mediaSource.url = res.URL + mediaSource.url = res.url messageBus?.post(InterceptorEvent.CdnSwitchedEvent( InterceptorEvent.Type.CDN_SWITCHED, - res.CDN_CODE)) + res.cdnCode)) } is String -> { errorMessage = res diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/pluginconfig/SmartSwitchParser.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/pluginconfig/SmartSwitchParser.kt index c6f8b86..6e83464 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/pluginconfig/SmartSwitchParser.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/pluginconfig/SmartSwitchParser.kt @@ -1,6 +1,7 @@ package com.kaltura.playkit.plugins.smartswitch.pluginconfig import androidx.annotation.Nullable +import com.google.gson.annotations.SerializedName class SmartSwitchParser { @Nullable @@ -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 { From adbf79a31d079d265d30fd040ae325372769cd84 Mon Sep 17 00:00:00 2001 From: Gourav Saxena Date: Thu, 9 Sep 2021 13:40:57 +0530 Subject: [PATCH 4/5] - Travis build --- .../com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt index e729fe1..34fd84b 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt @@ -98,6 +98,7 @@ class SmartSwitchPlugin: PKPlugin(), PKMediaEntryInterceptor { } + override fun onDestroy() { smartSwitchExecutor?.terminateService() messageBus?.removeListeners(this) From 18bf28825af4e8eb742194e0aab76a37391a59d0 Mon Sep 17 00:00:00 2001 From: Gourav Saxena Date: Thu, 9 Sep 2021 13:49:29 +0530 Subject: [PATCH 5/5] - Travis build --- .../com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt index 34fd84b..e729fe1 100644 --- a/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt +++ b/smartswitchplugin/src/main/java/com/kaltura/playkit/plugins/smartswitch/SmartSwitchPlugin.kt @@ -98,7 +98,6 @@ class SmartSwitchPlugin: PKPlugin(), PKMediaEntryInterceptor { } - override fun onDestroy() { smartSwitchExecutor?.terminateService() messageBus?.removeListeners(this)