private void encode(GrowingByteBuffer buf, ProfileServerSync profileSync) { buildExtensionHeader(buf, PROFILE_EXTENSION_ID, NOTHING, (profileSync.getResponseStatus() == SyncResponseStatus.RESYNC ? RESYNC : NOTHING), 0); }
@Override public ClientSync decode(byte[] data) throws PlatformEncDecException { ClientSync sync = super.decode(data); sync.setUseConfigurationRawSchema(true); return sync; } }
private static String getUtf8String(ByteBuffer buf) { return getUtf8String(buf, getIntFromUnsignedShort(buf)); }
private List<EndpointDetachRequest> parseEndpointDetachRequests(ByteBuffer buf) { // reserved buf.get(); int count = getIntFromUnsignedShort(buf); List<EndpointDetachRequest> requests = new ArrayList<EndpointDetachRequest>(count); for (int i = 0; i < count; i++) { int requestId = getIntFromUnsignedShort(buf); // reserved buf.getShort(); requests.add(new EndpointDetachRequest( requestId, Base64Util.encode(getNewByteArray(buf, PUBLIC_KEY_HASH_SIZE)))); } return requests; }
private List<Event> parseEvents(ByteBuffer buf) { int eventsCount = getIntFromUnsignedShort(buf); List<Event> events = new ArrayList<>(eventsCount); for (int i = 0; i < eventsCount; i++) { Event event = new Event(); event.setSeqNum(buf.getInt()); int eventOptions = getIntFromUnsignedShort(buf); int fqnLength = getIntFromUnsignedShort(buf); int dataSize = 0; if (hasOption(eventOptions, CLIENT_EVENT_DATA_IS_PRESENT_OPTION)) { dataSize = buf.getInt(); } if (hasOption(eventOptions, 0x01)) { event.setTarget(Base64Util.encode(getNewByteArray(buf, PUBLIC_KEY_HASH_SIZE))); } event.setEventClassFqn(getUtf8String(buf, fqnLength)); if (dataSize > 0) { event.setEventData(getNewByteBuffer(buf, dataSize)); } else { event.setEventData(EMPTY_BUFFER); } events.add(event); } return events; }
option = 1; buildExtensionHeader(buf, EVENT_EXTENSION_ID, NOTHING, option, 0); final int extPosition = buf.position(); buf.putInt(response.getListeners().size()); for (String listener : response.getListeners()) { put(buf, Base64Util.decode(listener)); putUtf(buf, event.getEventClassFqn()); if (!eventDataIsEmpty) { put(buf, event.getEventData().array());
private void parseClientSyncMetaData(ClientSync sync, ByteBuffer buf, int options, int payloadLength) throws PlatformEncDecException { sync.setRequestId(buf.getInt()); ClientSyncMetaData md = new ClientSyncMetaData(); if (hasOption(options, CLIENT_META_SYNC_TIMEOUT_OPTION)) { md.setTimeout((long) buf.getInt()); } if (hasOption(options, CLIENT_META_SYNC_KEY_HASH_OPTION)) { md.setEndpointPublicKeyHash(getNewByteBuffer(buf, PUBLIC_KEY_HASH_SIZE)); } if (hasOption(options, CLIENT_META_SYNC_PROFILE_HASH_OPTION)) { md.setProfileHash(getNewByteBuffer(buf, PROFILE_HASH_SIZE)); } if (hasOption(options, CLIENT_META_SYNC_SDK_TOKEN_OPTION)) { md.setSdkToken(getUtf8String(buf, Constants.SDK_TOKEN_SIZE)); } sync.setClientSyncMetaData(md); }
private void parseProfileClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { int payloadLimitPosition = buf.position() + payloadLength; ProfileClientSync profileSync = new ProfileClientSync(); profileSync.setProfileBody(getNewByteBuffer(buf, buf.getInt())); while (buf.position() < payloadLimitPosition) { byte fieldId = buf.get(); // reading unused reserved field buf.get(); switch (fieldId) { case PUBLIC_KEY_FIELD_ID: profileSync.setEndpointPublicKey(getNewByteBuffer(buf, getIntFromUnsignedShort(buf))); break; case ACCESS_TOKEN_FIELD_ID: profileSync.setEndpointAccessToken(getUtf8String(buf)); break; default: break; } } sync.setProfileSync(profileSync); }
@Override public ClientSync decode(byte[] data) throws PlatformEncDecException { if (LOG.isTraceEnabled()) { LOG.trace("Decoding binary data {}", Arrays.toString(data)); } ByteBuffer buf = ByteBuffer.wrap(data); if (buf.remaining() < MIN_SIZE_OF_MESSAGE_HEADER) { throw new PlatformEncDecException( MessageFormat.format("Message header is to small {0} to be kaa binary message!", buf.capacity())); } int protocolId = buf.getInt(); if (protocolId != getId()) { throw new PlatformEncDecException( MessageFormat.format("Unknown protocol id {0}!", protocolId)); } int protocolVersion = getIntFromUnsignedShort(buf); if (protocolVersion < MIN_SUPPORTED_VERSION || protocolVersion > MAX_SUPPORTED_VERSION) { throw new PlatformEncDecException( MessageFormat.format("Can't decode data using protocol version {0}!", protocolVersion)); } int extensionsCount = getIntFromUnsignedShort(buf); LOG.trace("received data for protocol id {} and version {} that contain {} extensions", protocolId, protocolVersion, extensionsCount); ClientSync sync = parseExtensions(buf, protocolVersion, extensionsCount); sync.setUseConfigurationRawSchema(false); LOG.trace("Decoded binary data {}", sync); return sync; }
LOG.trace("Encoding server sync {}", sync); GrowingByteBuffer buf = new GrowingByteBuffer(DEFAULT_BUFFER_SIZE); buf.putInt(getId()); buf.putShort(PROTOCOL_VERSION); encodeMetaData(buf, sync); short extensionCount = 1; encode(buf, sync.getBootstrapSync()); extensionCount++; encode(buf, sync.getProfileSync()); extensionCount++; encode(buf, sync.getUserSync()); extensionCount++; encode(buf, sync.getLogSync()); extensionCount++; encode(buf, sync.getConfigurationSync()); extensionCount++; encode(buf, sync.getNotificationSync()); extensionCount++; encode(buf, sync.getEventSync()); extensionCount++;
option |= 0x02; buildExtensionHeader(buf, CONFIGURATION_EXTENSION_ID, NOTHING, (byte) option, 0); final int extPosition = buf.position(); put(buf, confSchemaBody.array()); put(buf, confDeltaBody.array());
private void parseConfigurationClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { ConfigurationClientSync confSync = new ConfigurationClientSync(); if (hasOption(options, CONFIGURATION_HASH_OPTION)) { confSync.setConfigurationHash(getNewByteBuffer(buf, CONFIGURATION_HASH_SIZE)); } if (hasOption(options, CONFIGURATION_RESYNC_OPTION)) { confSync.setResyncOnly(true); } sync.setConfigurationSync(confSync); }
private void parseLogClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { LogClientSync logSync = new LogClientSync(); logSync.setRequestId(getIntFromUnsignedShort(buf)); int size = getIntFromUnsignedShort(buf); List<LogEntry> logs = new ArrayList<>(size); for (int i = 0; i < size; i++) { logs.add(new LogEntry(getNewByteBuffer(buf, buf.getInt()))); } logSync.setLogEntries(logs); sync.setLogSync(logSync); }
private static byte[] getNewByteArray(ByteBuffer buf, int size) { return getNewByteArray(buf, size, true); }
private List<TopicState> parseTopicStates(ByteBuffer buf) { int count = getIntFromUnsignedShort(buf); List<TopicState> topicStates = new ArrayList<TopicState>(count); for (int i = 0; i < count; i++) { long topicId = buf.getLong(); int seqNumber = buf.getInt(); topicStates.add(new TopicState(topicId, seqNumber)); } return topicStates; }
private static ByteBuffer getNewByteBuffer(ByteBuffer buf, int size) { return getNewByteBuffer(buf, size, true); }
private static byte[] getNewByteArray(ByteBuffer buf, int size, boolean withPadding) { byte[] array = new byte[size]; buf.get(array); if (withPadding) { handlePadding(buf, size); } return array; }
private void encode(GrowingByteBuffer buf, NotificationServerSync notificationSync) { buildExtensionHeader(buf, NOTIFICATION_EXTENSION_ID, NOTHING, NOTHING, 0); final int extPosition = buf.position(); buf.put(NOTHING); buf.putShort((short) t.getName().getBytes(UTF8).length); putUtf(buf, t.getName()); long topicId = nf.getTopicId() != null ? nf.getTopicIdAsLong() : 0L; buf.putLong(topicId); putUtf(buf, nf.getUid()); put(buf, nf.getBody().array());
private void encode(GrowingByteBuffer buf, BootstrapServerSync bootstrapSync) { buildExtensionHeader(buf, BOOTSTRAP_EXTENSION_ID, NOTHING, NOTHING, 0); final int extPosition = buf.position(); buf.putShort((short) bootstrapSync.getRequestId()); buf.putShort((short) bootstrapSync.getProtocolList().size()); for (ProtocolConnectionData data : bootstrapSync.getProtocolList()) { buf.putInt(data.getAccessPointId()); buf.putInt(data.getProtocolId()); buf.putShort((short) data.getProtocolVersion()); buf.putShort((short) data.getConnectionData().length); put(buf, data.getConnectionData()); } buf.putInt(extPosition - SIZE_OF_INT, buf.position() - extPosition); }
private static String getUtf8String(ByteBuffer buf, int size) { return new String(getNewByteArray(buf, size), UTF8); }