View Javadoc
1   package org.sim0mq.message.federatestarter;
2   
3   import org.djutils.serialization.SerializationException;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.MessageStatus;
6   import org.sim0mq.message.Sim0MQMessage;
7   import org.sim0mq.message.SimulationMessage;
8   
9   /**
10   * RequestStatus, FS.1. This message is sent by the Federate Starter to the Model until a "started" response is received from
11   * the Model. Since the message type id clarifies the function of this message and no information exchange is necessary, the
12   * payload field can be empty (number of fields = 0).
13   * <p>
14   * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
16   * </p>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class FS1RequestStatusMessage extends Sim0MQMessage
20  {
21      /** */
22      private static final long serialVersionUID = 20170422L;
23  
24      /** the unique message id. */
25      private static final String MESSAGETYPE = "FS.1";
26  
27      /**
28       * @param simulationRunId the Simulation run ids can be provided in different types. Examples are two 64-bit longs
29       *            indicating a UUID, or a String with a UUID number, a String with meaningful identification, or a short or an
30       *            int with a simulation run number.
31       * @param senderId The sender id can be used to send back a message to the sender at some later time.
32       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
33       *            error can be sent if we receive a message not meant for us).
34       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
35       *            correctly. The number is unique for the sender, so not globally within the federation.
36       * @throws Sim0MQException on unknown data type
37       * @throws NullPointerException when one of the parameters is null
38       */
39      public FS1RequestStatusMessage(final Object simulationRunId, final Object senderId, final Object receiverId,
40              final long messageId) throws Sim0MQException, NullPointerException
41      {
42          super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW);
43      }
44  
45      /**
46       * @return messagetype
47       */
48      public static final String getMessageType()
49      {
50          return MESSAGETYPE;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public short getNumberOfPayloadFields()
56      {
57          return 0;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public Object[] createObjectArray()
63      {
64          return new Object[] {getMagicNumber(), getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
65                  getMessageId(), getMessageStatus(), getNumberOfPayloadFields()};
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public byte[] createByteArray() throws Sim0MQException, SerializationException
71      {
72          return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
73                  getMessageId(), getMessageStatus());
74      }
75  
76      /**
77       * Build a message from an Object[] that was received.
78       * @param fields Object[]; the fields in the message
79       * @param intendedReceiverId id of the intended receiver
80       * @return a Sim0MQ message
81       * @throws Sim0MQException when number of fields is not correct
82       */
83      public static FS1RequestStatusMessage createMessage(final Object[] fields, final Object intendedReceiverId)
84              throws Sim0MQException
85      {
86          check(fields, 0, MESSAGETYPE, intendedReceiverId);
87          return new FS1RequestStatusMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue());
88      }
89  
90      /**
91       * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
92       * message.
93       * <p>
94       * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
95       * <br>
96       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
97       * </p>
98       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
99       */
100     public static class Builder extends Sim0MQMessage.Builder<FS1RequestStatusMessage.Builder>
101     {
102         /**
103          * Empty constructor.
104          */
105         public Builder()
106         {
107             // nothing to do.
108         }
109 
110         /** {@inheritDoc} */
111         @Override
112         public FS1RequestStatusMessage build() throws Sim0MQException, NullPointerException
113         {
114             return new FS1RequestStatusMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId);
115         }
116 
117     }
118 }