View Javadoc
1   package org.sim0mq.message.modelcontroller;
2   
3   import org.djutils.exceptions.Throw;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.Sim0MQMessage;
6   import org.sim0mq.message.Sim0MQReply;
7   
8   /**
9    * AckNak, MC.2. Message sent by the Model to acknowledge the reception and implementation of a message sent by the Federation
10   * Manager.
11   * <p>
12   * Copyright (c) 2016-2024 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 MC2AckNakMessage extends Sim0MQReply
18  {
19      /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
20      private final boolean status;
21  
22      /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
23      private final String error;
24  
25      /** the unique message id. */
26      private static final String MESSAGETYPE = "MC.2";
27  
28      /** */
29      private static final long serialVersionUID = 20190712;
30  
31      /**
32       * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
33       *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
34       *            simulation run number.
35       * @param senderId The sender id can be used to send back a message to the sender at some later time.
36       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
37       *            error can be sent if we receive a message not meant for us).
38       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
39       *            correctly. The number is unique for the sender, so not globally within the federation.
40       * @param uniqueId Id to identify the callback to the message.
41       * @param status boolean; indicates whether the command sent by the FM has been successfully implemented, e.g. whether the
42       *            run control parameters are set successfully.
43       * @param error If ‘status’ is False, an error message that indicates what went wrong. Otherwise, an empty string.
44       * @throws Sim0MQException on unknown data type
45       * @throws NullPointerException when one of the parameters is null
46       */
47      public MC2AckNakMessage(final Object federationId, final Object senderId, final Object receiverId, final Object messageId,
48              final Object uniqueId, final boolean status, final String error) throws Sim0MQException, NullPointerException
49      {
50          this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 3, uniqueId,
51                  status, error});
52      }
53  
54      /**
55       * @param objectArray Object[]; Full message object array
56       * @throws Sim0MQException on unknown data type
57       * @throws NullPointerException when one of the parameters is null
58       */
59      public MC2AckNakMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
60      {
61          super(objectArray, 3, MESSAGETYPE);
62          Throw.when(!(objectArray[9] instanceof Boolean), Sim0MQException.class, "status (field 9) should be Boolean");
63          this.status = ((Boolean) objectArray[9]).booleanValue();
64          Throw.when(!(objectArray[10] instanceof String), Sim0MQException.class, "error (field 10) should be String");
65          this.error = objectArray[10].toString();
66      }
67  
68      /**
69       * @return status
70       */
71      public final boolean getStatus()
72      {
73          return this.status;
74      }
75  
76      /**
77       * @return error
78       */
79      public final String getError()
80      {
81          return this.error;
82      }
83  
84      /**
85       * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
86       * message.
87       * <p>
88       * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
89       * <br>
90       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
91       * </p>
92       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
93       */
94      public static class Builder extends Sim0MQReply.Builder<MC2AckNakMessage.Builder>
95      {
96          /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
97          private boolean status;
98  
99          /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
100         private String error;
101 
102         /**
103          * Empty constructor.
104          */
105         public Builder()
106         {
107             // nothing to do.
108         }
109 
110         /**
111          * @param newStatus set status
112          * @return the original object for chaining
113          */
114         public final Builder setStatus(final boolean newStatus)
115         {
116             this.status = newStatus;
117             return this;
118         }
119 
120         /**
121          * @param newError set error
122          * @return the original object for chaining
123          */
124         public final Builder setError(final String newError)
125         {
126             this.error = newError;
127             return this;
128         }
129 
130         @Override
131         public MC2AckNakMessage build() throws Sim0MQException, NullPointerException
132         {
133             return new MC2AckNakMessage(this.federationId, this.senderId, this.receiverId, this.messageId, this.replyToId,
134                     this.status, this.error);
135         }
136 
137     }
138 }