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; 18 19 import javax.annotation.Nullable; 20 21 /** 22 * Encapsulates a single call received from a remote client. Calls may not simply be unary 23 * request-response even though this is the most common pattern. Calls may stream any number of 24 * requests and responses. This API is generally intended for use by generated handlers, 25 * but applications may use it directly if they need to. 26 * 27 * <p>Headers must be sent before any messages, which must be sent before closing. 28 * 29 * <p>No generic method for determining message receipt or providing acknowledgement is provided. 30 * Applications are expected to utilize normal messages for such signals, as a response 31 * naturally acknowledges its request. 32 * 33 * <p>Methods are guaranteed to be non-blocking. Implementations are not required to be thread-safe. 34 * 35 * <p>DO NOT MOCK: Use InProcessTransport and make a fake server instead. 36 * 37 * @param <ReqT> parsed type of request message. 38 * @param <RespT> parsed type of response message. 39 */ 40 public abstract class ServerCall<ReqT, RespT> { 41 42 /** 43 * Callbacks for consuming incoming RPC messages. 44 * 45 * <p>Any contexts are guaranteed to arrive before any messages, which are guaranteed before half 46 * close, which is guaranteed before completion. 47 * 48 * <p>Implementations are free to block for extended periods of time. Implementations are not 49 * required to be thread-safe, but they must not be thread-hostile. The caller is free to call 50 * an instance from multiple threads, but only one call simultaneously. A single thread may 51 * interleave calls to multiple instances, so implementations using ThreadLocals must be careful 52 * to avoid leaking inappropriate state (e.g., clearing the ThreadLocal before returning). 53 */ 54 // TODO(ejona86): We need to decide what to do in the case of server closing with non-cancellation 55 // before client half closes. It may be that we treat such a case as an error. If we permit such 56 // a case then we either get to generate a half close or purposefully omit it. 57 public abstract static class Listener<ReqT> { 58 /** 59 * A request message has been received. For streaming calls, there may be zero or more request 60 * messages. 61 * 62 * @param message a received request message. 63 */ onMessage(ReqT message)64 public void onMessage(ReqT message) {} 65 66 /** 67 * The client completed all message sending. However, the call may still be cancelled. 68 */ onHalfClose()69 public void onHalfClose() {} 70 71 /** 72 * The call was cancelled and the server is encouraged to abort processing to save resources, 73 * since the client will not process any further messages. Cancellations can be caused by 74 * timeouts, explicit cancellation by the client, network errors, etc. 75 * 76 * <p>There will be no further callbacks for the call. 77 */ onCancel()78 public void onCancel() {} 79 80 /** 81 * The call is considered complete and {@link #onCancel} is guaranteed not to be called. 82 * However, the client is not guaranteed to have received all messages. 83 * 84 * <p>There will be no further callbacks for the call. 85 */ onComplete()86 public void onComplete() {} 87 88 /** 89 * This indicates that the call may now be capable of sending additional messages (via 90 * {@link #sendMessage}) without requiring excessive buffering internally. This event is 91 * just a suggestion and the application is free to ignore it, however doing so may 92 * result in excessive buffering within the call. 93 * 94 * <p>Because there is a processing delay to deliver this notification, it is possible for 95 * concurrent writes to cause {@code isReady() == false} within this callback. Handle "spurious" 96 * notifications by checking {@code isReady()}'s current value instead of assuming it is now 97 * {@code true}. If {@code isReady() == false} the normal expectations apply, so there would be 98 * <em>another</em> {@code onReady()} callback. 99 */ onReady()100 public void onReady() {} 101 } 102 103 /** 104 * Requests up to the given number of messages from the call to be delivered to 105 * {@link Listener#onMessage(Object)}. Once {@code numMessages} have been delivered 106 * no further request messages will be delivered until more messages are requested by 107 * calling this method again. 108 * 109 * <p>Servers use this mechanism to provide back-pressure to the client for flow-control. 110 * 111 * <p>This method is safe to call from multiple threads without external synchronization. 112 * 113 * @param numMessages the requested number of messages to be delivered to the listener. 114 */ request(int numMessages)115 public abstract void request(int numMessages); 116 117 /** 118 * Send response header metadata prior to sending a response message. This method may 119 * only be called once and cannot be called after calls to {@link #sendMessage} or {@link #close}. 120 * 121 * <p>Since {@link Metadata} is not thread-safe, the caller must not access (read or write) {@code 122 * headers} after this point. 123 * 124 * @param headers metadata to send prior to any response body. 125 * @throws IllegalStateException if {@code close} has been called, a message has been sent, or 126 * headers have already been sent 127 */ sendHeaders(Metadata headers)128 public abstract void sendHeaders(Metadata headers); 129 130 /** 131 * Send a response message. Messages are the primary form of communication associated with 132 * RPCs. Multiple response messages may exist for streaming calls. 133 * 134 * @param message response message. 135 * @throws IllegalStateException if headers not sent or call is {@link #close}d 136 */ sendMessage(RespT message)137 public abstract void sendMessage(RespT message); 138 139 /** 140 * If {@code true}, indicates that the call is capable of sending additional messages 141 * without requiring excessive buffering internally. This event is 142 * just a suggestion and the application is free to ignore it, however doing so may 143 * result in excessive buffering within the call. 144 * 145 * <p>If {@code false}, {@link Listener#onReady()} will be called after {@code isReady()} 146 * transitions to {@code true}. 147 * 148 * <p>This abstract class's implementation always returns {@code true}. Implementations generally 149 * override the method. 150 */ isReady()151 public boolean isReady() { 152 return true; 153 } 154 155 /** 156 * Close the call with the provided status. No further sending or receiving will occur. If {@link 157 * Status#isOk} is {@code false}, then the call is said to have failed. 158 * 159 * <p>If no errors or cancellations are known to have occurred, then a {@link Listener#onComplete} 160 * notification should be expected, independent of {@code status}. Otherwise {@link 161 * Listener#onCancel} has been or will be called. 162 * 163 * <p>Since {@link Metadata} is not thread-safe, the caller must not access (read or write) {@code 164 * trailers} after this point. 165 * 166 * <p>This method implies the caller completed processing the RPC, but it does not imply the RPC 167 * is complete. The call implementation will need additional time to complete the RPC and during 168 * this time the client is still able to cancel the request or a network error might cause the 169 * RPC to fail. If you wish to know when the call is actually completed/closed, you have to use 170 * {@link Listener#onComplete} or {@link Listener#onCancel} instead. This method is not 171 * necessarily invoked when Listener.onCancel() is called. 172 * 173 * @throws IllegalStateException if call is already {@code close}d 174 */ close(Status status, Metadata trailers)175 public abstract void close(Status status, Metadata trailers); 176 177 /** 178 * Returns {@code true} when the call is cancelled and the server is encouraged to abort 179 * processing to save resources, since the client will not be processing any further methods. 180 * Cancellations can be caused by timeouts, explicit cancel by client, network errors, and 181 * similar. 182 * 183 * <p>This method may safely be called concurrently from multiple threads. 184 */ isCancelled()185 public abstract boolean isCancelled(); 186 187 /** 188 * Enables per-message compression, if an encoding type has been negotiated. If no message 189 * encoding has been negotiated, this is a no-op. By default per-message compression is enabled, 190 * but may not have any effect if compression is not enabled on the call. 191 */ setMessageCompression(boolean enabled)192 public void setMessageCompression(boolean enabled) { 193 // noop 194 } 195 196 /** 197 * Sets the compression algorithm for this call. This compression is utilized for sending. If 198 * the server does not support the compression algorithm, the call will fail. This method may 199 * only be called before {@link #sendHeaders}. The compressor to use will be looked up in the 200 * {@link CompressorRegistry}. Default gRPC servers support the "gzip" compressor. 201 * 202 * <p>It is safe to call this even if the client does not support the compression format chosen. 203 * The implementation will handle negotiation with the client and may fall back to no compression. 204 * 205 * @param compressor the name of the compressor to use. 206 * @throws IllegalArgumentException if the compressor name can not be found. 207 */ setCompression(String compressor)208 public void setCompression(String compressor) { 209 // noop 210 } 211 212 /** 213 * Returns the level of security guarantee in communications 214 * 215 * <p>Determining the level of security offered by the transport for RPCs on server-side. 216 * This can be approximated by looking for the SSLSession, but that doesn't work for ALTS and 217 * maybe some future TLS approaches. May return a lower security level when it cannot be 218 * determined precisely. 219 * 220 * @return non-{@code null} SecurityLevel enum 221 */ 222 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4692") getSecurityLevel()223 public SecurityLevel getSecurityLevel() { 224 return SecurityLevel.NONE; 225 } 226 227 /** 228 * Returns properties of a single call. 229 * 230 * <p>Attributes originate from the transport and can be altered by {@link ServerTransportFilter}. 231 * 232 * @return non-{@code null} Attributes container 233 */ 234 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1779") 235 @Grpc.TransportAttr getAttributes()236 public Attributes getAttributes() { 237 return Attributes.EMPTY; 238 } 239 240 /** 241 * Gets the authority this call is addressed to. 242 * 243 * @return the authority string. {@code null} if not available. 244 */ 245 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2924") 246 @Nullable getAuthority()247 public String getAuthority() { 248 return null; 249 } 250 251 /** 252 * The {@link MethodDescriptor} for the call. 253 */ getMethodDescriptor()254 public abstract MethodDescriptor<ReqT, RespT> getMethodDescriptor(); 255 } 256