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

org.yamcs.api.artemis.YamcsConnector Maven / Gradle / Ivy

package org.yamcs.api.artemis;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
import org.apache.activemq.artemis.api.core.client.SessionFailureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.YamcsException;
import org.yamcs.api.YamcsApiException;
import org.yamcs.api.YamcsConnectionProperties;
import org.yamcs.protobuf.YamcsManagement.YamcsInstance;
import org.yamcs.protobuf.YamcsManagement.YamcsInstances;


/**
 * this is like a yamcssession but performs reconnection and implements some callbacks to announce the state.
 * to be used by all the gui data receivers (event viewer, archive browser, etc)
 * @author nm
 *
 */
public class YamcsConnector implements SessionFailureListener {
    public int getMaxAttempts() {
        return maxAttempts;
    }
    public void setMaxAttempts(int maxAttempts) {
        this.maxAttempts = maxAttempts;
    }

    CopyOnWriteArrayList connectionListeners=new CopyOnWriteArrayList();
    volatile boolean connected, connecting;
    protected YamcsSession yamcsSession;
    protected YamcsConnectionProperties connectionParams;
    static Logger log= LoggerFactory.getLogger(YamcsConnector.class);
    private boolean retry = true;
    private boolean reconnecting = false;
    final private ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    int maxAttempts = 10;
    
    public YamcsConnector() {}
    public YamcsConnector(boolean retry) {
        this.retry = retry;
    }


    public void addConnectionListener(ConnectionListener connectionListener) {
        this.connectionListeners.add(connectionListener);
    }

    public List getYamcsInstances() {
        try{
            YamcsSession ys=YamcsSession.newBuilder().setConnectionParams(connectionParams).build();
            YamcsClient mc=ys.newClientBuilder().setRpc(true).build();
            YamcsInstances ainst=(YamcsInstances)mc.executeRpc(Protocol.YAMCS_SERVER_CONTROL_ADDRESS, "getYamcsInstances", null, YamcsInstances.newBuilder());
            mc.close();
            List instances=new ArrayList(ainst.getInstanceCount());
            for(YamcsInstance ai:ainst.getInstanceList()) {
                instances.add(ai.getName());
            }
            ys.close();
            return instances;
        } catch ( ActiveMQException hqe ) {
            // If we don't have permissions, treat as a failed connection
            if( hqe.getType() == ActiveMQExceptionType.SECURITY_EXCEPTION || hqe.getType() == ActiveMQExceptionType.SESSION_CREATION_REJECTED ) {
                String message = "Connection failed with security exception: " + hqe.getMessage();
                log.warn( message );
                if( connected ) {
                    disconnect();
                }
                for(ConnectionListener cl:connectionListeners)
                    cl.connectionFailed(connectionParams.getUrl(), new YamcsException( message ));
            } else {
                // Other errors may not be fatal
                for(ConnectionListener cl:connectionListeners)
                    cl.log("failed to retrieve instances: "+hqe);
            }
        } catch (Exception e) {
            for(ConnectionListener cl:connectionListeners)
                cl.log("failed to retrieve instances: "+e);
        }
        return null;
    }

    public Future connect(YamcsConnectionProperties cp) {
        this.connectionParams=cp;
        return doConnect();
    }

    private FutureTask doConnect() {
        if(connected) {
            disconnect();
        }
        final String url=connectionParams.getUrl();

        FutureTask future=new FutureTask<>(new Runnable() {
            @Override
            public void run() {

                //connect to yamcs
                try {
                    if(reconnecting && !retry) {
                        log.warn("Retries are disabled, cancelling reconnection");
                        reconnecting = false;
                        return;
                    }

                    connecting=true;
                    for(ConnectionListener cl:connectionListeners) {
                        cl.connecting(url);
                    }
                    for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy