View Javadoc
1   package org.sim0mq.message;
2   
3   import org.sim0mq.Sim0MQException;
4   
5   /**
6    * The abstract body of a reply message with the first fields of every Sim0MQ reply message.
7    * <p>
8    * Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
10   * </p>
11   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
12   * initial version Apr 22, 2017 <br>
13   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   */
15  public abstract class Sim0MQReply extends Sim0MQMessage
16  {
17      /** */
18      private static final long serialVersionUID = 20170422L;
19  
20      /** The unique message id (Frame 5) of the sender for which this is the reply. */
21      private final long replyToId;
22  
23      /**
24       * Encode the object array into a message.
25       * @param simulationRunId the Simulation run ids can be provided in different types. Examples are two 64-bit longs
26       *            indicating a UUID, or a String with a UUID number, a String with meaningful identification, or a short or an
27       *            int with a simulation run number.
28       * @param senderId The sender id can be used to send back a message to the sender at some later time.
29       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
30       *            error can be sent if we receive a message not meant for us).
31       * @param messageTypeId Message type ids can be defined per type of simulation, and can be provided in different types.
32       *            Examples are a String with a meaningful identification, or a short or an int with a message type number.
33       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
34       *            correctly. The number is unique for the sender, so not globally within the federation.
35       * @param messageStatus Three different status messages are defined: 1 for new, 2 for change, and 3 for delete. This field
36       *            is coded as a byte.
37       * @param replyToId The unique message id (Frame 5) of the sender for which this is the reply.
38       * @throws Sim0MQException on unknown data type
39       * @throws NullPointerException when one of the parameters is null
40       */
41      public Sim0MQReply(final Object simulationRunId, final Object senderId, final Object receiverId, final Object messageTypeId,
42              final long messageId, final MessageStatus messageStatus, final long replyToId)
43              throws Sim0MQException, NullPointerException
44      {
45          super(simulationRunId, senderId, receiverId, messageTypeId, messageId, messageStatus);
46          this.replyToId = replyToId;
47      }
48  
49      /**
50       * @return replyToId
51       */
52      public final long getReplyToId()
53      {
54          return this.replyToId;
55      }
56  
57      /**
58       * Builder for the Sim0MQReply. Can string setters together, and call build() at the end to build the actual message.
59       * <p>
60       * Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
61       * <br>
62       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
63       * </p>
64       * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
65       * initial version Apr 22, 2017 <br>
66       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
67       * @param <B> the actual inherited builder for the return types.
68       */
69      public abstract static class Builder<B extends Sim0MQMessage.Builder<B>> extends Sim0MQMessage.Builder<B>
70      {
71          /** The unique message id (Frame 5) of the sender for which this is the reply. */
72          @SuppressWarnings("checkstyle:visibilitymodifier")
73          protected long replyToId;
74  
75          /**
76           * Empty constructor.
77           */
78          public Builder()
79          {
80              // nothing to do.
81          }
82  
83          /**
84           * @param newReplyToId set replyToId
85           * @return the original object for chaining
86           */
87          public final Builder<B> setReplyToId(final long newReplyToId)
88          {
89              this.replyToId = newReplyToId;
90              return this;
91          }
92  
93          /**
94           * @param message set replyToId and receiver based on the message to which this is a reply
95           * @return the original object for chaining
96           */
97          public final Builder<B> setReplyTo(final Sim0MQMessage message)
98          {
99              this.replyToId = message.getMessageId();
100             this.receiverId = message.getSenderId();
101             return this;
102         }
103 
104         /** {@inheritDoc} */
105         @Override
106         public abstract Sim0MQReply build() throws Sim0MQException, NullPointerException;
107 
108     }
109 }