1 package org.sim0mq.message.modelcontroller;
2
3 import org.djutils.exceptions.Throw;
4 import org.djutils.serialization.SerializationException;
5 import org.sim0mq.Sim0MQException;
6 import org.sim0mq.message.MessageStatus;
7 import org.sim0mq.message.Sim0MQReply;
8 import org.sim0mq.message.SimulationMessage;
9
10 /**
11 * AckNak, MC.2. Message sent by the Model to acknowledge the reception and implementation of a message sent by the Federation
12 * Manager.
13 * <p>
14 * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
16 * </p>
17 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18 */
19 public class MC2AckNakMessage extends Sim0MQReply
20 {
21 /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
22 private final boolean status;
23
24 /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
25 private final String error;
26
27 /** the unique message id. */
28 private static final String MESSAGETYPE = "MC.2";
29
30 /** */
31 private static final long serialVersionUID = 20190712;
32
33 /**
34 * @param simulationRunId the Simulation run ids can be provided in different types. Examples are two 64-bit longs
35 * indicating a UUID, or a String with a UUID number, a String with meaningful identification, or a short or an
36 * int with a simulation run number.
37 * @param senderId The sender id can be used to send back a message to the sender at some later time.
38 * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
39 * error can be sent if we receive a message not meant for us).
40 * @param messageId The unique message number is meant to confirm with a callback that the message has been received
41 * correctly. The number is unique for the sender, so not globally within the federation.
42 * @param uniqueId Id to identify the callback to the message.
43 * @param status boolean; indicates whether the command sent by the FM has been successfully implemented, e.g. whether the
44 * run control parameters are set successfully.
45 * @param error If ‘status’ is False, an error message that indicates what went wrong. Otherwise, an empty string.
46 * @throws Sim0MQException on unknown data type
47 * @throws NullPointerException when one of the parameters is null
48 */
49 public MC2AckNakMessage(final Object simulationRunId, final Object senderId, final Object receiverId, final long messageId,
50 final long uniqueId, final boolean status, final String error) throws Sim0MQException, NullPointerException
51 {
52 super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW, uniqueId);
53 Throw.whenNull(error, "error cannot be null");
54 this.status = status;
55 this.error = error;
56 }
57
58 /**
59 * @return status
60 */
61 public final boolean getStatus()
62 {
63 return this.status;
64 }
65
66 /**
67 * @return error
68 */
69 public final String getError()
70 {
71 return this.error;
72 }
73
74 /**
75 * @return messagetype
76 */
77 public static final String getMessageType()
78 {
79 return MESSAGETYPE;
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public short getNumberOfPayloadFields()
85 {
86 return 3;
87 }
88
89 /** {@inheritDoc} */
90 @Override
91 public Object[] createObjectArray()
92 {
93 return new Object[] {getMagicNumber(), getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
94 getMessageId(), getMessageStatus(), getNumberOfPayloadFields(), getReplyToId(), this.status, this.error};
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public byte[] createByteArray() throws Sim0MQException, SerializationException
100 {
101 return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
102 getMessageId(), getMessageStatus(), getReplyToId(), this.status, this.error);
103 }
104
105 /**
106 * Build a message from an Object[] that was received.
107 * @param fields Object[]; the fields in the message
108 * @param intendedReceiverId id of the intended receiver
109 * @return a Sim0MQ message
110 * @throws Sim0MQException when number of fields is not correct
111 */
112 public static MC2AckNakMessage createMessage(final Object[] fields, final Object intendedReceiverId) throws Sim0MQException
113 {
114 check(fields, 3, MESSAGETYPE, intendedReceiverId);
115 return new MC2AckNakMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue(),
116 ((Long) fields[8]).longValue(), (boolean) fields[9], fields[10].toString());
117 }
118
119 /**
120 * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
121 * message.
122 * <p>
123 * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
124 * <br>
125 * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
126 * </p>
127 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
128 */
129 public static class Builder extends Sim0MQReply.Builder<MC2AckNakMessage.Builder>
130 {
131 /** A string that refers to the model status. Four options: "started", "running", "ended", "error". */
132 private boolean status;
133
134 /** Optional. If there is an error, the error message is sent as well. Otherwise this field is an empty string. */
135 private String error;
136
137 /**
138 * Empty constructor.
139 */
140 public Builder()
141 {
142 // nothing to do.
143 }
144
145 /**
146 * @param newStatus set status
147 * @return the original object for chaining
148 */
149 public final Builder setStatus(final boolean newStatus)
150 {
151 this.status = newStatus;
152 return this;
153 }
154
155 /**
156 * @param newError set error
157 * @return the original object for chaining
158 */
159 public final Builder setError(final String newError)
160 {
161 this.error = newError;
162 return this;
163 }
164
165 /** {@inheritDoc} */
166 @Override
167 public MC2AckNakMessage build() throws Sim0MQException, NullPointerException
168 {
169 return new MC2AckNakMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId, this.replyToId,
170 this.status, this.error);
171 }
172
173 }
174 }