Class HelperBase

  • Direct Known Subclasses:
    CameraHelper, CryptoHelper, DataConverterHelper, MQTTHelper, SerialHelper, SpeechRecognitionHelper, TextDisplayHelper, VertxBrowserHelper, VertxHelperBase

    public class HelperBase
    extends java.lang.Object
    A base class for helper classes. The main function of this class is to provide a reference to the JavaScript actor for which the helper is helping. This is available in a protected method so that it isn't directly available in JavaScript. This actor should be used for all synchronized actions (to avoid deadlocks and race conditions).
    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Edward A. Lee, Hokeun Kim
    Pt.AcceptedRating:
    Red (bilung)
    Pt.ProposedRating:
    Yellow (eal)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  HelperBase.DATA_TYPE
      Support data types for reading and writing to buffers.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected JavaScript _actor
      The JavaScript actor that this is helping.
      protected jdk.nashorn.api.scripting.ScriptObjectMirror _currentObj
      The JavaScript object that this is a helper for.
    • Constructor Summary

      Constructors 
      Constructor Description
      HelperBase​(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror helping)
      Construct a helper for the specified actor and JavaScript object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void _appendNumericToBuffer​(io.vertx.core.buffer.Buffer buffer, java.lang.Object data, HelperBase.DATA_TYPE type)
      Append a numeric instance of the specified type to a buffer.
      protected void _appendToBuffer​(java.lang.Object data, HelperBase.DATA_TYPE type, java.lang.String imageType, io.vertx.core.buffer.Buffer buffer)
      Append data to be sent to the specified buffer.
      protected void _error​(java.lang.String message)
      Handle an error by emitting an error event, or if there is no error event handler registered, by invoking the error() method of the associated actor.
      protected void _error​(java.lang.String message, java.lang.Throwable throwable)
      Handle an error by emitting an error event, or if there is no error event handler registered, by invoking the error() method of the associated actor.
      protected void _error​(jdk.nashorn.api.scripting.ScriptObjectMirror emitter, java.lang.String message)
      Emit the error message in the director thread by emitting an "error" event on the specified JavaScript object.
      protected void _error​(jdk.nashorn.api.scripting.ScriptObjectMirror emitter, java.lang.String message, java.lang.Throwable throwable)
      Emit the error message in the director thread by emitting an "error" event on the specified JavaScript object.
      protected java.lang.Object _extractFromBuffer​(io.vertx.core.buffer.Buffer buffer, HelperBase.DATA_TYPE type, int position)
      Extract a numeric instance of the specified type from a buffer.
      protected void _issueResponse​(java.lang.Runnable response)
      Execute the specified response in the director thread.
      protected static java.util.TreeSet<java.lang.String> _removeDuplicates​(java.lang.String[] original)
      Given an array of strings, return an array where everything is converted to lower case, duplicates are removed, and the order is alphabetical.
      protected int _sizeOfType​(HelperBase.DATA_TYPE type)
      Return the size of a data type.
      protected static byte[] _toJavaBytes​(java.lang.Object object)
      Convert the native JavaScript array or JavaScript string into a Java byte array.
      protected java.lang.Object _toJSArray​(byte[] buffer)
      Convert a Java byte array into the JavaScript integer array.
      jdk.nashorn.api.scripting.ScriptObjectMirror getHelping()
      Return the JavaScript object that this helper is helping.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _actor

        protected JavaScript _actor
        The JavaScript actor that this is helping. All synchronization done by the helper should synchronize using this object as the monitor.
      • _currentObj

        protected jdk.nashorn.api.scripting.ScriptObjectMirror _currentObj
        The JavaScript object that this is a helper for.
    • Constructor Detail

      • HelperBase

        public HelperBase​(java.lang.Object actor,
                          jdk.nashorn.api.scripting.ScriptObjectMirror helping)
        Construct a helper for the specified actor and JavaScript object.
        Parameters:
        actor - The actor associated with this helper.
        helping - The object that this is helping.
    • Method Detail

      • getHelping

        public jdk.nashorn.api.scripting.ScriptObjectMirror getHelping()
        Return the JavaScript object that this helper is helping.
        Returns:
        The helping object given in the constructor.
      • _error

        protected void _error​(java.lang.String message)
        Handle an error by emitting an error event, or if there is no error event handler registered, by invoking the error() method of the associated actor. Note that this may not stop execution.
        Parameters:
        message - The error message.
      • _error

        protected void _error​(java.lang.String message,
                              java.lang.Throwable throwable)
        Handle an error by emitting an error event, or if there is no error event handler registered, by invoking the error() method of the associated actor. Note that this may not stop execution.
        Parameters:
        message - The error message.
        throwable - The throwable.
      • _error

        protected void _error​(jdk.nashorn.api.scripting.ScriptObjectMirror emitter,
                              java.lang.String message)
        Emit the error message in the director thread by emitting an "error" event on the specified JavaScript object.
        Parameters:
        emitter - The JavaScript object that should emit an "error" event.
        message - The error message.
      • _error

        protected void _error​(jdk.nashorn.api.scripting.ScriptObjectMirror emitter,
                              java.lang.String message,
                              java.lang.Throwable throwable)
        Emit the error message in the director thread by emitting an "error" event on the specified JavaScript object.
        Parameters:
        emitter - The JavaScript object that should emit an "error" event.
        message - The error message.
        throwable - The exception that caused the error.
      • _appendNumericToBuffer

        protected void _appendNumericToBuffer​(io.vertx.core.buffer.Buffer buffer,
                                              java.lang.Object data,
                                              HelperBase.DATA_TYPE type)
        Append a numeric instance of the specified type to a buffer.
        Parameters:
        buffer - The buffer to which to append
        data - The data to be appended
        type - The type of data.
      • _appendToBuffer

        protected void _appendToBuffer​(java.lang.Object data,
                                       HelperBase.DATA_TYPE type,
                                       java.lang.String imageType,
                                       io.vertx.core.buffer.Buffer buffer)
        Append data to be sent to the specified buffer.
        Parameters:
        data - The data to append.
        type - The type of data append.
        imageType - If the type is IMAGE, then then the image encoding to use, or null to use the default (JPG).
        buffer - The buffer.
      • _extractFromBuffer

        protected java.lang.Object _extractFromBuffer​(io.vertx.core.buffer.Buffer buffer,
                                                      HelperBase.DATA_TYPE type,
                                                      int position)
        Extract a numeric instance of the specified type from a buffer.
        Parameters:
        buffer - The buffer containing the data.
        type - The type to extract.
        position - The position in the buffer from which to extract it.
        Returns:
        The numeric instance.
      • _issueResponse

        protected void _issueResponse​(java.lang.Runnable response)
        Execute the specified response in the director thread. If this is called within the director thread and the associated actor is in its fire() method, then the response is executed immediately. Otherwise, it is deferred using the director's fireAtCurrentTime() function. o * This is useful, for example, when a response produces multiple output events or errors, because it ensures that all those output events and errors are simultaneous in the DE sense. It also prevents threading issues from having the response execute concurrently with the swarmlet execution. Generally, this should not be used for callbacks that will register listeners in reaction to some event, because the listeners will be registered an arbitrary amount of time in the future, which can cause events to be missed.
        Parameters:
        response - The response to execute.
      • _removeDuplicates

        protected static java.util.TreeSet<java.lang.String> _removeDuplicates​(java.lang.String[] original)
        Given an array of strings, return an array where everything is converted to lower case, duplicates are removed, and the order is alphabetical.
        Parameters:
        original - The array of strings
        Returns:
        the sorted, lower case, deduped array of Strings.
      • _sizeOfType

        protected int _sizeOfType​(HelperBase.DATA_TYPE type)
        Return the size of a data type.
        Parameters:
        type - The object
        Returns:
        The size
      • _toJSArray

        protected java.lang.Object _toJSArray​(byte[] buffer)
                                       throws IllegalActionException
        Convert a Java byte array into the JavaScript integer array.
        Parameters:
        buffer - The input Java byte array to be converted.
        Returns:
        The resulting JavaScript integer array.
        Throws:
        IllegalActionException - If the conversion fails.
      • _toJavaBytes

        protected static byte[] _toJavaBytes​(java.lang.Object object)
                                      throws IllegalActionException
        Convert the native JavaScript array or JavaScript string into a Java byte array. When the input is String that starts with "0x", interpret the input as hex bytes.
        Parameters:
        object - The input in JavaScript object, either a JavaScrypt integer array or a JavaScript string. If the string starts with "0x", it is interpreted as hex bytes.
        Returns:
        The Java byte array.
        Throws:
        IllegalActionException - If the conversion fails.