SammyVimes commented on a change in pull request #102:
URL: https://github.com/apache/ignite-3/pull/102#discussion_r620098413



##########
File path: 
modules/network/src/main/java/org/apache/ignite/network/scalecube/ScaleCubeDirectMarshallerTransport.java
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.network.scalecube;
+
+import io.scalecube.cluster.transport.api.Message;
+import io.scalecube.cluster.transport.api.Transport;
+import io.scalecube.net.Address;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.Map;
+import java.util.Objects;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.network.internal.netty.ConnectionManager;
+import org.apache.ignite.network.message.NetworkMessage;
+import org.apache.ignite.network.scalecube.message.ScaleCubeMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import reactor.core.Disposable;
+import reactor.core.Disposables;
+import reactor.core.publisher.DirectProcessor;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.FluxSink;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.MonoProcessor;
+
+/**
+ * ScaleCube transport over {@link ConnectionManager}.
+ */
+public class ScaleCubeDirectMarshallerTransport implements Transport {
+    /** Logger. */
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(Transport.class);
+
+    /** Message subject. */
+    private final DirectProcessor<Message> subject = DirectProcessor.create();
+
+    /** Message sink. */
+    private final FluxSink<Message> sink = subject.sink();
+
+    /** Close handler */
+    private final MonoProcessor<Void> stop = MonoProcessor.create();
+
+    /** On stop. */
+    private final MonoProcessor<Void> onStop = MonoProcessor.create();
+
+    /** Connection manager. */
+    private final ConnectionManager connectionManager;
+
+    /** Node address. */
+    private final Address address;
+
+    /**
+     * Constructor.
+     *
+     * @param connectionManager Connection manager.
+     */
+    public ScaleCubeDirectMarshallerTransport(ConnectionManager 
connectionManager) {
+        this.connectionManager = connectionManager;
+        this.connectionManager.addListener(this::onMessage);
+        this.address = prepareAddress(connectionManager.getLocalAddress());
+        // Setup cleanup
+        stop.then(doStop())
+            .doFinally(s -> onStop.onComplete())
+            .subscribe(
+                null,
+                ex -> LOGGER.warn("[{}][doStop] Exception occurred: {}", 
address, ex.toString())
+            );
+    }
+
+    /**
+     * Convert {@link InetSocketAddress} to {@link Address}.
+     *
+     * @param addr Address.
+     * @return ScaleCube address.
+     */
+    private static Address prepareAddress(InetSocketAddress addr) {
+        InetAddress address = addr.getAddress();
+
+        int port = addr.getPort();
+
+        if (address.isAnyLocalAddress())
+            return 
Address.create(Address.getLocalIpAddress().getHostAddress(), port);
+        else
+            return Address.create(address.getHostAddress(), port);
+    }
+
+    /**
+     * Cleanup resources on stop.
+     *
+     * @return A mono, that resolves when the stop operation is finished.
+     */
+    private Mono<Void> doStop() {
+        return Mono.defer(() -> {
+            LOGGER.info("[{}][doStop] Stopping", address);
+
+            // Complete incoming messages observable
+            sink.complete();
+
+            LOGGER.info("[{}][doStop] Stopped", address);
+            return Mono.empty();
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override public Address address() {
+        return address;
+    }
+
+    /** {@inheritDoc} */
+    @Override public Mono<Transport> start() {
+        return Mono.just(this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Mono<Void> stop() {
+        return Mono.defer(() -> {
+            stop.onComplete();
+            return onStop;
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isStopped() {
+        return onStop.isDisposed();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Mono<Void> send(Address address, Message message) {
+        return Mono.defer(() -> {

Review comment:
       Returnin `null` in either `map` or `flatMap` counts like an error signal




-- 
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:
[email protected]


Reply via email to