You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/11/20 07:43:48 UTC

[GitHub] [ignite] ivandasch commented on a change in pull request #8483: [DRAFT] IGNITE-13496 Java thin: make async API non-blocking with GridNioServer

ivandasch commented on a change in pull request #8483:
URL: https://github.com/apache/ignite/pull/8483#discussion_r527494615



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioServerClientConnectionMultiplexer.java
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.ignite.internal.client.thin.io.gridnioserver;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.failure.FailureType;
+import org.apache.ignite.internal.client.thin.io.ClientConnection;
+import org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer;
+import org.apache.ignite.internal.client.thin.io.ClientMessageDecoder;
+import org.apache.ignite.internal.util.nio.GridNioCodecFilter;
+import org.apache.ignite.internal.util.nio.GridNioFilter;
+import org.apache.ignite.internal.util.nio.GridNioFuture;
+import org.apache.ignite.internal.util.nio.GridNioParser;
+import org.apache.ignite.internal.util.nio.GridNioServer;
+import org.apache.ignite.internal.util.nio.GridNioServerListener;
+import org.apache.ignite.internal.util.nio.GridNioSession;
+import org.apache.ignite.logger.java.JavaLogger;
+import org.jetbrains.annotations.Nullable;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Consumer;
+
+/**
+ * Client connection multiplexer based on {@link org.apache.ignite.internal.util.nio.GridNioServer}.
+ */
+public class GridNioServerClientConnectionMultiplexer implements ClientConnectionMultiplexer {
+    /** */
+    private static final int CLIENT_MODE_PORT = -1;
+
+    /** */
+    private final GridNioServer<ByteBuffer> srv; // TODO: <ByteBuffer> possible?
+
+    public GridNioServerClientConnectionMultiplexer() {
+        IgniteLogger gridLog = new JavaLogger(false);
+
+        ClientMessageDecoder decoder = new ClientMessageDecoder();
+
+        GridNioFilter[] filters;
+
+        GridNioFilter codecFilter = new GridNioCodecFilter(new GridNioParser() {
+            @Override
+            public @Nullable Object decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException {
+                byte[] bytes = decoder.apply(buf);
+
+                return ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder());
+            }
+
+            @Override
+            public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
+                return (ByteBuffer)msg;
+            }
+        }, gridLog, false);
+
+//        if (sslCtx != null) {
+//            GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, true, ByteOrder.nativeOrder(), gridLog);
+//
+//            sslFilter.directMode(false);
+//
+//            filters = new GridNioFilter[]{codecFilter, sslFilter};
+//        }
+//        else
+        filters = new GridNioFilter[]{codecFilter};
+
+        try {
+            srv = GridNioServer.<ByteBuffer>builder()
+                    .address(InetAddress.getLoopbackAddress()) // TODO: Remove?
+                    .port(CLIENT_MODE_PORT)
+                    .listener(new GridNioServerListener<ByteBuffer>() {
+                        @Override
+                        public void onConnected(GridNioSession ses) {
+                            System.out.println("Connect");
+                        }
+
+                        @Override
+                        public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
+                            System.out.println("Disconnect");
+                        }
+
+                        @Override
+                        public void onMessageSent(GridNioSession ses, ByteBuffer msg) {
+
+                        }
+
+                        @Override
+                        public void onMessage(GridNioSession ses, ByteBuffer msg) {
+                            GridNioServerClientConnection conn = ses.meta(GridNioServerClientConnection.SES_META_CONN);
+
+                            conn.onMessage(msg);
+                        }
+
+                        @Override
+                        public void onSessionWriteTimeout(GridNioSession ses) {
+
+                        }
+
+                        @Override
+                        public void onSessionIdleTimeout(GridNioSession ses) {
+
+                        }
+
+                        @Override
+                        public void onFailure(FailureType failureType, Throwable failure) {
+                            System.out.println("Fail");
+                        }
+                    })
+                    .filters(filters)
+                    .logger(gridLog)
+                    .selectorCount(1) // TODO: Get from settings

Review comment:
       To be honest, there is no need to create more than one selector.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org