Skip to content

Commit

Permalink
refactored presence impl for entering internal members
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Jan 23, 2024
1 parent cf567ec commit 564a96a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,12 @@ private void setAttached(ProtocolMessage message) {
if(state == ChannelState.attached) {
Log.v(TAG, String.format(Locale.ROOT, "Server initiated attach for channel %s", name));
if (!message.hasFlag(Flag.resumed)) { // RTL12
presence.onAttached(message.hasFlag(Flag.has_presence), true);
presence.onAttached(message.hasFlag(Flag.has_presence));
emitUpdate(message.error, false);
}
}
else {
presence.onAttached(message.hasFlag(Flag.has_presence), true);
presence.onAttached(message.hasFlag(Flag.has_presence));
setState(ChannelState.attached, message.error, message.hasFlag(Flag.resumed));
}
}
Expand Down
24 changes: 18 additions & 6 deletions lib/src/main/java/io/ably/lib/realtime/Presence.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,21 @@ public void enterClient(String clientId, Object data, CompletionListener listene
updatePresence(new PresenceMessage(PresenceMessage.Action.enter, clientId, data), listener);
}

private void enterClientWithId(String id, String clientId, Object data, CompletionListener listener) throws AblyException {
if(clientId == null) {
String errorMessage = String.format(Locale.ROOT, "Channel %s: unable to enter presence channel (null clientId specified)", channel.name);
Log.v(TAG, errorMessage);
if(listener != null) {
listener.onError(new ErrorInfo(errorMessage, 40000));
return;
}
}
PresenceMessage presenceMsg = new PresenceMessage(PresenceMessage.Action.enter, clientId, data);
presenceMsg.id = id;
Log.v(TAG, "enterClient(); channel = " + channel.name + "; clientId = " + clientId);
updatePresence(presenceMsg, listener);
}

/**
* Updates the data payload for a presence member using a given clientId.
* Enables a single client to update presence on behalf of any number of clients using a single connection.
Expand Down Expand Up @@ -887,7 +902,7 @@ private void failQueuedMessages(ErrorInfo reason) {
* attach / detach
************************************/

void onAttached(boolean hasPresence, boolean enterInternalPresenceMembers) {
void onAttached(boolean hasPresence) {
/* Interrupt get() call => by unblocking presence.waitForSync()*/
synchronized (presence) {
presence.notifyAll();
Expand All @@ -898,10 +913,7 @@ void onAttached(boolean hasPresence, boolean enterInternalPresenceMembers) {
endSync();
}
sendQueuedMessages(); // RTP5b

if (enterInternalPresenceMembers) { // RTP17f
enterInternalMembers();
}
enterInternalMembers(); // RTP17f
}

/**
Expand All @@ -910,7 +922,7 @@ void onAttached(boolean hasPresence, boolean enterInternalPresenceMembers) {
synchronized void enterInternalMembers() {
for (final PresenceMessage item: internalPresence.values()) {
try {
enterClient(item.clientId, item.data, new CompletionListener() {
enterClientWithId(item.id, item.clientId, item.data, new CompletionListener() {
@Override
public void onSuccess() {
}
Expand Down

0 comments on commit 564a96a

Please sign in to comment.