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

io.crossbar.autobahn.wamp.Client Maven / Gradle / Ivy

There is a newer version: 21.7.1
Show newest version
///////////////////////////////////////////////////////////////////////////////
//
//   AutobahnJava - http://crossbar.io/autobahn
//
//   Copyright (c) Crossbar.io Technologies GmbH and contributors
//
//   Licensed under the MIT License.
//   http://www.opensource.org/licenses/mit-license.php
//
///////////////////////////////////////////////////////////////////////////////

package io.crossbar.autobahn.wamp;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;

import io.crossbar.autobahn.utils.ABLogger;
import io.crossbar.autobahn.utils.IABLogger;
import io.crossbar.autobahn.wamp.interfaces.IAuthenticator;
import io.crossbar.autobahn.wamp.interfaces.ITransport;
import io.crossbar.autobahn.wamp.types.ExitInfo;
import io.crossbar.autobahn.wamp.types.TransportOptions;
import io.crossbar.autobahn.wamp.utils.Platform;

public class Client {

    private static final IABLogger LOGGER = ABLogger.getLogger(Client.class.getName());
    private final List mTransports;

    private Session mSession;
    private String mRealm;
    private List mAuthenticators;

    private Executor mExecutor;

    public Client(String webSocketURL) {
        this(Platform.autoSelectTransport(webSocketURL));
    }

    public Client(ITransport transport) {
        mTransports = new ArrayList<>();
        mTransports.add(transport);
    }

    public Client(ITransport transport, Executor executor) {
        this(transport);
        mExecutor = executor;
    }

    public Client(String webSocketURL, Executor executor) {
        this(webSocketURL);
        mExecutor = executor;
    }

    public Client(Session session, String webSocketURL, String realm) {
        this(webSocketURL);
        mSession = session;
        mRealm = realm;
    }

    public Client(Session session, String webSocketURL, String realm, Executor executor) {
        this(webSocketURL);
        mSession = session;
        mRealm = realm;
        mExecutor = executor;
    }

    public Client(Session session, String webSocketURL, String realm,
                  List authenticators) {
        this(webSocketURL);
        mSession = session;
        mRealm = realm;
        mAuthenticators = authenticators;
    }

    public Client(Session session, String webSocketURL, String realm,
                  IAuthenticator authenticator) {
        this(webSocketURL);
        mSession = session;
        mRealm = realm;
        mAuthenticators = new ArrayList<>();
        mAuthenticators.add(authenticator);
    }

    public Client(List transports) {
        mTransports = transports;
    }

    public Client(List transports, Executor executor) {
        this(transports);
        mExecutor = executor;
    }

    private Executor getExecutor() {
        return mExecutor == null ? Platform.autoSelectExecutor(): mExecutor;
    }

    public void add(Session session, String realm, List authenticators) {
        if (mSession != null) {
            throw new IllegalStateException("Addition of multiple sessions not implemented");
        }
        mSession = session;
        mRealm = realm;
        mAuthenticators = authenticators;
    }

    public void add(Session session, String realm) {
        add(session, realm, null);
    }

    public CompletableFuture connect() {
        return connect(new TransportOptions());
    }

    public CompletableFuture connect(TransportOptions options) {
        CompletableFuture exitFuture = new CompletableFuture<>();
        mSession.addOnConnectListener((session) ->
                mSession.join(mRealm, mAuthenticators).thenAccept(details ->
                        LOGGER.i(String.format("JOINED session=%s realm=%s", details.sessionID,
                                details.realm))));
        mSession.addOnDisconnectListener((session, wasClean) ->
                exitFuture.complete(new ExitInfo(wasClean)));
        CompletableFuture.runAsync(() -> {
            try {
                mTransports.get(0).connect(mSession, options);
            } catch (Exception e) {
                throw new CompletionException(e);
            }
        }, getExecutor());
        return exitFuture;
    }

    public void setOptions(TransportOptions options) {
        mTransports.get(0).setOptions(options);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy