This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
31 changed files
with
328 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/dluvian/nozzle/data/provider/IOnlineStatusProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.dluvian.nozzle.data.provider | ||
|
||
import com.dluvian.nozzle.model.OnlineStatus | ||
import com.dluvian.nozzle.model.Relay | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface IOnlineStatusProvider { | ||
fun getOnlineStatuses(relays: Collection<Relay>): Flow<Map<Relay, OnlineStatus>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/dluvian/nozzle/data/provider/impl/OnlineStatusProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dluvian.nozzle.data.provider.impl | ||
|
||
import com.dluvian.nozzle.data.PING_INTERVAL | ||
import com.dluvian.nozzle.data.PING_WAIT_TIME | ||
import com.dluvian.nozzle.data.provider.IOnlineStatusProvider | ||
import com.dluvian.nozzle.model.Offline | ||
import com.dluvian.nozzle.model.Online | ||
import com.dluvian.nozzle.model.OnlineStatus | ||
import com.dluvian.nozzle.model.Relay | ||
import com.dluvian.nozzle.model.Waiting | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.launch | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import java.util.Collections | ||
|
||
class OnlineStatusProvider(private val httpClient: OkHttpClient) : IOnlineStatusProvider { | ||
private val scope = CoroutineScope(Dispatchers.IO) | ||
private val cache = Collections.synchronizedMap(mutableMapOf<Relay, OnlineStatus>()) | ||
override fun getOnlineStatuses(relays: Collection<Relay>): Flow<Map<Relay, OnlineStatus>> { | ||
if (relays.isEmpty()) return flowOf(emptyMap()) | ||
val distinctRelays = relays.toSet() | ||
return flow { | ||
ping(relays = distinctRelays) | ||
var lastPing = System.currentTimeMillis() | ||
|
||
while (true) { | ||
delay(PING_WAIT_TIME) | ||
emit(distinctRelays.associateWith { cache[it] ?: Waiting }) | ||
val currentTime = System.currentTimeMillis() | ||
if (currentTime - lastPing > PING_INTERVAL) { | ||
lastPing = currentTime | ||
ping(relays = distinctRelays) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun ping(relays: Set<Relay>) { | ||
relays.forEach { relay -> | ||
val request = Request.Builder() | ||
.url(relay) | ||
.build() | ||
scope.launch { | ||
cache[relay] = try { | ||
httpClient.newCall(request).execute().use { response -> | ||
val ping = response.receivedResponseAtMillis - response.sentRequestAtMillis | ||
Online(ping = ping) | ||
} | ||
} catch (e: Exception) { | ||
Offline | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
app/src/main/java/com/dluvian/nozzle/data/room/entity/Nip65Entity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package com.dluvian.nozzle.data.room.entity | ||
|
||
import androidx.room.Embedded | ||
import androidx.room.Entity | ||
import com.dluvian.nozzle.data.room.helper.Nip65Relay | ||
|
||
@Entity(tableName = "nip65", primaryKeys = ["pubkey", "url"]) | ||
data class Nip65Entity( | ||
val pubkey: String, | ||
val url: String, | ||
val isRead: Boolean, | ||
val isWrite: Boolean, | ||
@Embedded val nip65Relay: Nip65Relay, | ||
val createdAt: Long, | ||
) |
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/dluvian/nozzle/data/room/helper/Nip65Relay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.dluvian.nozzle.data.room.helper | ||
|
||
import com.dluvian.nozzle.model.Relay | ||
|
||
data class Nip65Relay( | ||
val url: String, | ||
val url: Relay, | ||
val isRead: Boolean, | ||
val isWrite: Boolean, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/dluvian/nozzle/model/ItemWithOnlineStatus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.dluvian.nozzle.model | ||
|
||
data class ItemWithOnlineStatus<T>(val item: T, val onlineStatus: OnlineStatus) |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/dluvian/nozzle/model/OnlineStatus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.dluvian.nozzle.model | ||
|
||
import androidx.compose.runtime.Immutable | ||
|
||
@Immutable | ||
sealed class OnlineStatus | ||
|
||
data class Online(val ping: Long) : OnlineStatus() | ||
data object Offline : OnlineStatus() | ||
data object Waiting : OnlineStatus() |
2 changes: 1 addition & 1 deletion
2
...dluvian/nozzle/model/CountedRelayUsage.kt → ...n/nozzle/model/relay/CountedRelayUsage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.dluvian.nozzle.model | ||
package com.dluvian.nozzle.model.relay | ||
|
||
data class CountedRelayUsage(val pubkey: String, val relayUrl: String, val numOfPosts: Int) |
2 changes: 1 addition & 1 deletion
2
...a/com/dluvian/nozzle/model/IdAndRelays.kt → ...dluvian/nozzle/model/relay/IdAndRelays.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.dluvian.nozzle.model | ||
package com.dluvian.nozzle.model.relay | ||
|
||
data class IdAndRelays(val id: String, val relays: List<String>) |
4 changes: 3 additions & 1 deletion
4
...om/dluvian/nozzle/model/RelaySelection.kt → ...vian/nozzle/model/relay/RelaySelection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.