BWAPI
Undermind/proxy/cpp/include/google/protobuf/service.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 //
00035 // DEPRECATED:  This module declares the abstract interfaces underlying proto2
00036 // RPC services.  These are intented to be independent of any particular RPC
00037 // implementation, so that proto2 services can be used on top of a variety
00038 // of implementations.  Starting with version 2.3.0, RPC implementations should
00039 // not try to build on these, but should instead provide code generator plugins
00040 // which generate code specific to the particular RPC implementation.  This way
00041 // the generated code can be more appropriate for the implementation in use
00042 // and can avoid unnecessary layers of indirection.
00043 //
00044 //
00045 // When you use the protocol compiler to compile a service definition, it
00046 // generates two classes:  An abstract interface for the service (with
00047 // methods matching the service definition) and a "stub" implementation.
00048 // A stub is just a type-safe wrapper around an RpcChannel which emulates a
00049 // local implementation of the service.
00050 //
00051 // For example, the service definition:
00052 //   service MyService {
00053 //     rpc Foo(MyRequest) returns(MyResponse);
00054 //   }
00055 // will generate abstract interface "MyService" and class "MyService::Stub".
00056 // You could implement a MyService as follows:
00057 //   class MyServiceImpl : public MyService {
00058 //    public:
00059 //     MyServiceImpl() {}
00060 //     ~MyServiceImpl() {}
00061 //
00062 //     // implements MyService ---------------------------------------
00063 //
00064 //     void Foo(google::protobuf::RpcController* controller,
00065 //              const MyRequest* request,
00066 //              MyResponse* response,
00067 //              Closure* done) {
00068 //       // ... read request and fill in response ...
00069 //       done->Run();
00070 //     }
00071 //   };
00072 // You would then register an instance of MyServiceImpl with your RPC server
00073 // implementation.  (How to do that depends on the implementation.)
00074 //
00075 // To call a remote MyServiceImpl, first you need an RpcChannel connected to it.
00076 // How to construct a channel depends, again, on your RPC implementation.
00077 // Here we use a hypothentical "MyRpcChannel" as an example:
00078 //   MyRpcChannel channel("rpc:hostname:1234/myservice");
00079 //   MyRpcController controller;
00080 //   MyServiceImpl::Stub stub(&channel);
00081 //   FooRequest request;
00082 //   FooRespnose response;
00083 //
00084 //   // ... fill in request ...
00085 //
00086 //   stub.Foo(&controller, request, &response, NewCallback(HandleResponse));
00087 //
00088 // On Thread-Safety:
00089 //
00090 // Different RPC implementations may make different guarantees about what
00091 // threads they may run callbacks on, and what threads the application is
00092 // allowed to use to call the RPC system.  Portable software should be ready
00093 // for callbacks to be called on any thread, but should not try to call the
00094 // RPC system from any thread except for the ones on which it received the
00095 // callbacks.  Realistically, though, simple software will probably want to
00096 // use a single-threaded RPC system while high-end software will want to
00097 // use multiple threads.  RPC implementations should provide multiple
00098 // choices.
00099 
00100 #ifndef GOOGLE_PROTOBUF_SERVICE_H__
00101 #define GOOGLE_PROTOBUF_SERVICE_H__
00102 
00103 #include <string>
00104 #include <google/protobuf/stubs/common.h>
00105 
00106 namespace google {
00107 namespace protobuf {
00108 
00109 // Defined in this file.
00110 class Service;
00111 class RpcController;
00112 class RpcChannel;
00113 
00114 // Defined in other files.
00115 class Descriptor;            // descriptor.h
00116 class ServiceDescriptor;     // descriptor.h
00117 class MethodDescriptor;      // descriptor.h
00118 class Message;               // message.h
00119 
00120 // Abstract base interface for protocol-buffer-based RPC services.  Services
00121 // themselves are abstract interfaces (implemented either by servers or as
00122 // stubs), but they subclass this base interface.  The methods of this
00123 // interface can be used to call the methods of the Service without knowing
00124 // its exact type at compile time (analogous to Reflection).
00125 class LIBPROTOBUF_EXPORT Service {
00126  public:
00127   inline Service() {}
00128   virtual ~Service();
00129 
00130   // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second
00131   // parameter to the constructor to tell it to delete its RpcChannel when
00132   // destroyed.
00133   enum ChannelOwnership {
00134     STUB_OWNS_CHANNEL,
00135     STUB_DOESNT_OWN_CHANNEL
00136   };
00137 
00138   // Get the ServiceDescriptor describing this service and its methods.
00139   virtual const ServiceDescriptor* GetDescriptor() = 0;
00140 
00141   // Call a method of the service specified by MethodDescriptor.  This is
00142   // normally implemented as a simple switch() that calls the standard
00143   // definitions of the service's methods.
00144   //
00145   // Preconditions:
00146   // * method->service() == GetDescriptor()
00147   // * request and response are of the exact same classes as the objects
00148   //   returned by GetRequestPrototype(method) and
00149   //   GetResponsePrototype(method).
00150   // * After the call has started, the request must not be modified and the
00151   //   response must not be accessed at all until "done" is called.
00152   // * "controller" is of the correct type for the RPC implementation being
00153   //   used by this Service.  For stubs, the "correct type" depends on the
00154   //   RpcChannel which the stub is using.  Server-side Service
00155   //   implementations are expected to accept whatever type of RpcController
00156   //   the server-side RPC implementation uses.
00157   //
00158   // Postconditions:
00159   // * "done" will be called when the method is complete.  This may be
00160   //   before CallMethod() returns or it may be at some point in the future.
00161   // * If the RPC succeeded, "response" contains the response returned by
00162   //   the server.
00163   // * If the RPC failed, "response"'s contents are undefined.  The
00164   //   RpcController can be queried to determine if an error occurred and
00165   //   possibly to get more information about the error.
00166   virtual void CallMethod(const MethodDescriptor* method,
00167                           RpcController* controller,
00168                           const Message* request,
00169                           Message* response,
00170                           Closure* done) = 0;
00171 
00172   // CallMethod() requires that the request and response passed in are of a
00173   // particular subclass of Message.  GetRequestPrototype() and
00174   // GetResponsePrototype() get the default instances of these required types.
00175   // You can then call Message::New() on these instances to construct mutable
00176   // objects which you can then pass to CallMethod().
00177   //
00178   // Example:
00179   //   const MethodDescriptor* method =
00180   //     service->GetDescriptor()->FindMethodByName("Foo");
00181   //   Message* request  = stub->GetRequestPrototype (method)->New();
00182   //   Message* response = stub->GetResponsePrototype(method)->New();
00183   //   request->ParseFromString(input);
00184   //   service->CallMethod(method, *request, response, callback);
00185   virtual const Message& GetRequestPrototype(
00186     const MethodDescriptor* method) const = 0;
00187   virtual const Message& GetResponsePrototype(
00188     const MethodDescriptor* method) const = 0;
00189 
00190  private:
00191   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Service);
00192 };
00193 
00194 // An RpcController mediates a single method call.  The primary purpose of
00195 // the controller is to provide a way to manipulate settings specific to the
00196 // RPC implementation and to find out about RPC-level errors.
00197 //
00198 // The methods provided by the RpcController interface are intended to be a
00199 // "least common denominator" set of features which we expect all
00200 // implementations to support.  Specific implementations may provide more
00201 // advanced features (e.g. deadline propagation).
00202 class LIBPROTOBUF_EXPORT RpcController {
00203  public:
00204   inline RpcController() {}
00205   virtual ~RpcController();
00206 
00207   // Client-side methods ---------------------------------------------
00208   // These calls may be made from the client side only.  Their results
00209   // are undefined on the server side (may crash).
00210 
00211   // Resets the RpcController to its initial state so that it may be reused in
00212   // a new call.  Must not be called while an RPC is in progress.
00213   virtual void Reset() = 0;
00214 
00215   // After a call has finished, returns true if the call failed.  The possible
00216   // reasons for failure depend on the RPC implementation.  Failed() must not
00217   // be called before a call has finished.  If Failed() returns true, the
00218   // contents of the response message are undefined.
00219   virtual bool Failed() const = 0;
00220 
00221   // If Failed() is true, returns a human-readable description of the error.
00222   virtual string ErrorText() const = 0;
00223 
00224   // Advises the RPC system that the caller desires that the RPC call be
00225   // canceled.  The RPC system may cancel it immediately, may wait awhile and
00226   // then cancel it, or may not even cancel the call at all.  If the call is
00227   // canceled, the "done" callback will still be called and the RpcController
00228   // will indicate that the call failed at that time.
00229   virtual void StartCancel() = 0;
00230 
00231   // Server-side methods ---------------------------------------------
00232   // These calls may be made from the server side only.  Their results
00233   // are undefined on the client side (may crash).
00234 
00235   // Causes Failed() to return true on the client side.  "reason" will be
00236   // incorporated into the message returned by ErrorText().  If you find
00237   // you need to return machine-readable information about failures, you
00238   // should incorporate it into your response protocol buffer and should
00239   // NOT call SetFailed().
00240   virtual void SetFailed(const string& reason) = 0;
00241 
00242   // If true, indicates that the client canceled the RPC, so the server may
00243   // as well give up on replying to it.  The server should still call the
00244   // final "done" callback.
00245   virtual bool IsCanceled() const = 0;
00246 
00247   // Asks that the given callback be called when the RPC is canceled.  The
00248   // callback will always be called exactly once.  If the RPC completes without
00249   // being canceled, the callback will be called after completion.  If the RPC
00250   // has already been canceled when NotifyOnCancel() is called, the callback
00251   // will be called immediately.
00252   //
00253   // NotifyOnCancel() must be called no more than once per request.
00254   virtual void NotifyOnCancel(Closure* callback) = 0;
00255 
00256  private:
00257   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
00258 };
00259 
00260 // Abstract interface for an RPC channel.  An RpcChannel represents a
00261 // communication line to a Service which can be used to call that Service's
00262 // methods.  The Service may be running on another machine.  Normally, you
00263 // should not call an RpcChannel directly, but instead construct a stub Service
00264 // wrapping it.  Example:
00265 //   RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234");
00266 //   MyService* service = new MyService::Stub(channel);
00267 //   service->MyMethod(request, &response, callback);
00268 class LIBPROTOBUF_EXPORT RpcChannel {
00269  public:
00270   inline RpcChannel() {}
00271   virtual ~RpcChannel();
00272 
00273   // Call the given method of the remote service.  The signature of this
00274   // procedure looks the same as Service::CallMethod(), but the requirements
00275   // are less strict in one important way:  the request and response objects
00276   // need not be of any specific class as long as their descriptors are
00277   // method->input_type() and method->output_type().
00278   virtual void CallMethod(const MethodDescriptor* method,
00279                           RpcController* controller,
00280                           const Message* request,
00281                           Message* response,
00282                           Closure* done) = 0;
00283 
00284  private:
00285   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel);
00286 };
00287 
00288 }  // namespace protobuf
00289 
00290 }  // namespace google
00291 #endif  // GOOGLE_PROTOBUF_SERVICE_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines