759f6683bf40f02c808a456be2d081549d607e8b
[platform/upstream/grpc.git] / include / grpcpp / impl / codegen / async_generic_service.h
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #ifndef GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
20 #define GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H
21
22 #include <grpcpp/impl/codegen/async_stream.h>
23 #include <grpcpp/impl/codegen/byte_buffer.h>
24 #include <grpcpp/impl/codegen/server_callback.h>
25
26 struct grpc_server;
27
28 namespace grpc {
29
30 typedef ServerAsyncReaderWriter<ByteBuffer, ByteBuffer>
31     GenericServerAsyncReaderWriter;
32 typedef ServerAsyncResponseWriter<ByteBuffer> GenericServerAsyncResponseWriter;
33 typedef ServerAsyncReader<ByteBuffer, ByteBuffer> GenericServerAsyncReader;
34 typedef ServerAsyncWriter<ByteBuffer> GenericServerAsyncWriter;
35
36 class GenericServerContext final : public ServerContext {
37  public:
38   const grpc::string& method() const { return method_; }
39   const grpc::string& host() const { return host_; }
40
41  private:
42   friend class Server;
43   friend class ServerInterface;
44
45   void Clear() {
46     method_.clear();
47     host_.clear();
48     ServerContext::Clear();
49   }
50
51   grpc::string method_;
52   grpc::string host_;
53 };
54
55 // A generic service at the server side accepts all RPC methods and hosts. It is
56 // typically used in proxies. The generic service can be registered to a server
57 // which also has other services.
58 // Sample usage:
59 //   ServerBuilder builder;
60 //   auto cq = builder.AddCompletionQueue();
61 //   AsyncGenericService generic_service;
62 //   builder.RegisterAsyncGenericService(&generic_service);
63 //   auto server = builder.BuildAndStart();
64 //
65 //   // request a new call
66 //   GenericServerContext context;
67 //   GenericServerAsyncReaderWriter stream;
68 //   generic_service.RequestCall(&context, &stream, cq.get(), cq.get(), tag);
69 //
70 // When tag is retrieved from cq->Next(), context.method() can be used to look
71 // at the method and the RPC can be handled accordingly.
72 class AsyncGenericService final {
73  public:
74   AsyncGenericService() : server_(nullptr) {}
75
76   void RequestCall(GenericServerContext* ctx,
77                    GenericServerAsyncReaderWriter* reader_writer,
78                    CompletionQueue* call_cq,
79                    ServerCompletionQueue* notification_cq, void* tag);
80
81  private:
82   friend class Server;
83   Server* server_;
84 };
85
86 namespace experimental {
87
88 /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs
89 /// invoked on a CallbackGenericService. The API difference relative to
90 /// ServerBidiReactor is that the argument to OnStarted is a
91 /// GenericServerContext rather than a ServerContext. All other reaction and
92 /// operation initiation APIs are the same as ServerBidiReactor.
93 class ServerGenericBidiReactor
94     : public ServerBidiReactor<ByteBuffer, ByteBuffer> {
95  public:
96   /// Similar to ServerBidiReactor::OnStarted except for argument type.
97   ///
98   /// \param[in] context The context object associated with this RPC.
99   virtual void OnStarted(GenericServerContext* context) {}
100
101  private:
102   void OnStarted(ServerContext* ctx) final {
103     OnStarted(static_cast<GenericServerContext*>(ctx));
104   }
105 };
106
107 }  // namespace experimental
108
109 namespace internal {
110 class UnimplementedGenericBidiReactor
111     : public experimental::ServerGenericBidiReactor {
112  public:
113   void OnDone() override { delete this; }
114   void OnStarted(GenericServerContext*) override {
115     this->Finish(Status(StatusCode::UNIMPLEMENTED, ""));
116   }
117 };
118 }  // namespace internal
119
120 namespace experimental {
121
122 /// \a CallbackGenericService is the base class for generic services implemented
123 /// using the callback API and registered through the ServerBuilder using
124 /// RegisterCallbackGenericService.
125 class CallbackGenericService {
126  public:
127   CallbackGenericService() {}
128   virtual ~CallbackGenericService() {}
129
130   /// The "method handler" for the generic API. This function should be
131   /// overridden to return a ServerGenericBidiReactor that implements the
132   /// application-level interface for this RPC.
133   virtual ServerGenericBidiReactor* CreateReactor() {
134     return new internal::UnimplementedGenericBidiReactor;
135   }
136
137  private:
138   friend class ::grpc::Server;
139
140   internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>* Handler() {
141     return new internal::CallbackBidiHandler<ByteBuffer, ByteBuffer>(
142         [this] { return CreateReactor(); });
143   }
144
145   Server* server_{nullptr};
146 };
147 }  // namespace experimental
148 }  // namespace grpc
149
150 #endif  // GRPCPP_IMPL_CODEGEN_ASYNC_GENERIC_SERVICE_H