blob: 5fa371ba626ccd0f87c3630d7d73f20de76fd8cc [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, 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 __GRPCPP_SERVER_H__
35#define __GRPCPP_SERVER_H__
36
37#include <condition_variable>
38#include <map>
39#include <memory>
40#include <mutex>
41
42#include <grpc++/completion_queue.h>
43#include <grpc++/config.h>
44#include <grpc++/status.h>
45
46struct grpc_server;
47
48namespace google {
49namespace protobuf {
50class Message;
51}
52}
53
54namespace grpc {
55class AsyncServerContext;
56class RpcService;
57class RpcServiceMethod;
yangg9e21f722014-12-08 15:49:52 -080058class ServerCredentials;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059class ThreadPoolInterface;
60
61// Currently it only supports handling rpcs in a single thread.
62class Server {
63 public:
64 ~Server();
65
66 // Shutdown the server, block until all rpc processing finishes.
67 void Shutdown();
68
69 private:
70 friend class ServerBuilder;
71
72 // ServerBuilder use only
yangg9e21f722014-12-08 15:49:52 -080073 Server(ThreadPoolInterface* thread_pool, ServerCredentials* creds);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 Server();
75 // Register a service. This call does not take ownership of the service.
76 // The service must exist for the lifetime of the Server instance.
77 void RegisterService(RpcService* service);
78 // Add a listening port. Can be called multiple times.
79 void AddPort(const grpc::string& addr);
80 // Start the server.
81 void Start();
82
83 void AllowOneRpc();
84 void HandleQueueClosed();
85 void RunRpc();
86 void ScheduleCallback();
87
88 // Completion queue.
89 CompletionQueue cq_;
90
91 // Sever status
92 std::mutex mu_;
93 bool started_;
94 bool shutdown_;
95 // The number of threads which are running callbacks.
96 int num_running_cb_;
97 std::condition_variable callback_cv_;
98
99 // Pointer to the c grpc server.
100 grpc_server* server_;
101
102 // A map for all method information.
103 std::map<grpc::string, RpcServiceMethod*> method_map_;
104
105 ThreadPoolInterface* thread_pool_;
106 // Whether the thread pool is created and owned by the server.
107 bool thread_pool_owned_;
yangg9e21f722014-12-08 15:49:52 -0800108 // Whether the server is created with credentials.
109 bool secure_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110};
111
112} // namespace grpc
113
114#endif // __GRPCPP_SERVER_H__