Class DatagramWriter

  • All Implemented Interfaces:
    java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

    public class DatagramWriter
    extends TypedAtomicActor
    This actor sends its input as a Datagram over the network using the UDP protocol. Before being sent, the data is optionally encoded as a text string representing the value being sent. When this option is selected, any Ptolemy data type may be represented. See the encoding parameter.

    NOTE: This actor has problems, the tests do not reliably pass. For details, see https://chess.eecs.berkeley.edu/bugzilla/show_bug.cgi?id=54.

    The address and socket number towards which the datagram is sent are given by the optional inputs remoteAddress and remoteSocketNumber. However, unless/until token(s) arrive at these inputs, default values, given by defaultRemoteAddress and defaultRemoteSocketNumber are used. Note that some IP addresses are special broadcast addresses. An address such as "128.32.239.255" broadcasts to any IP addresses on the "128.23.239.X" subnet. This only works on your own subnet.

    Each instance of this actor needs to allocate a local socket from which to transmit datagrams. Initially, the local socket number is set to 4003, just to pick a number. The socket is not allocated until the model is run.

    Since:
    Ptolemy II 2.0
    Version:
    $Id$
    Author:
    Winthrop Williams, Joern Janneck, Xiaojun Liu, Edward A. Lee (Based on TiltSensor actor written by Chamberlain Fong, Xiaojun Liu, Edward Lee)
    Pt.AcceptedRating:
    Yellow (winthrop)
    Pt.ProposedRating:
    Yellow (winthrop)
    • Field Detail

      • triggerOutput

        public TypedIOPort triggerOutput
        The triggerOutput port. The type of this port is GENERAL, forcing input ports connected here to also be of type GENERAL, (as trigger inputs typically are). This port always transmits a Token with nothing in it. This gives the designer a way to control the order in which other actors fire with respect to the firing of this actor in the SDF domain.
      • localSocketNumber

        public Parameter localSocketNumber
        The local socket number for this actor's socket. Integer in the range 0..65535. Default is 4003.
      • data

        public TypedIOPort data
        Data to be sent. Data can be encoded different ways, depending on the setting of encoding.
      • defaultRemoteAddress

        public Parameter defaultRemoteAddress
        The default remote address to which to send datagrams. This is a string. It will get looked up to find the IP address. (Legal forms of this string include "128.32.239.10" and "localhost".)
      • remoteAddress

        public TypedIOPort remoteAddress
        The remote address towards which to launch the packet. This is a multiport. This permits it to be left unconnected in DE. If multiple addresses are delivered simultaneously to this port, the one arriving via the highest numbered channel is used. Type is string.
      • defaultRemoteSocketNumber

        public Parameter defaultRemoteSocketNumber
        The default remote UDP socket to which to launch the packet. This is an integer in 0..65535. NOTE: TCP sockets get their own distinct, non-interfering, set of 2^16 numbers!
      • remoteSocketNumber

        public TypedIOPort remoteSocketNumber
        The remote socket number towards which to launch the packet. This is a multiport. This permits it to be left unconnected in DE. If multiple addresses are delivered simultaneously to this port, the one arriving via the highest numbered channel is used. Type is integer.
    • Constructor Detail

      • DatagramWriter

        public DatagramWriter​(CompositeEntity container,
                              java.lang.String name)
                       throws NameDuplicationException,
                              IllegalActionException
        Construct a DatagramWriter actor with given name in the given container. Set up ports, parameters and default values. Two of the parameters, defaultRemoteAddress and defaultRemoteSocketNumber, give default values for when the remoteAddress and remoteSocketNumber ports have not yet received tokens. I wonder if the port and parameter concepts could be combined in some way to factor out this functionality. Perhaps every port could have the capability of having a default value and a setting as to whether this default takes effect every time the actor is fired and a token is absent of whether the previous input is repeated once an has been present.
        Parameters:
        container - The container.
        name - The name for this actor.
        Throws:
        NameDuplicationException - If the container already has an actor with this name.
        IllegalActionException - If the actor cannot be contained by this container.
    • Method Detail

      • attributeChanged

        public void attributeChanged​(Attribute attribute)
                              throws IllegalActionException
        Override the base class to reinitialize the state if the localSocketNumber, defaultRemoteAddress or defaultRemoteSocketNumber parameter has changed.

        If the parameter changed is localSocketNumber, then if the model is running (as evidenced by socket != null) then close socket and reopen with new socket number (even if it is the same as the old socket number). Do not close the socket until a new one has been successfully opened. If defaultRemoteAddress or defaultRemoteSocketNumber is changed, simply update these parameters, checking, in the case of the address, that it passes lookup anc conversion to an IP address. If the encoding parameter is changed, set the private encoding settings to the new values.

        Overrides:
        attributeChanged in class NamedObj
        Parameters:
        attribute - The attribute that changed.
        Throws:
        IllegalActionException - If the socket cannot be created.
      • fire

        public void fire()
                  throws IllegalActionException
        Does up to three things, in this order: Set new remote address value, Set new remote socket number, transmit data as a UDP packet over the ethernet. The first two can, of course, affect where the datagram goes. Any remote address/socket values supplied are saved and become the defaults for next time.
        Specified by:
        fire in interface Executable
        Overrides:
        fire in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - Not thrown in this base class.
      • initialize

        public void initialize()
                        throws IllegalActionException
        Initialize allocates the socket and makes use of default parameters for the remote address and socket to which datagrams will be sent.
        Specified by:
        initialize in interface Initializable
        Overrides:
        initialize in class AtomicActor<TypedIOPort>
        Throws:
        IllegalActionException - If the default remote socket number is outside the range 0..65535, the local socket number is outside the range 0..65535, the local socket cannot be created with the given socket number (such as because that number is already in use), or translation of remote address fails to make an IP address from the given address string (i.e. InetAddress.getByName() fails in the address lookup attempt.