View Javadoc
1   /*
2    * #%L
3    * PendingMessage.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.client.message;
21  
22  import com.allanbank.mongodb.client.Message;
23  import com.allanbank.mongodb.client.callback.ReplyCallback;
24  
25  /**
26   * Container for a pending message. Before the message is sent the message id
27   * will be zero. After it will contain the assigned message id for the
28   * connection.
29   * 
30   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
31   *         mutated in incompatible ways between any two releases of the driver.
32   * @copyright 2011-2014, Allanbank Consulting, Inc., All Rights Reserved
33   */
34  public class PendingMessage {
35  
36      /** The message sent. */
37      private Message myMessage;
38  
39      /** The message id assigned to the sent message. */
40      private int myMessageId;
41  
42      /** The callback for the reply to the message. */
43      private ReplyCallback myReplyCallback;
44  
45      /** The timestamp of the message. */
46      private long myTimestamp;
47  
48      /**
49       * Create a new PendingMessage.
50       */
51      public PendingMessage() {
52          this(0, null, null);
53      }
54  
55      /**
56       * Create a new PendingMessage.
57       * 
58       * @param messageId
59       *            The id assigned to the message.
60       * @param message
61       *            The sent message.
62       */
63      public PendingMessage(final int messageId, final Message message) {
64          this(messageId, message, null);
65      }
66  
67      /**
68       * Create a new PendingMessage.
69       * 
70       * @param messageId
71       *            The id assigned to the message.
72       * @param message
73       *            The sent message.
74       * @param replyCallback
75       *            The callback for the reply to the message.
76       * 
77       */
78      public PendingMessage(final int messageId, final Message message,
79              final ReplyCallback replyCallback) {
80          myMessageId = messageId;
81          myMessage = message;
82          myReplyCallback = replyCallback;
83      }
84  
85      /**
86       * Clears the state of the message allowing the referenced objects to be
87       * garbage collected.
88       */
89      public void clear() {
90          myTimestamp = 0;
91          myMessageId = 0;
92          myMessage = null;
93          myReplyCallback = null;
94      }
95  
96      /**
97       * Returns the sent message.
98       * 
99       * @return The sent message.
100      */
101     public Message getMessage() {
102         return myMessage;
103     }
104 
105     /**
106      * Returns the message id assigned to the sent message.
107      * 
108      * @return The message id assigned to the sent message.
109      */
110     public int getMessageId() {
111         return myMessageId;
112     }
113 
114     /**
115      * Returns the callback for the reply to the message.
116      * 
117      * @return The callback for the reply to the message.
118      */
119     public ReplyCallback getReplyCallback() {
120         return myReplyCallback;
121     }
122 
123     /**
124      * Determines the latency of the message in nano-seconds. If the message
125      * does not have a time stamp then zero is returned.
126      * 
127      * @return The current latency for the message.
128      */
129     public long latency() {
130         final long timestamp = myTimestamp;
131 
132         if (timestamp == 0) {
133             return 0;
134         }
135 
136         return System.nanoTime() - timestamp;
137     }
138 
139     /**
140      * Sets the state of the pending message.
141      * 
142      * @param messageId
143      *            The id of the sent message.
144      * @param message
145      *            The sent message.
146      * @param replyCallback
147      *            The callback for the message. May be null.
148      */
149     public void set(final int messageId, final Message message,
150             final ReplyCallback replyCallback) {
151         myMessageId = messageId;
152         myMessage = message;
153         myReplyCallback = replyCallback;
154         myTimestamp = System.nanoTime();
155     }
156 
157     /**
158      * Sets the state of the pending message.
159      * 
160      * @param other
161      *            The pending message to copy from.
162      */
163     public void set(final PendingMessage other) {
164         myMessageId = other.getMessageId();
165         myMessage = other.getMessage();
166         myReplyCallback = other.getReplyCallback();
167         myTimestamp = other.myTimestamp;
168     }
169 
170     /**
171      * Sets the time stamp of the message to now.
172      */
173     public void timestampNow() {
174         myTimestamp = System.nanoTime();
175     }
176 }