View Javadoc
1   package org.sim0mq.message.model;
2   
3   import org.sim0mq.Sim0MQException;
4   import org.sim0mq.message.MessageStatus;
5   import org.sim0mq.message.Sim0MQMessage;
6   import org.sim0mq.message.Sim0MQReply;
7   import org.sim0mq.message.SimulationMessage;
8   
9   import nl.tudelft.simulation.language.Throw;
10  
11  /**
12   * StatusMessage, MC.1. The Model sends this message as a response to RequestStatus messages sent by the Federate Starter or the
13   * Federation Manager.
14   * <p>
15   * Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
17   * </p>
18   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
19   * initial version Apr 22, 2017 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   */
22  public class StatusMessage extends Sim0MQReply
23  {
24      /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
25      private final String status;
26  
27      /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
28      private final String error;
29  
30      /** the unique message id. */
31      private static final String MESSAGETYPE = "MC.1";
32  
33      /** */
34      private static final long serialVersionUID = 20170422L;
35  
36      /**
37       * @param simulationRunId the Simulation run ids can be provided in different types. Examples are two 64-bit longs
38       *            indicating a UUID, or a String with a UUID number, a String with meaningful identification, or a short or an
39       *            int with a simulation run number.
40       * @param senderId The sender id can be used to send back a message to the sender at some later time.
41       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
42       *            error can be sent if we receive a message not meant for us).
43       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
44       *            correctly. The number is unique for the sender, so not globally within the federation.
45       * @param uniqueId Id to identify the callback to know which model instance has been started, e.g. "IDVV.14". The model
46       *            instance will use this as its sender id.
47       * @param status Code for the software to run, will be looked up in a table on the local computer to determine the path to
48       *            start the software on that computer. Example: "java". If the softwarePath is defined, softwareCode can be an
49       *            empty String (0 characters).
50       * @param error Arguments that the software needs, before the model file path and name; e.g. "–Xmx2G -jar" in case of a Java
51       *            model. This String can be empty (0 characters).
52       * @throws Sim0MQException on unknown data type
53       * @throws NullPointerException when one of the parameters is null
54       */
55      public StatusMessage(final Object simulationRunId, final Object senderId, final Object receiverId, final long messageId,
56              final long uniqueId, final String status, final String error) throws Sim0MQException, NullPointerException
57      {
58          super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW, uniqueId);
59          Throw.whenNull(status, "status cannot be null");
60          Throw.whenNull(error, "error cannot be null");
61  
62          Throw.when(status.isEmpty(), Sim0MQException.class, "status cannot be empty");
63          Throw.when(!status.equals("started") && !status.equals("running") && !status.equals("ended") && !status.equals("error"),
64                  Sim0MQException.class, "status should be one of 'started', 'running', 'ended', 'error'");
65  
66          this.status = status;
67          this.error = error;
68      }
69  
70      /**
71       * @return status
72       */
73      public final String getStatus()
74      {
75          return this.status;
76      }
77  
78      /**
79       * @return error
80       */
81      public final String getError()
82      {
83          return this.error;
84      }
85  
86      /**
87       * @return messagetype
88       */
89      public static final String getMessageType()
90      {
91          return MESSAGETYPE;
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public Object[] createObjectArray()
97      {
98          return new Object[] { getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(), getMessageId(),
99                  getMessageStatus(), getReplyToId(), this.status, this.error };
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public byte[] createByteArray() throws Sim0MQException
105     {
106         return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
107                 getMessageId(), getMessageStatus(), getReplyToId(), this.status, this.error);
108     }
109 
110     /**
111      * Build a message from an Object[] that was received.
112      * @param fields Object[]; the fields in the message
113      * @param intendedReceiverId id of the intended receiver
114      * @return a Sim0MQ message
115      * @throws Sim0MQException when number of fields is not correct
116      */
117     public static StatusMessage createMessage(final Object[] fields, final Object intendedReceiverId) throws Sim0MQException
118     {
119         check(fields, 3, MESSAGETYPE, intendedReceiverId);
120         return new StatusMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue(),
121                 ((Long) fields[8]).longValue(), fields[9].toString(), fields[10].toString());
122     }
123 
124     /**
125      * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
126      * message.
127      * <p>
128      * Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
129      * <br>
130      * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
131      * </p>
132      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
133      * initial version Apr 22, 2017 <br>
134      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
135      */
136     public static class Builder extends Sim0MQMessage.Builder<StatusMessage.Builder>
137     {
138         /** The unique message id (Frame 5) of the sender for which this is the reply. */
139         private long uniqueId;
140 
141         /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
142         private String status;
143 
144         /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
145         private String error;
146 
147         /**
148          * Empty constructor.
149          */
150         public Builder()
151         {
152             // nothing to do.
153         }
154 
155         /**
156          * @param newUniqueId set uniqueId
157          * @return the original object for chaining
158          */
159         public final Builder setUniqueId(final long newUniqueId)
160         {
161             this.uniqueId = newUniqueId;
162             return this;
163         }
164 
165         /**
166          * @param newStatus set status
167          * @return the original object for chaining
168          */
169         public final Builder setStatus(final String newStatus)
170         {
171             this.status = newStatus;
172             return this;
173         }
174 
175         /**
176          * @param newError set error
177          * @return the original object for chaining
178          */
179         public final Builder setError(final String newError)
180         {
181             this.error = newError;
182             return this;
183         }
184 
185         /** {@inheritDoc} */
186         @Override
187         public Sim0MQMessage build() throws Sim0MQException, NullPointerException
188         {
189             return new StatusMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId, this.uniqueId,
190                     this.status, this.error);
191         }
192 
193     }
194 }