Class AccessorSSHCodeGenerator

  • All Implemented Interfaces:
    java.lang.Cloneable, Changeable, Debuggable, DebugListener, Decorator, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

    public class AccessorSSHCodeGenerator
    extends AccessorCodeGenerator
    Generate a JavaScript composite accessor for a model and deploy it to a remote host.

    Accessors are a technology, developed by the TerraSwarm Research Center, for composing heterogeneous devices and services in the Internet of Things (IoT). For more information, see http://accessors.org.

    The model can only contain JavaScript and JSAccessor actors.

    To generate an Accessor version of a model, use:

      java -classpath $PTII ptolemy.cg.kernel.generic.accessor.AccessorSSHCodeGenerator -language accessor $PTII/ptolemy/cg/kernel/generic/accessor/demo/TestComposite/TestComposite.xml; cat $PTII/org/terraswarm/accessor/accessors/web/hosts/node/TestComposite.js
      
    which is shorthand for:
      java -classpath $PTII ptolemy.cg.kernel.generic.accessor.AccessorSSHCodeGenerator -generatorPackage ptolemy.cg.kernel.generic.accessor -generatorPackageList generic.accessor $PTII/ptolemy/cg/adapter/generic/accessor/adapters/org/test/auto/TestComposite.xml; cat ~/cg/TestComposite.js
      
    To use Cape Code, invoke:
      $PTII/bin/vergil -capecode $PTII/ptolemy/cg/kernel/generic/accessor/test/auto/RampJSTextDisplay.xml
      

    This actor runs $PTII/ptolemy/cg/kernel/generic/accessor/accessorInvokeSSH, which does the following on the remote machine:

    1. Creates a directory in ~/cg/ModelName, for example ~/cg/MyCompositeAccessor
    2. Installs the @terraswarm/accessors and pm2 modules in ~/cg/ModelName. Note that this means that to run a composite accessor with the latest accessors, the npm @terraswarm/accessors module must be updated. See NPM Upload in the accessors Wiki. In addition, any modules listed in the comma-separated modules parameter are also installed.
    3. Creates a small node script called invoke.js to run the composite accessor.
    4. Copies the composite accessor to the directory.
    5. Creates a script called runit that uses pm2 to stop any processes with the same name as the accessor started with forever name and then invokes node on the remote machine using pm2. If the director of the model has a stopTime parameter, then the value is multiplied by 1000 and used as the value of the timeout parameter on the remote machine. If the stopTime parameter is not set, then a default value (currently 15000 ms.) is used.
    6. The stderr and stdout are then reported using tail

    The accessorInvokeSSH script should work on any machine that has node and npm installed.

    pm2 is a Node package installed using npm that can run a process forever and can cause it to be started upon reboot. Note that for the process to be started after reboot, a command needs to be run once as root on the host machine. The command is displayed during code generation. The command is specific to the user account on the remote machine.

    To use a SwarmBox, add your ~/.ssh/id_rsa.pub file to swarmboxadmin/ansible/keys/sbuser_authorized_keys. See https://www.terraswarm.org/testbeds/wiki/Main/SbuserSSHAccess.

    For more information, see the Code Generation wiki.

    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Christopher Brooks. Based on HTMLCodeGenerator by Man-Kit Leung, Bert Rodiers
    Pt.AcceptedRating:
    red (cxh)
    Pt.ProposedRating:
    red (cxh)
    • Field Detail

      • runForever

        public Parameter runForever
        If true, then use npm forever to run the Node composite accessor forever. If false, then run until stopTime ms. has passed. Note that if true, then the process will be restarted after stopTime ms. or until the code generator is run with stopForeverAccessors is true or the remote machine is rebooted. The default value is true, indicating that the process should be run forever.
      • stopForeverAccessors

        public Parameter stopForeverAccessors
        If true, then connect to the remote machine and stop any npm forever processes with the same basename as the model. Confusingly, to stop any remote processes that have been previously invoked, run should be true so that the accessorInvokeSSH script is invoked on the remote machine with a stop argument. The default value is false, indicating that the remote machine will invoke the Node accessor host composite accessor.
    • Constructor Detail

      • AccessorSSHCodeGenerator

        public AccessorSSHCodeGenerator​(NamedObj container,
                                        java.lang.String name)
                                 throws IllegalActionException,
                                        NameDuplicationException
        Create a new instance of the AccessorSSHCodeGenerator. The value of the generatorPackageList parameter of the base class is set to generic.accessor
        Parameters:
        container - The container.
        name - The name of the AccessorSSHCodeGenerator.
        Throws:
        IllegalActionException - If the super class throws the exception or error occurs when setting the file path.
        NameDuplicationException - If the super class throws the exception or an error occurs when setting the file path.
    • Method Detail

      • _setupCommands

        protected java.util.List<java.lang.String> _setupCommands()
                                                           throws IllegalActionException
        Return a list of setup commands to be invoked before the run command. In this class, any commands that start with "npm install" are removed.
        Overrides:
        _setupCommands in class AccessorCodeGenerator
        Returns:
        return The list of commands.
        Throws:
        IllegalActionException - If thrown in a base class.
      • _updateSubstituteMap

        protected void _updateSubstituteMap()
                                     throws IllegalActionException
        Update the substitute map for the setup and run commands. The base classes adds codeDirectory, modelName, PTII, modules and npmInstall and timeoutFlagAndValue. This method adds modulesFlagAndValue,runForever, and stopForeverAccessor.
        Overrides:
        _updateSubstituteMap in class AccessorCodeGenerator
        Throws:
        IllegalActionException - If the modules parameter contains spaces or if thrown by a base class.