com.pubnub.api.managers.BasePathManager.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin Show documentation
Show all versions of pubnub-kotlin Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
second!
package com.pubnub.api.managers
import com.pubnub.api.PNConfiguration
import com.pubnub.api.PNConfiguration.Companion.isValid
internal class BasePathManager(private val config: PNConfiguration) {
/**
* for cache busting, the current subdomain number used.
*/
private var currentSubdomain = 1
/**
* if using cache busting, this is the max number of subdomains that are supported.
*/
private val MAX_SUBDOMAIN = 20
/**
* default subdomain used if cache busting is disabled.
*/
private val DEFAULT_SUBDOMAIN = "ps"
/**
* default base path if a custom one is not provided.
*/
private val DEFAULT_BASE_PATH = "pndsn.com"
fun basePath(): String {
val basePathBuilder = StringBuilder("http")
.append(if (config.secure) "s" else "")
.append("://")
when {
config.origin.isValid() -> {
basePathBuilder.append(config.origin)
}
config.cacheBusting -> {
basePathBuilder
.append("ps")
.append(currentSubdomain)
.append(".")
.append(DEFAULT_BASE_PATH)
incrementSubdomain()
}
else -> {
basePathBuilder
.append(DEFAULT_SUBDOMAIN)
.append(".")
.append(DEFAULT_BASE_PATH)
}
}
return basePathBuilder.toString()
}
private fun incrementSubdomain() {
if (currentSubdomain == MAX_SUBDOMAIN) {
currentSubdomain = 1
} else {
currentSubdomain++
}
}
}