-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathVoipPerCallDdpRegistry.kt
More file actions
50 lines (41 loc) · 1.48 KB
/
VoipPerCallDdpRegistry.kt
File metadata and controls
50 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package chat.rocket.reactnative.voip
/**
* Per-call DDP client slots: each [callId] maps to at most one [DDPClient] in production.
* Isolates teardown so a busy-call reject (call B) does not disconnect call A's listener.
*/
internal class VoipPerCallDdpRegistry<T : Any>(
private val releaseClient: (T) -> Unit
) {
private val lock = Any()
private val clients = mutableMapOf<String, T>()
private val loggedInCallIds = mutableSetOf<String>()
fun clientFor(callId: String): T? = synchronized(lock) { clients[callId] }
fun isLoggedIn(callId: String): Boolean = synchronized(lock) { loggedInCallIds.contains(callId) }
fun putClient(callId: String, client: T) {
synchronized(lock) {
clients.remove(callId)?.let(releaseClient)
clients[callId] = client
loggedInCallIds.remove(callId)
}
}
fun markLoggedIn(callId: String) {
synchronized(lock) {
loggedInCallIds.add(callId)
}
}
fun stopClient(callId: String) {
synchronized(lock) {
loggedInCallIds.remove(callId)
clients.remove(callId)?.let(releaseClient)
}
}
fun stopAllClients() {
synchronized(lock) {
loggedInCallIds.clear()
clients.values.forEach(releaseClient)
clients.clear()
}
}
fun clientCount(): Int = synchronized(lock) { clients.size }
fun clientIds(): Set<String> = synchronized(lock) { clients.keys.toSet() }
}