All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.remoting3.FutureConnection Maven / Gradle / Ivy

There is a newer version: 5.0.8.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.remoting3;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import javax.security.sasl.SaslClientFactory;

import org.wildfly.security.auth.client.AuthenticationContext;
import org.xnio.FailedIoFuture;
import org.xnio.FutureResult;
import org.xnio.IoFuture;
import org.xnio.OptionMap;

/**
 * @author David M. Lloyd
 */
class FutureConnection {

    private final EndpointImpl endpoint;
    private final URI uri;
    private final AtomicReference> futureConnectionRef = new AtomicReference>();
    private final boolean immediate;
    private final OptionMap options;
    private final AuthenticationContext context;
    private final SaslClientFactory clientFactory;
    private final SocketAddress bindAddress;

    FutureConnection(final EndpointImpl endpoint, final SocketAddress bindAddress, final URI uri, final boolean immediate, final OptionMap options, final AuthenticationContext context, final SaslClientFactory clientFactory) {
        this.endpoint = endpoint;
        this.bindAddress = bindAddress;
        this.uri = uri;
        this.immediate = immediate;
        this.options = options;
        this.context = context;
        this.clientFactory = clientFactory;
    }

    void reconnectAfterDelay() {
        endpoint.getXnioWorker().getIoThread().executeAfter(FutureConnection.this::init, 30L, TimeUnit.SECONDS);
    }

    IoFuture init() {
        return connect(null);
    }

    void splice(FutureResult futureResult, IoFuture realFuture) {
        // always add in this order
        futureResult.addCancelHandler(realFuture);
        realFuture.addNotifier(new IoFuture.HandlingNotifier>() {
            public void handleCancelled(final FutureResult attachment) {
                attachment.setCancelled();
            }

            public void handleFailed(final IOException exception, final FutureResult attachment) {
                attachment.setException(exception);
            }

            public void handleDone(final Connection data, final FutureResult attachment) {
                attachment.setResult(new ManagedConnection(data, FutureConnection.this, futureResult));
            }
        }, futureResult);
    }

    IoFuture connect(FutureResult orig) {
        AtomicReference> futureConnectionRef = this.futureConnectionRef;
        FutureResult oldVal;
        oldVal = futureConnectionRef.get();
        if (oldVal != orig) {
            return oldVal.getIoFuture();
        }
        final FutureResult futureResult = new FutureResult<>();
        while (! futureConnectionRef.compareAndSet(oldVal, futureResult)) {
            oldVal = futureConnectionRef.get();
            if (oldVal != orig) {
                // discard our new one
                return oldVal.getIoFuture();
            }
        }
        IoFuture realFuture;
        try {
            realFuture = endpoint.connect(uri, options, context, clientFactory);
        } catch (IOException e) {
            realFuture = new FailedIoFuture<>(e);
        }
        splice(futureResult, realFuture);
        final IoFuture ioFuture = futureResult.getIoFuture();
        ioFuture.addNotifier(new IoFuture.HandlingNotifier() {
            public void handleCancelled(final FutureConnection attachment) {
                attachment.futureConnectionRef.set(null);
                if (attachment.immediate) {
                    attachment.reconnectAfterDelay();
                }
            }

            public void handleFailed(final IOException exception, final FutureConnection attachment) {
                attachment.futureConnectionRef.set(null);
                if (attachment.immediate) {
                    attachment.reconnectAfterDelay();
                }
            }

            public void handleDone(final Connection connection, final FutureConnection attachment) {
                connection.addCloseHandler((closed, exception) -> {
                    FutureConnection.this.clearRef(futureResult);
                    if (attachment.immediate) {
                        attachment.connect(attachment.futureConnectionRef.get());
                    }
                });
            }
        }, this);
        return ioFuture;
    }

    void clearRef(FutureResult futureResult) {
        futureConnectionRef.compareAndSet(futureResult, null);
    }

    public IoFuture get() {
        return init();
    }

    boolean isConnected() {
        final FutureResult futureResult = futureConnectionRef.get();
        return futureResult != null && futureResult.getIoFuture().getStatus() == IoFuture.Status.DONE;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy