The Robot Application Programming Interface Delegate Project
RAPID Logo

RapidExampleApplication.java

An Eclipse application (implements IApplication) that is an example of a non-GUI RAPID application.

/*
 * Copyright (c) 2009 United States Government as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All Rights Reserved.
 */

package gov.nasa.rapid.examples;

import gov.nasa.rapid.dds.Agent;
import gov.nasa.rapid.examples.publisher.RapidExampleEventPublisher;
import gov.nasa.rapid.messaging.dds.DDSMiddlewarePlugin;

import org.apache.log4j.Logger;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class RapidExampleApplication implements IApplication {

    final Logger         log              = Logger.getLogger(RapidExampleApplication.class);

    public static String standAloneOption = "-standAlone";

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
     */
    @Override
    public Object start(IApplicationContext context) {

        String args[] = (String[]) context.getArguments().get("application.args");
        if (args.length < 1) {
            log.error("[Expected usage]: RapidExample [ClientAgentName] <<-standAlone> <BridgeAgentName>> <-sequencer:[SequencerAgentName]>");
            log.error("Options:");
            log.error("     [ClientAgentName]: name of approved client agents - REQUIRED");
            log.error("     <-standAlone>: ability to run RapidExample with the Client and Bridge in the same GUI App - OPTIONAL");
            log.error("     <-bridge:[BridgeAgentName]>: if -standAlone, option to name the Bridge from approved agent names, "
                    + "defaults to GenericSim if no input entered - OPTIONAL");
            log.error("     <-sequencer:[SequencerAgentName]>: name of Sequencer from approved agent names, "
                    + "defaults to NULL if none entered; commands go directly to Bridge - OPTIONAL");
            log.error("List of approved names: ");
            for (Agent a : Agent.values()) {
                log.error(a);
            }
            log.error("Please use one of these values.");
            return EXIT_OK;
        }
        Agent agent = null;
        Agent bridgeAgent = Agent.GenericSim;
//        Agent sequencerAgent = null;

        // get the default robot's name
        if (args.length >= 1) {
            // test input
            agent = Agent.valueOf(args[0]);

            if (agent == null) {
                log.error("[Argument error] The robot name is not valid: " + args[0]);
                log.error("[Argument error] Default name must be a valid robot name: ");
                log.error("[Argument error] List of approved names: ");
                for (Agent a : Agent.values()) {
                    log.error(a);
                }
                log.error("[Argument error] Please use one of these values.");
                return EXIT_OK;
            }

            DDSExampleSettings.CLIENT_AGENT = agent;

            if (args.length >= 2) {
                if (args[1].equals(standAloneOption)) {
                    DDSExampleSettings.STAND_ALONE_APPLICATION = true;

                    // third option should be Bridge Agent name or nothing for Agent.GenericSim default
                    if (args.length == 3) {
//                        if (args[3].startsWith("-bridge:")) {
                        String bridgeName = args[2];
                        bridgeAgent = Agent.valueOf(bridgeName);
//                        } else if (args[3].startsWith("-sequencer:")) {
//                            String sequencerName = args[3].substring(args[3].indexOf(':'));
//                            sequencerAgent = Agent.getAgentFromName(sequencerName);
//                            DDSExampleSettings.SEQUENCER_AGENT = sequencerAgent;
//                        }
                    }

                    // separate out the bridge
                    DDSExampleSettings.PUBLISHER_AGENT = bridgeAgent;
                    RapidExampleEventPublisher publisher = new RapidExampleEventPublisher(bridgeAgent);
                    publisher.startBridge();
                    return publisher;

//                } else if (args[1].startsWith("-sequencer:")) {
//                    String sequencerName = args[1].substring(args[1].indexOf(':'));
//                    sequencerAgent = Agent.getAgentFromName(sequencerName);
//                    DDSExampleSettings.SEQUENCER_AGENT = sequencerAgent;
                }
            }
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.equinox.app.IApplication#stop()
     */
    @Override
    public void stop() {
        DDSMiddlewarePlugin.cleanup();
    }
}

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines