Skip to content

Commit

Permalink
add timestamp to pending position update.
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Mar 5, 2021
1 parent 5e47f14 commit f4c30bb
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions app/src/main/java/eu/zderadicka/audioserve/net/PositionClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PositionClient(private val serverUrl: String, private val token: String, p

}

val ready: Boolean
private val ready: Boolean
get() = state == ClientState.Ready

private val reopen = Runnable {
Expand Down Expand Up @@ -97,7 +97,7 @@ class PositionClient(private val serverUrl: String, private val token: String, p
socket = client.newWebSocket(req, listener)
}

fun sendPosition(filePath: String?, position: Double) {
fun sendPosition(filePath: String?, position: Double, timeStamp: Long? = null) {
if (!ready) {
pendingPosition = PendingPosition(filePath, position)
handler.post(reopen)
Expand All @@ -109,7 +109,7 @@ class PositionClient(private val serverUrl: String, private val token: String, p
return
}
socket?.apply {
val msg = encodeMessage(filePath, position)
val msg = encodeMessage(filePath, position, timeStamp)
send(msg)
}
}
Expand Down Expand Up @@ -148,15 +148,19 @@ class PositionClient(private val serverUrl: String, private val token: String, p
}
}

private fun encodeMessage(filePath: String, position: Double): String =
private fun encodeMessage(filePath: String, position: Double, timeStamp: Long?): String =
lastFile.let {
fun fmt(p: Double) = "%.3f".format(java.util.Locale.US, p)
return if (it == filePath) {
"${fmt(position)}|"
} else {
lastFile = filePath
val normedPath = mediaIdToPositionPath(filePath, group!!)
"${fmt(position)}|${normedPath}"
var m = "${fmt(position)}|${normedPath}"
if (timeStamp != null) {
m+= "|${timeStamp/1000}"
}
m
}
}

Expand All @@ -176,8 +180,8 @@ class PositionClient(private val serverUrl: String, private val token: String, p

pendingPosition?.apply {
// TODO what will be best expiration time?
if ( (System.currentTimeMillis() - timestamp) < 5*60*1000) { // only send if it is resent
sendPosition(filePath, position)
if ( (System.currentTimeMillis() - timestamp) < 300_000_000) { // only send if it is resent
sendPosition(filePath, position, timestamp)
}
pendingPosition = null
}
Expand Down Expand Up @@ -247,7 +251,7 @@ class PositionClient(private val serverUrl: String, private val token: String, p
inner class PendingQuery(
val pendingReceive: PendingReceive,
val query: String?,
val timeStamp: Long,
private val timeStamp: Long,
var errors: Int = 0
) {
override fun toString(): String {
Expand Down

0 comments on commit f4c30bb

Please sign in to comment.