Search in sources :

Example 1 with YamcsConnectionProperties

use of org.yamcs.api.YamcsConnectionProperties in project yamcs-studio by yamcs.

the class YamcsClient method doConnect.

private FutureTask<YamcsConnectionProperties> doConnect() {
    if (connected) {
        disconnect();
    }
    restClient = new RestClient(yprops);
    restClient.setAutoclose(false);
    wsclient = new WebSocketClient(yprops, this);
    wsclient.setUserAgent(application);
    wsclient.enableReconnection(true);
    // Provides compatiblity with old Yamcs instances
    wsclient.enableLegacyURLFallback(true);
    wsclient.setMaxFramePayloadLength(MAX_FRAME_PAYLOAD_LENGTH);
    FutureTask<YamcsConnectionProperties> future = new FutureTask<>(new Runnable() {

        @Override
        public void run() {
            log.info("Connecting to " + yprops);
            int maxAttempts = 10;
            try {
                if (reconnecting && !retry) {
                    log.warning("Retries are disabled, cancelling reconnection");
                    reconnecting = false;
                    return;
                }
                connecting = true;
                connecting();
                for (int i = 0; i < maxAttempts; i++) {
                    try {
                        log.fine(String.format("Connecting to %s attempt %d", yprops, i));
                        instances = restClient.blockingGetYamcsInstances();
                        if (instances == null || instances.isEmpty()) {
                            log.warning("No configured yamcs instance");
                            return;
                        }
                        String defaultInstanceName = instances.get(0).getName();
                        String instanceName = defaultInstanceName;
                        if (yprops.getInstance() != null) {
                            // check if the instance saved in properties exists,
                            // otherwise use the default one
                            instanceName = instances.stream().map(yi -> yi.getName()).filter(s -> s.equals(yprops.getInstance())).findFirst().orElse(defaultInstanceName);
                        }
                        yprops.setInstance(instanceName);
                        ChannelFuture future = wsclient.connect();
                        future.get(5000, TimeUnit.MILLISECONDS);
                        return;
                    } catch (Exception e) {
                        // For anything other than a security exception, re-try
                        if (log.isLoggable(Level.FINEST)) {
                            log.log(Level.FINEST, String.format("Connection to %s failed (attempt %d of %d)", yprops, i + 1, maxAttempts), e);
                        } else {
                            log.warning(String.format("Connection to %s failed (attempt %d of %d)", yprops, i + 1, maxAttempts));
                        }
                        Thread.sleep(5000);
                    }
                }
                connecting = false;
                for (ConnectionListener cl : connectionListeners) {
                    cl.connectionFailed(null, new YamcsException(maxAttempts + " connection attempts failed, giving up."));
                }
                log.warning(maxAttempts + " connection attempts failed, giving up.");
            } catch (InterruptedException e) {
                log.info("Connection cancelled by user");
                connecting = false;
                for (ConnectionListener cl : connectionListeners) {
                    cl.connectionFailed(null, new YamcsException("Thread interrupted", e));
                }
            }
        }
    }, yprops);
    executor.submit(future);
    // Add Progress indicator in status bar
    String jobName = "Connecting to " + yprops;
    scheduleAsJob(jobName, future, Job.SHORT);
    return future;
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompletableFuture(java.util.concurrent.CompletableFuture) WebSocketSubscriptionData(org.yamcs.protobuf.Web.WebSocketServerMessage.WebSocketSubscriptionData) Level(java.util.logging.Level) ConnectionListener(org.yamcs.api.ws.ConnectionListener) Future(java.util.concurrent.Future) YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties) RestClient(org.yamcs.api.rest.RestClient) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) WebSocketReplyData(org.yamcs.protobuf.Web.WebSocketServerMessage.WebSocketReplyData) YamcsPlugin(org.yamcs.studio.core.YamcsPlugin) CancellationException(java.util.concurrent.CancellationException) Job(org.eclipse.core.runtime.jobs.Job) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Status(org.eclipse.core.runtime.Status) HttpMethod(io.netty.handler.codec.http.HttpMethod) Logger(java.util.logging.Logger) YamcsException(org.yamcs.YamcsException) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ChannelFuture(io.netty.channel.ChannelFuture) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) BulkRestDataReceiver(org.yamcs.api.rest.BulkRestDataReceiver) WebSocketClientCallback(org.yamcs.api.ws.WebSocketClientCallback) MessageLite(com.google.protobuf.MessageLite) List(java.util.List) WebSocketRequest(org.yamcs.api.ws.WebSocketRequest) YamcsInstance(org.yamcs.protobuf.YamcsManagement.YamcsInstance) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) WebSocketClient(org.yamcs.api.ws.WebSocketClient) ChannelFuture(io.netty.channel.ChannelFuture) YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties) RestClient(org.yamcs.api.rest.RestClient) ConnectionListener(org.yamcs.api.ws.ConnectionListener) WebSocketClient(org.yamcs.api.ws.WebSocketClient) CancellationException(java.util.concurrent.CancellationException) YamcsException(org.yamcs.YamcsException) ExecutionException(java.util.concurrent.ExecutionException) YamcsException(org.yamcs.YamcsException) FutureTask(java.util.concurrent.FutureTask)

Example 2 with YamcsConnectionProperties

use of org.yamcs.api.YamcsConnectionProperties in project yamcs-studio by yamcs.

the class YamcsAuthorizations method isAuthorizationEnabled.

public boolean isAuthorizationEnabled() {
    YamcsClient yamcsClient = YamcsPlugin.getYamcsClient();
    // TODO we should probably control this from the server, rather than here. Just because
    // the creds are null, does not really mean anything. We could also send creds to an
    // unsecured yamcs server. It would just ignore it, and then our client state would
    // be wrong
    YamcsConnectionProperties yprops = yamcsClient.getYamcsConnectionProperties();
    return (yprops == null) ? false : yprops.getAuthenticationToken() != null;
}
Also used : YamcsClient(org.yamcs.studio.core.client.YamcsClient) YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties)

Example 3 with YamcsConnectionProperties

use of org.yamcs.api.YamcsConnectionProperties in project yamcs-studio by yamcs.

the class AutoConnectHandler method doConnect.

private void doConnect(Shell shell, YamcsConfiguration conf, boolean noPasswordPopup) {
    // FIXME get the password out before doing this
    ConnectionPreferences.setLastUsedConfiguration(conf);
    // Check if authentication is needed
    YamcsConnectionProperties yprops = conf.getPrimaryConnectionProperties();
    if (conf.isAnonymous()) {
        log.fine("Will connect anonymously to " + yprops);
        YamcsPlugin.getYamcsClient().connect(yprops);
    } else if (conf.isSavePassword() || noPasswordPopup) {
        log.fine("Will connect as user '" + conf.getUser() + "' to " + yprops);
        YamcsPlugin.getYamcsClient().connect(yprops);
    } else {
        log.fine("Want to connect to '" + yprops + "' but credentials are needed (not saved and not in dialog). Show password dialog");
        LoginDialog dialog = new LoginDialog(shell, conf);
        if (dialog.open() == Dialog.OK) {
            conf.setUser(dialog.getUser());
            conf.setPassword(dialog.getPassword());
            ConnectionUIHelper.connectWithProgressDialog(shell, yprops);
        }
    }
}
Also used : YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties)

Example 4 with YamcsConnectionProperties

use of org.yamcs.api.YamcsConnectionProperties in project yamcs-studio by yamcs.

the class YamcsConfiguration method getFailoverConnectionProperties.

public YamcsConnectionProperties getFailoverConnectionProperties() {
    if (failoverHost != null) {
        YamcsConnectionProperties yprops = new YamcsConnectionProperties(failoverHost, failoverPort, instance);
        yprops.setProtocol(Protocol.http);
        if (!isAnonymous()) {
            yprops.setAuthenticationToken(new UsernamePasswordToken(user, password));
        }
        return yprops;
    } else {
        return null;
    }
}
Also used : YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties) UsernamePasswordToken(org.yamcs.security.UsernamePasswordToken)

Example 5 with YamcsConnectionProperties

use of org.yamcs.api.YamcsConnectionProperties in project yamcs-studio by yamcs.

the class ConnectionStringStatusLineContributionItem method onYamcsConnected.

@Override
public void onYamcsConnected() {
    YamcsConnectionProperties yprops = YamcsPlugin.getYamcsClient().getYamcsConnectionProperties();
    Display.getDefault().asyncExec(() -> {
        setErrorText(null, null);
        setImage(null);
        setText(getConnectionString(yprops));
    });
}
Also used : YamcsConnectionProperties(org.yamcs.api.YamcsConnectionProperties)

Aggregations

YamcsConnectionProperties (org.yamcs.api.YamcsConnectionProperties)9 UsernamePasswordToken (org.yamcs.security.UsernamePasswordToken)2 YamcsClient (org.yamcs.studio.core.client.YamcsClient)2 MessageLite (com.google.protobuf.MessageLite)1 ChannelFuture (io.netty.channel.ChannelFuture)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 FutureTask (java.util.concurrent.FutureTask)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (java.util.logging.Level)1