Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Present Publisher as offline to Subscribers on loss of connectivity on the Subscriber's side #855

Merged
merged 19 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
130dfa6
Refactor the CoreSubscriber and its Interactor interface to remove so…
QuintinWillison Dec 16, 2022
55752b0
Use memberKey (clientId + connectionId) rather than just clientId for…
QuintinWillison Dec 16, 2022
9198979
Enable Subscribers to support presence messages from multiple Publish…
QuintinWillison Dec 16, 2022
86125f7
Fix bug where we were indicating that a publisher was present, despit…
QuintinWillison Dec 16, 2022
33c52a5
Reduce visibility of utility class.
QuintinWillison Dec 19, 2022
09c8baa
Use isNotEmpty() rather than size for checking collections.
QuintinWillison Dec 19, 2022
aeb1cbc
Further encapsulate the subscriber event flows.
QuintinWillison Dec 19, 2022
90fd871
Add commentary to data class as scope wasn't necessarily clear.
QuintinWillison Dec 19, 2022
b52ecca
Make reference constant.
QuintinWillison Dec 19, 2022
660988d
Move reference from field to local variable (reduce scope of accessib…
QuintinWillison Dec 19, 2022
b1ed03e
Add link to bug issue from TODO commentary.
QuintinWillison Dec 19, 2022
afe49dd
Rename variable for readability.
QuintinWillison Dec 20, 2022
b0cd85e
Remove unused field.
QuintinWillison Dec 20, 2022
5ff01eb
Conform argument name.
QuintinWillison Dec 20, 2022
8ea1a1b
Rename methods for clarity.
QuintinWillison Dec 20, 2022
078d33d
Increase responsibilities of update method to simplify use at upstrea…
QuintinWillison Dec 20, 2022
da21c9b
Simplify nullable Boolean check.
QuintinWillison Dec 20, 2022
fca0789
Reinstate removed tests.
QuintinWillison Dec 21, 2022
88067b8
Fix formatting.
QuintinWillison Dec 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fun io.ably.lib.types.PresenceMessage.toTracking(gson: Gson): PresenceMessage? =
PresenceMessage(
this.action.toTracking(),
presenceData,
this.clientId
this.memberKey(),
)
}

Expand Down
14 changes: 13 additions & 1 deletion common/src/main/java/com/ably/tracking/common/AblyModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ package com.ably.tracking.common

import com.ably.tracking.Resolution

data class PresenceMessage(val action: PresenceAction, val data: PresenceData, val clientId: String)
/**
* Encapsulates the properties of an Ably presence message which are needed by asset tracking SDKs.
*/
data class PresenceMessage(
val action: PresenceAction,
val data: PresenceData,

/**
* Combination of Ably `clientId` and `connectionId`.
* See: https://sdk.ably.com/builds/ably/specification/main/features/#TP3h
*/
val memberKey: String,
)

enum class PresenceAction {
PRESENT_OR_ENTER, LEAVE_OR_ABSENT, UPDATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ data class Trackable(
override fun hashCode(): Int = id.hashCode()
}

data class Subscriber(val id: String, val trackable: Trackable)
data class Subscriber(val memberKey: String, val trackable: Trackable)

sealed class Proximity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ internal interface CorePublisher {
val routingProfile: RoutingProfile
val trackableStateFlows: Map<String, StateFlow<TrackableState>>

fun addSubscriber(id: String, trackable: Trackable, data: PresenceData, properties: PublisherProperties)
fun addSubscriber(memberKey: String, trackable: Trackable, data: PresenceData, properties: PublisherProperties)
fun updateSubscriber(
id: String,
memberKey: String,
trackable: Trackable,
data: PresenceData,
properties: PublisherProperties
)

fun removeSubscriber(id: String, trackable: Trackable, properties: PublisherProperties)
fun removeSubscriber(memberKey: String, trackable: Trackable, properties: PublisherProperties)
fun removeAllSubscribers(trackable: Trackable, properties: PublisherProperties)

fun setDestination(destination: Destination, properties: PublisherProperties)
fun removeCurrentDestination(properties: PublisherProperties)
fun startLocationUpdates(properties: PublisherProperties)
Expand Down Expand Up @@ -492,8 +493,13 @@ constructor(
}
}

override fun addSubscriber(id: String, trackable: Trackable, data: PresenceData, properties: PublisherProperties) {
val subscriber = Subscriber(id, trackable)
override fun addSubscriber(
memberKey: String,
trackable: Trackable,
data: PresenceData,
properties: PublisherProperties
) {
val subscriber = Subscriber(memberKey, trackable)
if (properties.subscribers[trackable.id] == null) {
properties.subscribers[trackable.id] = mutableSetOf()
}
Expand All @@ -504,13 +510,13 @@ constructor(
}

override fun updateSubscriber(
id: String,
memberKey: String,
trackable: Trackable,
data: PresenceData,
properties: PublisherProperties
) {
properties.subscribers[trackable.id]?.let { subscribers ->
subscribers.find { it.id == id }?.let { subscriber ->
subscribers.find { it.memberKey == memberKey }?.let { subscriber ->
data.resolution.let { resolution ->
saveOrRemoveResolutionRequest(resolution, trackable, subscriber, properties)
resolveResolution(trackable, properties)
Expand All @@ -519,9 +525,9 @@ constructor(
}
}

override fun removeSubscriber(id: String, trackable: Trackable, properties: PublisherProperties) {
override fun removeSubscriber(memberKey: String, trackable: Trackable, properties: PublisherProperties) {
properties.subscribers[trackable.id]?.let { subscribers ->
subscribers.find { it.id == id }?.let { subscriber ->
subscribers.find { it.memberKey == memberKey }?.let { subscriber ->
subscribers.remove(subscriber)
properties.requests[trackable.id]?.remove(subscriber)
hooks.subscribers?.onSubscriberRemoved(subscriber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class PresenceMessageWorker(
PresenceAction.PRESENT_OR_ENTER -> {
if (presenceMessage.data.type == ClientTypes.SUBSCRIBER) {
corePublisher.addSubscriber(
presenceMessage.clientId,
presenceMessage.memberKey,
trackable,
presenceMessage.data,
properties
Expand All @@ -28,13 +28,13 @@ internal class PresenceMessageWorker(
}
PresenceAction.LEAVE_OR_ABSENT -> {
if (presenceMessage.data.type == ClientTypes.SUBSCRIBER) {
corePublisher.removeSubscriber(presenceMessage.clientId, trackable, properties)
corePublisher.removeSubscriber(presenceMessage.memberKey, trackable, properties)
}
}
PresenceAction.UPDATE -> {
if (presenceMessage.data.type == ClientTypes.SUBSCRIBER) {
corePublisher.updateSubscriber(
presenceMessage.clientId,
presenceMessage.memberKey,
trackable,
presenceMessage.data,
properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class PresenceMessageWorkerTest {
PresenceMessage(
action,
PresenceData(if (isSubscriber) ClientTypes.SUBSCRIBER else ClientTypes.PUBLISHER),
"test-client-id"
"test-member-key"
),
corePublisher
)
Expand Down
Loading