1 package org.sim0mq.message.federatestarter;
2
3 import org.djutils.serialization.SerializationException;
4 import org.sim0mq.Sim0MQException;
5 import org.sim0mq.message.MessageStatus;
6 import org.sim0mq.message.Sim0MQMessage;
7 import org.sim0mq.message.SimulationMessage;
8
9 /**
10 * KillModel, FS.3. The message is sent by the federate starter to a Model Controller. The number of extra fields is zero.
11 * <p>
12 * Copyright (c) 2016-2019 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 FS3KillModelMessage extends Sim0MQMessage
18 {
19 /** */
20 private static final long serialVersionUID = 20170422L;
21
22 /** the unique message id. */
23 private static final String MESSAGETYPE = "FS.3";
24
25 /**
26 * @param simulationRunId the Simulation run ids can be provided in different types. Examples are two 64-bit longs
27 * indicating a UUID, or a String with a UUID number, a String with meaningful identification, or a short or an
28 * int with a simulation run number.
29 * @param senderId The sender id can be used to send back a message to the sender at some later time.
30 * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
31 * error can be sent if we receive a message not meant for us).
32 * @param messageId The unique message number is meant to confirm with a callback that the message has been received
33 * correctly. The number is unique for the sender, so not globally within the federation.
34 * @throws Sim0MQException on unknown data type
35 * @throws NullPointerException when one of the parameters is null
36 */
37 public FS3KillModelMessage(final Object simulationRunId, final Object senderId, final Object receiverId,
38 final long messageId) throws Sim0MQException, NullPointerException
39 {
40 super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW);
41 }
42
43 /**
44 * @return messagetype
45 */
46 public static final String getMessageType()
47 {
48 return MESSAGETYPE;
49 }
50
51 /** {@inheritDoc} */
52 @Override
53 public short getNumberOfPayloadFields()
54 {
55 return 0;
56 }
57
58 /** {@inheritDoc} */
59 @Override
60 public Object[] createObjectArray()
61 {
62 return new Object[] {getMagicNumber(), getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
63 getMessageId(), getMessageStatus(), getNumberOfPayloadFields()};
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public byte[] createByteArray() throws Sim0MQException, SerializationException
69 {
70 return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
71 getMessageId(), getMessageStatus());
72 }
73
74 /**
75 * Build a message from an Object[] that was received.
76 * @param fields Object[]; the fields in the message
77 * @param intendedReceiverId id of the intended receiver
78 * @return a Sim0MQ message
79 * @throws Sim0MQException when number of fields is not correct
80 */
81 public static FS3KillModelMessage createMessage(final Object[] fields, final Object intendedReceiverId)
82 throws Sim0MQException
83 {
84 check(fields, 0, MESSAGETYPE, intendedReceiverId);
85 return new FS3KillModelMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue());
86 }
87
88 /**
89 * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
90 * message.
91 * <p>
92 * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
93 * <br>
94 * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
95 * </p>
96 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
97 */
98 public static class Builder extends Sim0MQMessage.Builder<FS3KillModelMessage.Builder>
99 {
100 /**
101 * Empty constructor.
102 */
103 public Builder()
104 {
105 // nothing to do.
106 }
107
108 /** {@inheritDoc} */
109 @Override
110 public FS3KillModelMessage build() throws Sim0MQException, NullPointerException
111 {
112 return new FS3KillModelMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId);
113 }
114
115 }
116 }