View Javadoc
1   package org.sim0mq.message.federatestarter;
2   
3   import org.djutils.exceptions.Throw;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.Sim0MQMessage;
6   
7   /**
8    * FederateKilled, FS.4. Message sent by the Federate Starter to the Federation Manager in response to message FM.8.
9    * <p>
10   * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
12   * </p>
13   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   */
15  public class FS4FederateKilledMessage extends Sim0MQMessage
16  {
17      /** */
18      private static final long serialVersionUID = 20170422L;
19  
20      /** The sender id of the model that was started and now has to be terminated. */
21      private final Object instanceId;
22  
23      /** Did the termination of the model succeed? */
24      private final boolean status;
25  
26      /** If there was an error, the error message is sent as well. Otherwise this field is an empty string. */
27      private final String error;
28  
29      /** the unique message id. */
30      private static final String MESSAGETYPE = "FS.4";
31  
32      /**
33       * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
34       *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
35       *            simulation run number.
36       * @param senderId The sender id can be used to send back a message to the sender at some later time.
37       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
38       *            error can be sent if we receive a message not meant for us).
39       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
40       *            correctly. The number is unique for the sender, so not globally within the federation.
41       * @param instanceId The sender id of the model that was killed or had an error while killing. This is exactly the same as
42       *            the instanceId sent by the Federation Manager in the KillFederate message.
43       * @param status Did the termination of the model succeed?
44       * @param error If there was an error with the model termination, the error message is sent as well. Otherwise this field is
45       *            an empty string.
46       * @throws Sim0MQException on unknown data type
47       * @throws NullPointerException when one of the parameters is null
48       */
49      public FS4FederateKilledMessage(final Object federationId, final Object senderId, final Object receiverId,
50              final Object messageId, final Object instanceId, final boolean status, final String error)
51              throws Sim0MQException, NullPointerException
52      {
53          this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 3,
54                  instanceId, status, error});
55      }
56  
57      /**
58       * @param objectArray Object[]; the fields that constitute the message
59       * @throws Sim0MQException on unknown data type
60       * @throws NullPointerException when one of the parameters is null
61       */
62      public FS4FederateKilledMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
63      {
64          super(objectArray, 3, MESSAGETYPE);
65          this.instanceId = objectArray[8];
66          Throw.when(!(objectArray[9] instanceof Boolean), Sim0MQException.class, "status (field 9) should be a Boolean");
67          this.status = ((Boolean) objectArray[9]).booleanValue();
68          Throw.when(!(objectArray[10] instanceof String), Sim0MQException.class, "error (field 10) should be a String");
69          this.error = objectArray[10].toString();
70      }
71  
72      /**
73       * @return instanceId
74       */
75      public Object getInstanceId()
76      {
77          return this.instanceId;
78      }
79  
80      /**
81       * @return status
82       */
83      public boolean isStatus()
84      {
85          return this.status;
86      }
87  
88      /**
89       * @return error
90       */
91      public String getError()
92      {
93          return this.error;
94      }
95  
96      /**
97       * @return messagetype
98       */
99      public static final String getMessageType()
100     {
101         return MESSAGETYPE;
102     }
103 
104     /**
105      * Builder for the FederateStarted Message. Can string setters together, and call build() at the end to build the actual
106      * message.
107      * <p>
108      * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
109      * <br>
110      * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
111      * </p>
112      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
113      */
114     public static class Builder extends Sim0MQMessage.Builder<FS4FederateKilledMessage.Builder>
115     {
116         /**
117          * The sender id of the model that was started or had an error while starting. This is exactly the same as the
118          * instanceId sent by the Federation Manager in the StartFederate message.
119          */
120         private String instanceId;
121 
122         /** Did the termination of the model succeed? */
123         private boolean status;
124 
125         /** If there was an error, the error message is sent as well. Otherwise this field is an empty string. */
126         private String error;
127 
128         /**
129          * Empty constructor.
130          */
131         public Builder()
132         {
133             // nothing to do.
134         }
135 
136         /**
137          * @param newInstanceId set instanceId
138          * @return the original object for chaining
139          */
140         public final Builder setInstanceId(final String newInstanceId)
141         {
142             this.instanceId = newInstanceId;
143             return this;
144         }
145 
146         /**
147          * @param newStatus set status
148          * @return the original object for chaining
149          */
150         public final Builder setStatus(final boolean newStatus)
151         {
152             this.status = newStatus;
153             return this;
154         }
155 
156         /**
157          * @param newError set error
158          * @return the original object for chaining
159          */
160         public final Builder setError(final String newError)
161         {
162             this.error = newError;
163             return this;
164         }
165 
166         @Override
167         public FS4FederateKilledMessage build() throws Sim0MQException, NullPointerException
168         {
169             return new FS4FederateKilledMessage(this.federationId, this.senderId, this.receiverId, this.messageId,
170                     this.instanceId, this.status, this.error);
171         }
172 
173     }
174 }