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.ByteBufUtil;
21  import io.netty.buffer.DefaultByteBufHolder;
22  
23  /**
24   * Representation of SCTP Data Chunk
25   */
26  public final class SctpMessage extends DefaultByteBufHolder {
27      private final int streamIdentifier;
28      private final int protocolIdentifier;
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          super(payloadBuffer);
40          this.protocolIdentifier = protocolIdentifier;
41          this.streamIdentifier = streamIdentifier;
42          msgInfo = null;
43      }
44  
45      /**
46       * Essential data that is being carried within SCTP Data Chunk
47       * @param msgInfo       the {@link MessageInfo}
48       * @param payloadBuffer channel buffer
49       */
50      public SctpMessage(MessageInfo msgInfo, ByteBuf payloadBuffer) {
51          super(payloadBuffer);
52          if (msgInfo == null) {
53              throw new NullPointerException("msgInfo");
54          }
55          this.msgInfo = msgInfo;
56          streamIdentifier = msgInfo.streamNumber();
57          protocolIdentifier = msgInfo.payloadProtocolID();
58      }
59  
60      /**
61       * Return the stream-identifier
62       */
63      public int streamIdentifier() {
64          return streamIdentifier;
65      }
66  
67      /**
68       * Return the protocol-identifier
69       */
70      public int protocolIdentifier() {
71          return protocolIdentifier;
72      }
73  
74      /**
75       * Return the {@link MessageInfo} for inbound messages or {@code null} for
76       * outbound messages.
77       */
78      public MessageInfo messageInfo() {
79          return msgInfo;
80      }
81  
82      /**
83       * Return {@code true} if this message is complete.
84       */
85      public boolean isComplete() {
86          if (msgInfo != null) {
87              return msgInfo.isComplete();
88          }  else {
89              //all outbound sctp messages are complete
90              return true;
91          }
92      }
93  
94      @Override
95      public boolean equals(Object o) {
96          if (this == o) {
97              return true;
98          }
99  
100         if (o == null || getClass() != o.getClass()) {
101             return false;
102         }
103 
104         SctpMessage sctpFrame = (SctpMessage) o;
105 
106         if (protocolIdentifier != sctpFrame.protocolIdentifier) {
107             return false;
108         }
109 
110         if (streamIdentifier != sctpFrame.streamIdentifier) {
111             return false;
112         }
113 
114         return content().equals(sctpFrame.content());
115     }
116 
117     @Override
118     public int hashCode() {
119         int result = streamIdentifier;
120         result = 31 * result + protocolIdentifier;
121         result = 31 * result + content().hashCode();
122         return result;
123     }
124 
125     @Override
126     public SctpMessage copy() {
127         if (msgInfo == null) {
128             return new SctpMessage(protocolIdentifier, streamIdentifier, content().copy());
129         } else {
130             return new SctpMessage(msgInfo, content().copy());
131         }
132     }
133 
134     @Override
135     public SctpMessage duplicate() {
136         if (msgInfo == null) {
137             return new SctpMessage(protocolIdentifier, streamIdentifier, content().duplicate());
138         } else {
139             return new SctpMessage(msgInfo, content().copy());
140         }
141     }
142 
143     @Override
144     public SctpMessage retain() {
145         super.retain();
146         return this;
147     }
148 
149     @Override
150     public SctpMessage retain(int increment) {
151         super.retain(increment);
152         return this;
153     }
154 
155     @Override
156     public SctpMessage touch() {
157         super.touch();
158         return this;
159     }
160 
161     @Override
162     public SctpMessage touch(Object hint) {
163         super.touch(hint);
164         return this;
165     }
166 
167     @Override
168     public String toString() {
169         if (refCnt() == 0) {
170             return "SctpFrame{" +
171                     "streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
172                     ", data=(FREED)}";
173         }
174         return "SctpFrame{" +
175                 "streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
176                 ", data=" + ByteBufUtil.hexDump(content()) + '}';
177     }
178 }