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