greyp9 commented on code in PR #6040:
URL: https://github.com/apache/nifi/pull/6040#discussion_r876084246


##########
nifi-nar-bundles/nifi-standard-services/nifi-distributed-cache-services-bundle/nifi-distributed-cache-server/src/main/java/org/apache/nifi/distributed/cache/server/codec/CacheRequestDecoder.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.distributed.cache.server.codec;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import org.apache.nifi.distributed.cache.operations.CacheOperation;
+import org.apache.nifi.distributed.cache.operations.StandardCacheOperation;
+import org.apache.nifi.distributed.cache.server.protocol.CacheRequest;
+import org.apache.nifi.distributed.cache.server.protocol.CacheVersionRequest;
+import org.apache.nifi.logging.ComponentLog;
+
+import java.net.SocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.OptionalInt;
+import java.util.OptionalLong;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Cache Request Decoder processes bytes and decodes cache version and 
operation requests
+ */
+public class CacheRequestDecoder extends ByteToMessageDecoder {
+    private static final int HEADER_LENGTH = 4;
+
+    private static final int LONG_LENGTH = 8;
+
+    private static final int INT_LENGTH = 4;
+
+    private static final int SHORT_LENGTH = 2;
+
+    private final AtomicBoolean headerReceived = new AtomicBoolean();
+
+    private final AtomicInteger protocolVersion = new AtomicInteger();
+
+    private final ComponentLog log;
+
+    private final int maxLength;
+
+    private final CacheOperation[] supportedOperations;
+
+    public CacheRequestDecoder(
+            final ComponentLog log,
+            final int maxLength,
+            final CacheOperation[] supportedOperations
+    ) {
+        this.log = log;
+        this.maxLength = maxLength;
+        this.supportedOperations = supportedOperations;
+    }
+
+    /**
+     * Decode Byte Buffer reading header on initial connection followed by 
protocol version and cache operations
+     *
+     * @param channelHandlerContext Channel Handler Context
+     * @param byteBuf Byte Buffer
+     * @param objects Decoded Objects
+     */
+    @Override
+    protected void decode(final ChannelHandlerContext channelHandlerContext, 
final ByteBuf byteBuf, final List<Object> objects) {
+        if (!headerReceived.get()) {
+            readHeader(byteBuf, 
channelHandlerContext.channel().remoteAddress());
+        }
+
+        if (protocolVersion.get() == 0) {
+            final OptionalInt clientVersion = readInt(byteBuf);
+            if (clientVersion.isPresent()) {
+                final int clientVersionFound = clientVersion.getAsInt();
+                log.debug("Protocol Version [{}] Received [{}]", 
clientVersionFound, channelHandlerContext.channel().remoteAddress());
+                final CacheVersionRequest cacheVersionRequest = new 
CacheVersionRequest(clientVersionFound);
+                objects.add(cacheVersionRequest);
+            }
+        } else {
+            // Mark ByteBuf reader index to reset when sufficient bytes are 
not found
+            byteBuf.markReaderIndex();

Review Comment:
   +1



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to