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    * StatusMessage, MC.1. The Model sends this message as a response to RequestStatus messages sent by the Federate Starter or the
10   * Federation Manager.
11   * <p>
12   * Copyright (c) 2016-2020 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 MC1StatusMessage extends Sim0MQReply
18  {
19      /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
20      private final String 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.1";
27  
28      /** */
29      private static final long serialVersionUID = 20170422L;
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 A string that refers to the model status. Four options: "started", "running", "ended", "error".
42       * @param error Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string.
43       * @throws Sim0MQException on unknown data type
44       * @throws NullPointerException when one of the parameters is null
45       */
46      public MC1StatusMessage(final Object federationId, final Object senderId, final Object receiverId, final Object messageId,
47              final Object uniqueId, final String status, final String error) throws Sim0MQException, NullPointerException
48      {
49          this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 3, uniqueId,
50                  status, error});
51      }
52  
53      /**
54       * @param objectArray Object[]; Full message object array
55       * @throws Sim0MQException on unknown data type
56       * @throws NullPointerException when one of the parameters is null
57       */
58      public MC1StatusMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
59      {
60          super(objectArray, 3, MESSAGETYPE);
61          Throw.when(!(objectArray[9] instanceof String), Sim0MQException.class, "status (field 9) should be String");
62          this.status = objectArray[9].toString();
63          Throw.when(!(objectArray[10] instanceof String), Sim0MQException.class, "error (field 10) should be String");
64          this.error = objectArray[10].toString();
65      }
66  
67      /**
68       * @return status
69       */
70      public final String getStatus()
71      {
72          return this.status;
73      }
74  
75      /**
76       * @return error
77       */
78      public final String getError()
79      {
80          return this.error;
81      }
82  
83      /**
84       * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
85       * message.
86       * <p>
87       * Copyright (c) 2016-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
88       * <br>
89       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
90       * </p>
91       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
92       */
93      public static class Builder extends Sim0MQReply.Builder<MC1StatusMessage.Builder>
94      {
95          /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
96          private String status;
97  
98          /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
99          private String error;
100 
101         /**
102          * Empty constructor.
103          */
104         public Builder()
105         {
106             // nothing to do.
107         }
108 
109         /**
110          * @param newStatus set status
111          * @return the original object for chaining
112          */
113         public final Builder setStatus(final String newStatus)
114         {
115             this.status = newStatus;
116             return this;
117         }
118 
119         /**
120          * @param newError set error
121          * @return the original object for chaining
122          */
123         public final Builder setError(final String newError)
124         {
125             this.error = newError;
126             return this;
127         }
128 
129         /** {@inheritDoc} */
130         @Override
131         public MC1StatusMessage build() throws Sim0MQException, NullPointerException
132         {
133             return new MC1StatusMessage(this.federationId, this.senderId, this.receiverId, this.messageId, this.replyToId,
134                     this.status, this.error);
135         }
136 
137     }
138 }