1 /* 2 * Copyright 2014 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.netty; 18 19 import com.google.common.base.Objects; 20 import com.google.common.base.Preconditions; 21 import io.grpc.Status; 22 import io.netty.handler.codec.http2.Http2Headers; 23 24 /** 25 * Command sent from the transport to the Netty channel to send response headers to the client. 26 */ 27 final class SendResponseHeadersCommand extends WriteQueue.AbstractQueuedCommand { 28 private final StreamIdHolder stream; 29 private final Http2Headers headers; 30 private final Status status; 31 SendResponseHeadersCommand(StreamIdHolder stream, Http2Headers headers, Status status)32 private SendResponseHeadersCommand(StreamIdHolder stream, Http2Headers headers, Status status) { 33 this.stream = Preconditions.checkNotNull(stream, "stream"); 34 this.headers = Preconditions.checkNotNull(headers, "headers"); 35 this.status = status; 36 } 37 createHeaders(StreamIdHolder stream, Http2Headers headers)38 static SendResponseHeadersCommand createHeaders(StreamIdHolder stream, Http2Headers headers) { 39 return new SendResponseHeadersCommand(stream, headers, null); 40 } 41 createTrailers( StreamIdHolder stream, Http2Headers headers, Status status)42 static SendResponseHeadersCommand createTrailers( 43 StreamIdHolder stream, Http2Headers headers, Status status) { 44 return new SendResponseHeadersCommand( 45 stream, headers, Preconditions.checkNotNull(status, "status")); 46 } 47 stream()48 StreamIdHolder stream() { 49 return stream; 50 } 51 headers()52 Http2Headers headers() { 53 return headers; 54 } 55 endOfStream()56 boolean endOfStream() { 57 return status != null; 58 } 59 status()60 Status status() { 61 return status; 62 } 63 64 @Override equals(Object that)65 public boolean equals(Object that) { 66 if (that == null || !that.getClass().equals(SendResponseHeadersCommand.class)) { 67 return false; 68 } 69 SendResponseHeadersCommand thatCmd = (SendResponseHeadersCommand) that; 70 return thatCmd.stream.equals(stream) 71 && thatCmd.headers.equals(headers) 72 && thatCmd.status.equals(status); 73 } 74 75 @Override toString()76 public String toString() { 77 return getClass().getSimpleName() + "(stream=" + stream.id() + ", headers=" + headers 78 + ", status=" + status + ")"; 79 } 80 81 @Override hashCode()82 public int hashCode() { 83 return Objects.hashCode(stream, status); 84 } 85 } 86