FS2FederateStartedMessage.java

  1. package org.sim0mq.message.federatestarter;

  2. import org.djutils.exceptions.Throw;
  3. import org.sim0mq.Sim0MQException;
  4. import org.sim0mq.message.Sim0MQMessage;

  5. /**
  6.  * FederateStarted, FS.2. Message sent by the Federate Starter to the Federation Manager in response to message FM.1.
  7.  * <p>
  8.  * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
  10.  * </p>
  11.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  12.  */
  13. public class FS2FederateStartedMessage extends Sim0MQMessage
  14. {
  15.     /** */
  16.     private static final long serialVersionUID = 20170422L;

  17.     /**
  18.      * The sender id of the model that was started or had an error while starting. This is exactly the same as the instanceId
  19.      * sent by the Federation Manager in the StartFederate message.
  20.      */
  21.     private final Object instanceId;

  22.     /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
  23.     private final String status;

  24.     /** The model port number. We use an int in Java due to the fact there is no unsigned short. */
  25.     private final int modelPort;

  26.     /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
  27.     private final String error;

  28.     /** the unique message id. */
  29.     private static final String MESSAGETYPE = "FS.2";

  30.     /**
  31.      * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
  32.      *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
  33.      *            simulation run number.
  34.      * @param senderId The sender id can be used to send back a message to the sender at some later time.
  35.      * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
  36.      *            error can be sent if we receive a message not meant for us).
  37.      * @param messageId The unique message number is meant to confirm with a callback that the message has been received
  38.      *            correctly. The number is unique for the sender, so not globally within the federation.
  39.      * @param instanceId The sender id of the model that was started or had an error while starting. This is exactly the same as
  40.      *            the instanceId sent by the Federation Manager in the StartFederate message.
  41.      * @param status A string that refers to the model status. Four options: "started", "running", "ended", "error".
  42.      * @param modelPort The model port number. We use an int in Java due to the fact there is no unsigned short.
  43.      * @param error Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string.
  44.      * @throws Sim0MQException on unknown data type
  45.      * @throws NullPointerException when one of the parameters is null
  46.      */
  47.     @SuppressWarnings("checkstyle:parameternumber")
  48.     public FS2FederateStartedMessage(final Object federationId, final Object senderId, final Object receiverId,
  49.             final Object messageId, final Object instanceId, final String status, final int modelPort, final String error)
  50.             throws Sim0MQException, NullPointerException
  51.     {
  52.         this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 4,
  53.                 instanceId, status, modelPort, error});
  54.     }

  55.     /**
  56.      * @param objectArray Object[]; the fields that constitute the message
  57.      * @throws Sim0MQException on unknown data type
  58.      * @throws NullPointerException when one of the parameters is null
  59.      */
  60.     public FS2FederateStartedMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
  61.     {
  62.         super(objectArray, 4, MESSAGETYPE);

  63.         this.instanceId = objectArray[8];
  64.         Throw.when(!(objectArray[9] instanceof String), Sim0MQException.class, "status (field 9) should be a String");
  65.         this.status = objectArray[9].toString();
  66.         Throw.when(this.status.isEmpty(), Sim0MQException.class, "status cannot be empty");
  67.         Throw.when(
  68.                 !this.status.equals("started") && !this.status.equals("running") && !this.status.equals("ended")
  69.                         && !this.status.equals("error"),
  70.                 Sim0MQException.class, "status should be one of 'started', 'running', 'ended', 'error'");
  71.         Throw.when(!(objectArray[10] instanceof Integer), Sim0MQException.class, "modelPort (field 10) should be an Integer");
  72.         this.modelPort = (Integer) objectArray[10];
  73.         Throw.when(this.modelPort < 0 || this.modelPort > 65535, Sim0MQException.class,
  74.                 "modelPort should be between 0 and 65535");
  75.         Throw.when(!(objectArray[11] instanceof String), Sim0MQException.class, "error (field 11) should be a String");
  76.         this.error = objectArray[11].toString();
  77.     }

  78.     /**
  79.      * @return instanceId
  80.      */
  81.     public Object getInstanceId()
  82.     {
  83.         return this.instanceId;
  84.     }

  85.     /**
  86.      * @return status
  87.      */
  88.     public String getStatus()
  89.     {
  90.         return this.status;
  91.     }

  92.     /**
  93.      * @return modelPort
  94.      */
  95.     public int getModelPort()
  96.     {
  97.         return this.modelPort;
  98.     }

  99.     /**
  100.      * @return error
  101.      */
  102.     public String getError()
  103.     {
  104.         return this.error;
  105.     }

  106.     /**
  107.      * Builder for the FederateStarted Message. Can string setters together, and call build() at the end to build the actual
  108.      * message.
  109.      * <p>
  110.      * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  111.      * <br>
  112.      * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
  113.      * </p>
  114.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  115.      */
  116.     public static class Builder extends Sim0MQMessage.Builder<FS2FederateStartedMessage.Builder>
  117.     {
  118.         /**
  119.          * The sender id of the model that was started or had an error while starting. This is exactly the same as the
  120.          * instanceId sent by the Federation Manager in the StartFederate message.
  121.          */
  122.         private Object instanceId;

  123.         /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
  124.         private String status;

  125.         /** The model port number. We use an int in Java due to the fact there is no unsigned short. */
  126.         private int modelPort;

  127.         /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
  128.         private String error;

  129.         /**
  130.          * Empty constructor.
  131.          */
  132.         public Builder()
  133.         {
  134.             // nothing to do.
  135.         }

  136.         /**
  137.          * @param newInstanceId set instanceId
  138.          * @return the original object for chaining
  139.          */
  140.         public final Builder setInstanceId(final Object newInstanceId)
  141.         {
  142.             this.instanceId = newInstanceId;
  143.             return this;
  144.         }

  145.         /**
  146.          * @param newStatus set status
  147.          * @return the original object for chaining
  148.          */
  149.         public final Builder setStatus(final String newStatus)
  150.         {
  151.             this.status = newStatus;
  152.             return this;
  153.         }

  154.         /**
  155.          * @param newModelPort set modelPort (int instead of short because of signed short in Java)
  156.          * @return the original object for chaining
  157.          */
  158.         public final Builder setModelPort(final int newModelPort)
  159.         {
  160.             this.modelPort = newModelPort;
  161.             return this;
  162.         }

  163.         /**
  164.          * @param newError set error
  165.          * @return the original object for chaining
  166.          */
  167.         public final Builder setError(final String newError)
  168.         {
  169.             this.error = newError;
  170.             return this;
  171.         }

  172.         @Override
  173.         public FS2FederateStartedMessage build() throws Sim0MQException, NullPointerException
  174.         {
  175.             return new FS2FederateStartedMessage(this.federationId, this.senderId, this.receiverId, this.messageId,
  176.                     this.instanceId, this.status, this.modelPort, this.error);
  177.         }

  178.     }
  179. }