Skip to content

Commit

Permalink
Added explicit method for creating a recovery key
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Nov 28, 2023
1 parent 1897227 commit 180d68f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/src/main/java/io/ably/lib/realtime/AblyRealtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,25 @@ public void transferToChannels(List<ConnectionManager.QueuedMessage> queuedMessa
private void clear() {
map.clear();
}
}

protected void setChannelSerialsFromRecoverOption(HashMap<String, String> serials) {
for (Map.Entry<String, String> entry : serials.entrySet()) {
String channelName = entry.getKey();
String channelSerial = entry.getValue();
Channel channel = this.get(channelName);
if (channel != null) {
channel.properties.channelSerial = channelSerial;
}
protected void setChannelSerialsFromRecoverOption(HashMap<String, String> serials) {
for (Map.Entry<String, String> entry : serials.entrySet()) {
String channelName = entry.getKey();
String channelSerial = entry.getValue();
Channel channel = this.channels.get(channelName);
if (channel != null) {
channel.properties.channelSerial = channelSerial;
}
}
}

protected HashMap<String, String> getChannelSerials() {
HashMap<String, String> channelSerials = new HashMap<>();
for (Channel channel : this.values()) {
channelSerials.put(channel.name, channel.properties.channelSerial);
}
return channelSerials;
protected HashMap<String, String> getChannelSerials() {
HashMap<String, String> channelSerials = new HashMap<>();
for (Channel channel : this.channels.values()) {
channelSerials.put(channel.name, channel.properties.channelSerial);
}

return channelSerials;
}

/********************
Expand Down
23 changes: 23 additions & 0 deletions lib/src/main/java/io/ably/lib/realtime/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.ably.lib.transport.ConnectionManager;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.ErrorInfo;
import io.ably.lib.types.RecoveryKeyContext;
import io.ably.lib.util.EventEmitter;
import io.ably.lib.util.Log;
import io.ably.lib.util.PlatformAgentProvider;
Expand Down Expand Up @@ -54,6 +55,28 @@ public class Connection extends EventEmitter<ConnectionEvent, ConnectionStateLis
@Deprecated
public String recoveryKey;

/**
* Spec: RTN16g
*
* @return a json string which incorporates the @connectionKey@, the current @msgSerial@,
* and a collection of pairs of channel @name@ and current @channelSerial@ for every currently attached channel.
*/
public String createRecoveryKey() {
if (key == null || key.isEmpty() || this.state == ConnectionState.closing ||
this.state == ConnectionState.closed ||
this.state == ConnectionState.failed ||
this.state == ConnectionState.suspended
) {
//RTN16h
return null;
}

RecoveryKeyContext recoveryKey = new RecoveryKeyContext(key, serial);
recoveryKey.setChannelSerials(this.ably.getChannelSerials());

return recoveryKey.encode();
}

/**
* A unique public identifier for this connection, used to identify this member.
* <p>
Expand Down

0 comments on commit 180d68f

Please sign in to comment.