Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #ifndef GRPCXX_SERVER_BUILDER_H
35 : #define GRPCXX_SERVER_BUILDER_H
36 :
37 : #include <memory>
38 : #include <vector>
39 :
40 : #include <grpc/compression.h>
41 : #include <grpc++/support/config.h>
42 :
43 : namespace grpc {
44 :
45 : class AsyncGenericService;
46 : class AsynchronousService;
47 : class CompletionQueue;
48 : class RpcService;
49 : class Server;
50 : class ServerCompletionQueue;
51 : class ServerCredentials;
52 : class SynchronousService;
53 : class ThreadPoolInterface;
54 :
55 : /// A builder class for the creation and startup of \a grpc::Server instances.
56 159 : class ServerBuilder {
57 : public:
58 : ServerBuilder();
59 :
60 : /// Register a service. This call does not take ownership of the service.
61 : /// The service must exist for the lifetime of the \a Server instance returned
62 : /// by \a BuildAndStart().
63 : /// Matches requests with any :authority
64 : void RegisterService(SynchronousService* service);
65 :
66 : /// Register an asynchronous service.
67 : /// This call does not take ownership of the service or completion queue.
68 : /// The service and completion queuemust exist for the lifetime of the \a
69 : /// Server instance returned by \a BuildAndStart().
70 : /// Matches requests with any :authority
71 : void RegisterAsyncService(AsynchronousService* service);
72 :
73 : /// Register a generic service.
74 : /// Matches requests with any :authority
75 : void RegisterAsyncGenericService(AsyncGenericService* service);
76 :
77 : /// Register a service. This call does not take ownership of the service.
78 : /// The service must exist for the lifetime of the \a Server instance returned
79 : /// by BuildAndStart().
80 : /// Only matches requests with :authority \a host
81 : void RegisterService(const grpc::string& host, SynchronousService* service);
82 :
83 : /// Register an asynchronous service.
84 : /// This call does not take ownership of the service or completion queue.
85 : /// The service and completion queuemust exist for the lifetime of the \a
86 : /// Server instance returned by \a BuildAndStart().
87 : /// Only matches requests with :authority equal to \a host
88 : void RegisterAsyncService(const grpc::string& host,
89 : AsynchronousService* service);
90 :
91 : /// Set max message size in bytes.
92 82 : void SetMaxMessageSize(int max_message_size) {
93 82 : max_message_size_ = max_message_size;
94 82 : }
95 :
96 : /// Set the compression options to be used by the server.
97 : void SetCompressionOptions(const grpc_compression_options& options) {
98 : compression_options_ = options;
99 : }
100 :
101 : /// Tries to bind \a server to the given \a addr.
102 : ///
103 : /// It can be invoked multiple times.
104 : ///
105 : /// \param addr The address to try to bind to the server (eg, localhost:1234,
106 : /// 192.168.1.1:31416, [::1]:27182, etc.).
107 : /// \params creds The credentials associated with the server.
108 : /// \param selected_port[out] Upon success, updated to contain the port
109 : /// number. \a nullptr otherwise.
110 : ///
111 : // TODO(dgq): the "port" part seems to be a misnomer.
112 : void AddListeningPort(const grpc::string& addr,
113 : std::shared_ptr<ServerCredentials> creds,
114 : int* selected_port = nullptr);
115 :
116 : /// Add a completion queue for handling asynchronous services
117 : /// Caller is required to keep this completion queue live until
118 : /// the server is destroyed.
119 : std::unique_ptr<ServerCompletionQueue> AddCompletionQueue();
120 :
121 : /// Return a running server which is ready for processing calls.
122 : std::unique_ptr<Server> BuildAndStart();
123 :
124 : private:
125 477 : struct Port {
126 : grpc::string addr;
127 : std::shared_ptr<ServerCredentials> creds;
128 : int* selected_port;
129 : };
130 :
131 : typedef std::unique_ptr<grpc::string> HostString;
132 : template <class T>
133 319 : struct NamedService {
134 238 : explicit NamedService(T* s) : service(s) {}
135 81 : NamedService(const grpc::string& h, T* s)
136 81 : : host(new grpc::string(h)), service(s) {}
137 : HostString host;
138 : T* service;
139 : };
140 :
141 : int max_message_size_;
142 : grpc_compression_options compression_options_;
143 : std::vector<std::unique_ptr<NamedService<RpcService>>> services_;
144 : std::vector<std::unique_ptr<NamedService<AsynchronousService>>>
145 : async_services_;
146 : std::vector<Port> ports_;
147 : std::vector<ServerCompletionQueue*> cqs_;
148 : std::shared_ptr<ServerCredentials> creds_;
149 : AsyncGenericService* generic_service_;
150 : ThreadPoolInterface* thread_pool_;
151 : };
152 :
153 : } // namespace grpc
154 :
155 : #endif // GRPCXX_SERVER_BUILDER_H
|