View Javadoc
1   /*
2    * Copyright 2011 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.sctp;
17  
18  import com.sun.nio.sctp.MessageInfo;
19  import io.netty.buffer.ByteBuf;
20  import io.netty.buffer.DefaultByteBufHolder;
21  
22  /**
23   * Representation of SCTP Data Chunk
24   */
25  public final class SctpMessage extends DefaultByteBufHolder {
26      private final int streamIdentifier;
27      private final int protocolIdentifier;
28      private final boolean unordered;
29  
30      private final MessageInfo msgInfo;
31  
32      /**
33       * Essential data that is being carried within SCTP Data Chunk
34       * @param protocolIdentifier of payload
35       * @param streamIdentifier that you want to send the payload
36       * @param payloadBuffer channel buffer
37       */
38      public SctpMessage(int protocolIdentifier, int streamIdentifier, ByteBuf payloadBuffer) {
39          this(protocolIdentifier, streamIdentifier, false, payloadBuffer);
40      }
41  
42      /**
43       * Essential data that is being carried within SCTP Data Chunk
44       * @param protocolIdentifier of payload
45       * @param streamIdentifier that you want to send the payload
46       * @param unordered if {@literal true}, the SCTP Data Chunk will be sent with the U (unordered) flag set.
47       * @param payloadBuffer channel buffer
48       */
49      public SctpMessage(int protocolIdentifier, int streamIdentifier, boolean unordered, ByteBuf payloadBuffer) {
50          super(payloadBuffer);
51          this.protocolIdentifier = protocolIdentifier;
52          this.streamIdentifier = streamIdentifier;
53          this.unordered = unordered;
54          msgInfo = null;
55      }
56  
57      /**
58       * Essential data that is being carried within SCTP Data Chunk
59       * @param msgInfo       the {@link MessageInfo}
60       * @param payloadBuffer channel buffer
61       */
62      public SctpMessage(MessageInfo msgInfo, ByteBuf payloadBuffer) {
63          super(payloadBuffer);
64          if (msgInfo == null) {
65              throw new NullPointerException("msgInfo");
66          }
67          this.msgInfo = msgInfo;
68          streamIdentifier = msgInfo.streamNumber();
69          protocolIdentifier = msgInfo.payloadProtocolID();
70          unordered = msgInfo.isUnordered();
71      }
72  
73      /**
74       * Return the stream-identifier
75       */
76      public int streamIdentifier() {
77          return streamIdentifier;
78      }
79  
80      /**
81       * Return the protocol-identifier
82       */
83      public int protocolIdentifier() {
84          return protocolIdentifier;
85      }
86  
87      /**
88       * return the unordered flag
89       */
90      public boolean isUnordered() {
91          return unordered;
92      }
93  
94      /**
95       * Return the {@link MessageInfo} for inbound messages or {@code null} for
96       * outbound messages.
97       */
98      public MessageInfo messageInfo() {
99          return msgInfo;
100     }
101 
102     /**
103      * Return {@code true} if this message is complete.
104      */
105     public boolean isComplete() {
106         if (msgInfo != null) {
107             return msgInfo.isComplete();
108         }  else {
109             //all outbound sctp messages are complete
110             return true;
111         }
112     }
113 
114     @Override
115     public boolean equals(Object o) {
116         if (this == o) {
117             return true;
118         }
119 
120         if (o == null || getClass() != o.getClass()) {
121             return false;
122         }
123 
124         SctpMessage sctpFrame = (SctpMessage) o;
125 
126         if (protocolIdentifier != sctpFrame.protocolIdentifier) {
127             return false;
128         }
129 
130         if (streamIdentifier != sctpFrame.streamIdentifier) {
131             return false;
132         }
133 
134         if (unordered != sctpFrame.unordered) {
135             return false;
136         }
137 
138         if (!content().equals(sctpFrame.content())) {
139             return false;
140         }
141 
142         return true;
143     }
144 
145     @Override
146     public int hashCode() {
147         int result = streamIdentifier;
148         result = 31 * result + protocolIdentifier;
149         // values 1231 and 1237 are referenced in the javadocs of Boolean#hashCode()
150         result = 31 * result + (unordered ? 1231 : 1237);
151         result = 31 * result + content().hashCode();
152         return result;
153     }
154 
155     @Override
156     public SctpMessage copy() {
157         if (msgInfo == null) {
158             return new SctpMessage(protocolIdentifier, streamIdentifier, unordered, content().copy());
159         } else {
160             return new SctpMessage(msgInfo, content().copy());
161         }
162     }
163 
164     @Override
165     public SctpMessage duplicate() {
166         if (msgInfo == null) {
167             return new SctpMessage(protocolIdentifier, streamIdentifier, unordered, content().duplicate());
168         } else {
169             return new SctpMessage(msgInfo, content().duplicate());
170         }
171     }
172 
173     @Override
174     public SctpMessage retain() {
175         super.retain();
176         return this;
177     }
178 
179     @Override
180     public SctpMessage retain(int increment) {
181         super.retain(increment);
182         return this;
183     }
184 
185     @Override
186     public String toString() {
187         return "SctpFrame{" +
188                "streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
189                ", unordered=" + unordered +
190                ", data=" + contentToString() + '}';
191     }
192 }