View Javadoc
1   /*
2    * Copyright 2016 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  
17  package io.netty.handler.codec.http2;
18  
19  import io.netty.buffer.ByteBuf;
20  import io.netty.buffer.DefaultByteBufHolder;
21  import io.netty.util.internal.StringUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  /**
25   * The default {@link Http2PingFrame} implementation.
26   */
27  @UnstableApi
28  public class DefaultHttp2PingFrame extends DefaultByteBufHolder implements Http2PingFrame {
29  
30      private final boolean ack;
31  
32      public DefaultHttp2PingFrame(ByteBuf content) {
33          this(content, false);
34      }
35  
36      /**
37       * A user cannot send a ping ack, as this is done automatically when a ping is received.
38       */
39      DefaultHttp2PingFrame(ByteBuf content, boolean ack) {
40          super(mustBeEightBytes(content));
41          this.ack = ack;
42      }
43  
44      @Override
45      public boolean ack() {
46          return ack;
47      }
48  
49      @Override
50      public String name() {
51          return "PING";
52      }
53  
54      @Override
55      public DefaultHttp2PingFrame copy() {
56          return replace(content().copy());
57      }
58  
59      @Override
60      public DefaultHttp2PingFrame duplicate() {
61          return replace(content().duplicate());
62      }
63  
64      @Override
65      public DefaultHttp2PingFrame retainedDuplicate() {
66          return replace(content().retainedDuplicate());
67      }
68  
69      @Override
70      public DefaultHttp2PingFrame replace(ByteBuf content) {
71          return new DefaultHttp2PingFrame(content, ack);
72      }
73  
74      @Override
75      public DefaultHttp2PingFrame retain() {
76          super.retain();
77          return this;
78      }
79  
80      @Override
81      public DefaultHttp2PingFrame retain(int increment) {
82          super.retain(increment);
83          return this;
84      }
85  
86      @Override
87      public DefaultHttp2PingFrame touch() {
88          super.touch();
89          return this;
90      }
91  
92      @Override
93      public DefaultHttp2PingFrame touch(Object hint) {
94          super.touch(hint);
95          return this;
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (!(o instanceof Http2PingFrame)) {
101             return false;
102         }
103         Http2PingFrame other = (Http2PingFrame) o;
104         return super.equals(o) && ack == other.ack();
105     }
106 
107     @Override
108     public int hashCode() {
109         int hash = super.hashCode();
110         hash = hash * 31 + (ack ? 1 : 0);
111         return hash;
112     }
113 
114     private static ByteBuf mustBeEightBytes(ByteBuf content) {
115         if (content.readableBytes() != 8) {
116             throw new IllegalArgumentException("PING frames require 8 bytes of content. Was " +
117                                                content.readableBytes() + " bytes.");
118         }
119         return content;
120     }
121 
122     @Override
123     public String toString() {
124         return StringUtil.simpleClassName(this) + "(content=" + contentToString() + ", ack=" + ack + ')';
125     }
126 }