View Javadoc
1   /*
2    * #%L
3    * Header.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 java.io.IOException;
23  
24  import com.allanbank.mongodb.bson.io.BsonInputStream;
25  import com.allanbank.mongodb.client.Operation;
26  
27  /**
28   * The header of a message.
29   * 
30   * <pre>
31   * <code>
32   * struct MsgHeader {
33   *     int32   messageLength; // total message size, including this
34   *     int32   requestID;     // identifier for this message
35   *     int32   responseTo;    // requestID from the original request
36   *                            //   (used in reponses from db)
37   *     int32   opCode;        // request type - see table below
38   * }
39   * </code>
40   * </pre>
41   * 
42   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
43   *         mutated in incompatible ways between any two releases of the driver.
44   * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
45   */
46  public class Header {
47  
48      /** The size of a message header. */
49      public static final int SIZE = 16;
50  
51      /**
52       * The length of the message in bytes. This includes the length of the
53       * header (16 bytes).
54       */
55      private final int myLength;
56  
57      /** The operation for the message. */
58      private final Operation myOperation;
59  
60      /** The request id for the message. */
61      private final int myRequestId;
62  
63      /** The response id for the message. */
64      private final int myResponseId;
65  
66      /**
67       * Creates a new header.
68       * 
69       * @param in
70       *            The stream to read the header from.
71       * @throws IOException
72       *             On a failure reading the header.
73       */
74      public Header(final BsonInputStream in) throws IOException {
75          myLength = in.readInt();
76          myRequestId = in.readInt();
77          myResponseId = in.readInt();
78          myOperation = Operation.fromCode(in.readInt());
79      }
80  
81      /**
82       * Creates a new header.
83       * 
84       * @param length
85       *            The length of the message in bytes.
86       * @param requestId
87       *            The request id for the message.
88       * @param responseId
89       *            The response id for the message.
90       * @param operation
91       *            The operation for the message.
92       */
93      public Header(final int length, final int requestId, final int responseId,
94              final Operation operation) {
95          myLength = length;
96          myRequestId = requestId;
97          myResponseId = responseId;
98          myOperation = operation;
99      }
100 
101     /**
102      * Determines if the passed object is of this same type as this object and
103      * if so that its fields are equal.
104      * 
105      * @param object
106      *            The object to compare to.
107      * 
108      * @see java.lang.Object#equals(java.lang.Object)
109      */
110     @Override
111     public boolean equals(final Object object) {
112         boolean result = false;
113         if (this == object) {
114             result = true;
115         }
116         else if ((object != null) && (getClass() == object.getClass())) {
117             final Header other = (Header) object;
118 
119             result = (myLength == other.myLength)
120                     && (myOperation == other.myOperation)
121                     && (myRequestId == other.myRequestId)
122                     && (myResponseId == other.myResponseId);
123         }
124         return result;
125     }
126 
127     /**
128      * Returns the length of the message in bytes. This includes the
129      * {@link #SIZE} of the header.
130      * 
131      * @return The length of the message in bytes.
132      */
133     public int getLength() {
134         return myLength;
135     }
136 
137     /**
138      * Returns the operation for the message.
139      * 
140      * @return The operation for the message.
141      */
142     public Operation getOperation() {
143         return myOperation;
144     }
145 
146     /**
147      * Returns the request id for the message.
148      * 
149      * @return The request id for the message.
150      */
151     public int getRequestId() {
152         return myRequestId;
153     }
154 
155     /**
156      * Returns the response id for the message.
157      * 
158      * @return The response id for the message.
159      */
160     public int getResponseId() {
161         return myResponseId;
162     }
163 
164     /**
165      * Computes a reasonable hash code.
166      * 
167      * @return The hash code value.
168      */
169     @Override
170     public int hashCode() {
171         int result = 1;
172         result = (31 * result) + myLength;
173         result = (31 * result) + myRequestId;
174         result = (31 * result) + myResponseId;
175         result = (31 * result) + myOperation.hashCode();
176         return result;
177     }
178 }