• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 /**
34  * Abstract base interface for protocol-buffer-based RPC services. Services themselves are abstract
35  * classes (implemented either by servers or as stubs), but they subclass this base interface. The
36  * methods of this interface can be used to call the methods of the service without knowing its
37  * exact type at compile time (analogous to the Message interface).
38  *
39  * <p>Starting with version 2.3.0, RPC implementations should not try to build on this, but should
40  * instead provide code generator plugins which generate code specific to the particular RPC
41  * implementation. This way the generated code can be more appropriate for the implementation in use
42  * and can avoid unnecessary layers of indirection.
43  *
44  * @author kenton@google.com Kenton Varda
45  */
46 public interface Service {
47   /** Get the {@code ServiceDescriptor} describing this service and its methods. */
getDescriptorForType()48   Descriptors.ServiceDescriptor getDescriptorForType();
49 
50   /**
51    * Call a method of the service specified by MethodDescriptor. This is normally implemented as a
52    * simple {@code switch()} that calls the standard definitions of the service's methods.
53    *
54    * <p>Preconditions:
55    *
56    * <ul>
57    *   <li>{@code method.getService() == getDescriptorForType()}
58    *   <li>{@code request} is of the exact same class as the object returned by {@code
59    *       getRequestPrototype(method)}.
60    *   <li>{@code controller} is of the correct type for the RPC implementation being used by this
61    *       Service. For stubs, the "correct type" depends on the RpcChannel which the stub is using.
62    *       Server-side Service implementations are expected to accept whatever type of {@code
63    *       RpcController} the server-side RPC implementation uses.
64    * </ul>
65    *
66    * <p>Postconditions:
67    *
68    * <ul>
69    *   <li>{@code done} will be called when the method is complete. This may be before {@code
70    *       callMethod()} returns or it may be at some point in the future.
71    *   <li>The parameter to {@code done} is the response. It must be of the exact same type as would
72    *       be returned by {@code getResponsePrototype(method)}.
73    *   <li>If the RPC failed, the parameter to {@code done} will be {@code null}. Further details
74    *       about the failure can be found by querying {@code controller}.
75    * </ul>
76    */
callMethod( Descriptors.MethodDescriptor method, RpcController controller, Message request, RpcCallback<Message> done)77   void callMethod(
78       Descriptors.MethodDescriptor method,
79       RpcController controller,
80       Message request,
81       RpcCallback<Message> done);
82 
83   /**
84    * {@code callMethod()} requires that the request passed in is of a particular subclass of {@code
85    * Message}. {@code getRequestPrototype()} gets the default instances of this type for a given
86    * method. You can then call {@code Message.newBuilderForType()} on this instance to construct a
87    * builder to build an object which you can then pass to {@code callMethod()}.
88    *
89    * <p>Example:
90    *
91    * <pre>
92    *   MethodDescriptor method =
93    *     service.getDescriptorForType().findMethodByName("Foo");
94    *   Message request =
95    *     stub.getRequestPrototype(method).newBuilderForType()
96    *         .mergeFrom(input).build();
97    *   service.callMethod(method, request, callback);
98    * </pre>
99    */
getRequestPrototype(Descriptors.MethodDescriptor method)100   Message getRequestPrototype(Descriptors.MethodDescriptor method);
101 
102   /**
103    * Like {@code getRequestPrototype()}, but gets a prototype of the response message. {@code
104    * getResponsePrototype()} is generally not needed because the {@code Service} implementation
105    * constructs the response message itself, but it may be useful in some cases to know ahead of
106    * time what type of object will be returned.
107    */
getResponsePrototype(Descriptors.MethodDescriptor method)108   Message getResponsePrototype(Descriptors.MethodDescriptor method);
109 }
110