2021-10-27 16:15:59 -04:00
|
|
|
package io.heckel.ntfy.data
|
|
|
|
|
|
|
|
|
|
import com.google.gson.GsonBuilder
|
|
|
|
|
import com.google.gson.JsonObject
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
import java.net.HttpURLConnection
|
|
|
|
|
import java.net.URL
|
|
|
|
|
|
|
|
|
|
const val READ_TIMEOUT = 60_000 // Keep alive every 30s assumed
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
class ConnectionManager(private val repository: Repository) {
|
2021-10-27 16:15:59 -04:00
|
|
|
private val jobs = mutableMapOf<Long, Job>()
|
|
|
|
|
private val gson = GsonBuilder().create()
|
2021-10-27 19:50:44 -04:00
|
|
|
private var listener: NotificationListener? = null;
|
2021-10-27 16:15:59 -04:00
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
fun start(s: Subscription) {
|
|
|
|
|
jobs[s.id] = launchConnection(s.id, topicJsonUrl(s))
|
2021-10-27 16:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
fun stop(s: Subscription) {
|
|
|
|
|
jobs.remove(s.id)?.cancel() // Cancel coroutine and remove
|
2021-10-27 16:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
fun setListener(l: NotificationListener) {
|
|
|
|
|
this.listener = l
|
2021-10-27 16:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
private fun launchConnection(subscriptionId: Long, topicUrl: String): Job {
|
|
|
|
|
return GlobalScope.launch(Dispatchers.IO) {
|
2021-10-27 16:15:59 -04:00
|
|
|
while (isActive) {
|
2021-10-27 19:50:44 -04:00
|
|
|
openConnection(subscriptionId, topicUrl)
|
2021-10-27 16:15:59 -04:00
|
|
|
delay(5000) // TODO exponential back-off
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
private fun openConnection(subscriptionId: Long, topicUrl: String) {
|
|
|
|
|
println("Connecting to $topicUrl ...")
|
|
|
|
|
val conn = (URL(topicUrl).openConnection() as HttpURLConnection).also {
|
2021-10-27 16:15:59 -04:00
|
|
|
it.doInput = true
|
|
|
|
|
it.readTimeout = READ_TIMEOUT
|
|
|
|
|
}
|
|
|
|
|
try {
|
2021-10-27 19:50:44 -04:00
|
|
|
updateStatus(subscriptionId, Status.CONNECTED)
|
2021-10-27 16:15:59 -04:00
|
|
|
val input = conn.inputStream.bufferedReader()
|
2021-10-27 19:50:44 -04:00
|
|
|
while (GlobalScope.isActive) {
|
2021-10-27 16:15:59 -04:00
|
|
|
val line = input.readLine() ?: break // Break if EOF is reached, i.e. readLine is null
|
2021-10-27 19:50:44 -04:00
|
|
|
if (!GlobalScope.isActive) {
|
2021-10-27 16:15:59 -04:00
|
|
|
break // Break if scope is not active anymore; readLine blocks for a while, so we want to be sure
|
|
|
|
|
}
|
2021-10-27 19:50:44 -04:00
|
|
|
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Break on unexpected line
|
|
|
|
|
val validNotification = !json.isJsonNull
|
|
|
|
|
&& !json.has("event") // No keepalive or open messages
|
|
|
|
|
&& json.has("message")
|
|
|
|
|
if (validNotification) {
|
|
|
|
|
notify(subscriptionId, json.get("message").asString)
|
2021-10-27 16:15:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-27 19:50:44 -04:00
|
|
|
} catch (e: Exception) {
|
|
|
|
|
println("Connection error: " + e)
|
2021-10-27 16:15:59 -04:00
|
|
|
} finally {
|
|
|
|
|
conn.disconnect()
|
|
|
|
|
}
|
2021-10-28 11:45:34 -04:00
|
|
|
updateStatus(subscriptionId, Status.RECONNECTING)
|
2021-10-27 19:50:44 -04:00
|
|
|
println("Connection terminated: $topicUrl")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun updateStatus(subscriptionId: Long, status: Status) {
|
|
|
|
|
val subscription = repository.get(subscriptionId)
|
|
|
|
|
repository.update(subscription?.copy(status = status))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun notify(subscriptionId: Long, message: String) {
|
|
|
|
|
val subscription = repository.get(subscriptionId)
|
|
|
|
|
if (subscription != null) {
|
|
|
|
|
listener?.let { it(Notification(subscription, message)) }
|
|
|
|
|
repository.update(subscription.copy(messages = subscription.messages + 1))
|
|
|
|
|
}
|
2021-10-27 16:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private var instance: ConnectionManager? = null
|
|
|
|
|
|
2021-10-27 19:50:44 -04:00
|
|
|
fun getInstance(repository: Repository): ConnectionManager {
|
2021-10-27 16:15:59 -04:00
|
|
|
return synchronized(ConnectionManager::class) {
|
2021-10-27 19:50:44 -04:00
|
|
|
val newInstance = instance ?: ConnectionManager(repository)
|
2021-10-27 16:15:59 -04:00
|
|
|
instance = newInstance
|
|
|
|
|
newInstance
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|