1 package org.sim0mq.message.federationmanager;
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 * KillAll, FM.9. Kill all federates on the particular node of the FederateStarter that are still running.
11 * <p>
12 * Copyright (c) 2019-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 FM9KillAllMessage extends Sim0MQMessage
18 {
19 /** the unique message id. */
20 private static final String MESSAGETYPE = "FM.9";
21
22 /** */
23 private static final long serialVersionUID = 20190712L;
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 @SuppressWarnings("checkstyle:parameternumber")
38 public FM9KillAllMessage(final Object simulationRunId, final Object senderId, final Object receiverId, final long messageId)
39 throws Sim0MQException, NullPointerException
40 {
41 super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW);
42 }
43
44 /**
45 * @return messagetype
46 */
47 public static final String getMessageType()
48 {
49 return MESSAGETYPE;
50 }
51
52 /** {@inheritDoc} */
53 @Override
54 public short getNumberOfPayloadFields()
55 {
56 return 0;
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public Object[] createObjectArray()
62 {
63 return new Object[] {getMagicNumber(), getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
64 getMessageId(), getMessageStatus(), getNumberOfPayloadFields()};
65 }
66
67 /** {@inheritDoc} */
68 @Override
69 public byte[] createByteArray() throws Sim0MQException, SerializationException
70 {
71 return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
72 getMessageId(), getMessageStatus());
73 }
74
75 /**
76 * Build a message from an Object[] that was received.
77 * @param fields Object[]; the fields in the message
78 * @param intendedReceiverId id of the intended receiver
79 * @return a Sim0MQ message
80 * @throws Sim0MQException when number of fields is not correct
81 */
82 public static FM9KillAllMessage createMessage(final Object[] fields, final Object intendedReceiverId) throws Sim0MQException
83 {
84 check(fields, 0, MESSAGETYPE, intendedReceiverId);
85 return new FM9KillAllMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue());
86 }
87
88 /**
89 * Builder for the KillAll Message. Can string setters together, and call build() at the end to build the actual message.
90 * <p>
91 * Copyright (c) 2019-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
92 * <br>
93 * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
94 * </p>
95 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
96 */
97 public static class Builder extends Sim0MQMessage.Builder<FM9KillAllMessage.Builder>
98 {
99 /**
100 * Empty constructor.
101 */
102 public Builder()
103 {
104 // nothing to do.
105 }
106
107 /** {@inheritDoc} */
108 @Override
109 public FM9KillAllMessage build() throws Sim0MQException, NullPointerException
110 {
111 return new FM9KillAllMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId);
112 }
113
114 }
115 }