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         return content().equals(sctpFrame.content());
139     }
140 
141     @Override
142     public int hashCode() {
143         int result = streamIdentifier;
144         result = 31 * result + protocolIdentifier;
145         // values 1231 and 1237 are referenced in the javadocs of Boolean#hashCode()
146         result = 31 * result + (unordered ? 1231 : 1237);
147         result = 31 * result + content().hashCode();
148         return result;
149     }
150 
151     @Override
152     public SctpMessage copy() {
153         return (SctpMessage) super.copy();
154     }
155 
156     @Override
157     public SctpMessage duplicate() {
158         return (SctpMessage) super.duplicate();
159     }
160 
161     @Override
162     public SctpMessage retainedDuplicate() {
163         return (SctpMessage) super.retainedDuplicate();
164     }
165 
166     @Override
167     public SctpMessage replace(ByteBuf content) {
168         if (msgInfo == null) {
169             return new SctpMessage(protocolIdentifier, streamIdentifier, unordered, content);
170         } else {
171             return new SctpMessage(msgInfo, content);
172         }
173     }
174 
175     @Override
176     public SctpMessage retain() {
177         super.retain();
178         return this;
179     }
180 
181     @Override
182     public SctpMessage retain(int increment) {
183         super.retain(increment);
184         return this;
185     }
186 
187     @Override
188     public SctpMessage touch() {
189         super.touch();
190         return this;
191     }
192 
193     @Override
194     public SctpMessage touch(Object hint) {
195         super.touch(hint);
196         return this;
197     }
198 
199     @Override
200     public String toString() {
201         return "SctpFrame{" +
202                "streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
203                ", unordered=" + unordered +
204                ", data=" + contentToString() + '}';
205     }
206 }