Search in sources :

Example 1 with RestClient

use of org.yamcs.api.rest.RestClient 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)

Aggregations

MessageLite (com.google.protobuf.MessageLite)1 ChannelFuture (io.netty.channel.ChannelFuture)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)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 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Status (org.eclipse.core.runtime.Status)1