Skip to content

Commit

Permalink
Update C++ server with new core API
Browse files Browse the repository at this point in the history
And reflects the C++ API in ServerBuilder.
  • Loading branch information
ctiller committed Feb 23, 2015
1 parent 759026c commit 42bc87c
Show file tree
Hide file tree
Showing 30 changed files with 165 additions and 125 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ $(OBJDIR)/$(CONFIG)/src/core/transport/transport.o:

LIBGRPC++_SRC = \
src/cpp/client/secure_credentials.cc \
src/cpp/server/secure_server_credentials.cc \
src/cpp/client/channel.cc \
src/cpp/client/channel_arguments.cc \
src/cpp/client/client_context.cc \
Expand All @@ -3034,6 +3035,7 @@ LIBGRPC++_SRC = \
src/cpp/common/completion_queue.cc \
src/cpp/common/rpc_method.cc \
src/cpp/proto/proto_utils.cc \
src/cpp/server/insecure_server_credentials.cc \
src/cpp/server/server.cc \
src/cpp/server/server_builder.cc \
src/cpp/server/server_context.cc \
Expand Down Expand Up @@ -3101,6 +3103,7 @@ ifneq ($(OPENSSL_DEP),)
# installing headers to their final destination on the drive. We need this
# otherwise parallel compilation will fail if a source is compiled first.
src/cpp/client/secure_credentials.cc: $(OPENSSL_DEP)
src/cpp/server/secure_server_credentials.cc: $(OPENSSL_DEP)
src/cpp/client/channel.cc: $(OPENSSL_DEP)
src/cpp/client/channel_arguments.cc: $(OPENSSL_DEP)
src/cpp/client/client_context.cc: $(OPENSSL_DEP)
Expand All @@ -3113,6 +3116,7 @@ src/cpp/common/call.cc: $(OPENSSL_DEP)
src/cpp/common/completion_queue.cc: $(OPENSSL_DEP)
src/cpp/common/rpc_method.cc: $(OPENSSL_DEP)
src/cpp/proto/proto_utils.cc: $(OPENSSL_DEP)
src/cpp/server/insecure_server_credentials.cc: $(OPENSSL_DEP)
src/cpp/server/server.cc: $(OPENSSL_DEP)
src/cpp/server/server_builder.cc: $(OPENSSL_DEP)
src/cpp/server/server_context.cc: $(OPENSSL_DEP)
Expand Down Expand Up @@ -3169,6 +3173,7 @@ endif
endif

$(OBJDIR)/$(CONFIG)/src/cpp/client/secure_credentials.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/secure_server_credentials.o:
$(OBJDIR)/$(CONFIG)/src/cpp/client/channel.o:
$(OBJDIR)/$(CONFIG)/src/cpp/client/channel_arguments.o:
$(OBJDIR)/$(CONFIG)/src/cpp/client/client_context.o:
Expand All @@ -3181,6 +3186,7 @@ $(OBJDIR)/$(CONFIG)/src/cpp/common/call.o:
$(OBJDIR)/$(CONFIG)/src/cpp/common/completion_queue.o:
$(OBJDIR)/$(CONFIG)/src/cpp/common/rpc_method.o:
$(OBJDIR)/$(CONFIG)/src/cpp/proto/proto_utils.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/insecure_server_credentials.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server_builder.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server_context.o:
Expand Down Expand Up @@ -3268,6 +3274,7 @@ LIBGRPC++_UNSECURE_SRC = \
src/cpp/common/completion_queue.cc \
src/cpp/common/rpc_method.cc \
src/cpp/proto/proto_utils.cc \
src/cpp/server/insecure_server_credentials.cc \
src/cpp/server/server.cc \
src/cpp/server/server_builder.cc \
src/cpp/server/server_context.cc \
Expand Down Expand Up @@ -3363,6 +3370,7 @@ $(OBJDIR)/$(CONFIG)/src/cpp/common/call.o:
$(OBJDIR)/$(CONFIG)/src/cpp/common/completion_queue.o:
$(OBJDIR)/$(CONFIG)/src/cpp/common/rpc_method.o:
$(OBJDIR)/$(CONFIG)/src/cpp/proto/proto_utils.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/insecure_server_credentials.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server_builder.o:
$(OBJDIR)/$(CONFIG)/src/cpp/server/server_context.o:
Expand Down
5 changes: 3 additions & 2 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"src/cpp/common/completion_queue.cc",
"src/cpp/common/rpc_method.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/server/insecure_server_credentials.cc",
"src/cpp/server/server.cc",
"src/cpp/server/server_builder.cc",
"src/cpp/server/server_context.cc",
Expand Down Expand Up @@ -133,7 +134,6 @@
"src/core/surface/client.h",
"src/core/surface/completion_queue.h",
"src/core/surface/event_string.h",
"src/core/surface/lame_client.h",
"src/core/surface/server.h",
"src/core/surface/surface_trace.h",
"src/core/transport/chttp2/bin_encoder.h",
Expand Down Expand Up @@ -437,7 +437,8 @@
"build": "all",
"language": "c++",
"src": [
"src/cpp/client/secure_credentials.cc"
"src/cpp/client/secure_credentials.cc",
"src/cpp/server/secure_server_credentials.cc"
],
"deps": [
"gpr",
Expand Down
4 changes: 2 additions & 2 deletions examples/pubsub/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ int main(int argc, char** argv) {
std::unique_ptr<grpc::Credentials> creds;
if (FLAGS_service_account_key_file != "") {
grpc::string json_key = GetServiceAccountJsonKey();
creds = grpc::CredentialsFactory::ServiceAccountCredentials(
creds = grpc::ServiceAccountCredentials(
json_key, FLAGS_oauth_scope, std::chrono::hours(1));
} else {
creds = grpc::CredentialsFactory::ComputeEngineCredentials();
creds = grpc::ComputeEngineCredentials();
}

ss << FLAGS_server_host << ":" << FLAGS_server_port;
Expand Down
5 changes: 3 additions & 2 deletions examples/pubsub/publisher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/server_credentials.h>
#include <grpc++/status.h>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -106,11 +107,11 @@ class PublisherTest : public ::testing::Test {
int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddPort(server_address_.str());
builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();

channel_ = CreateChannel(server_address_.str(), ChannelArguments());
channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(), ChannelArguments());

publisher_.reset(new grpc::examples::pubsub::Publisher(channel_));
}
Expand Down
5 changes: 3 additions & 2 deletions examples/pubsub/subscriber_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/server_credentials.h>
#include <grpc++/status.h>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -105,11 +106,11 @@ class SubscriberTest : public ::testing::Test {
int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddPort(server_address_.str());
builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();

channel_ = CreateChannel(server_address_.str(), ChannelArguments());
channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(), ChannelArguments());

subscriber_.reset(new grpc::examples::pubsub::Subscriber(channel_));
}
Expand Down
11 changes: 4 additions & 7 deletions include/grpc++/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ class Server final : private CallHook,
class AsyncRequest;

// ServerBuilder use only
Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned,
ServerCredentials* creds);
Server();
Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned);
Server() = delete;
// Register a service. This call does not take ownership of the service.
// The service must exist for the lifetime of the Server instance.
bool RegisterService(RpcService* service);
bool RegisterAsyncService(AsynchronousService* service);
// Add a listening port. Can be called multiple times.
int AddPort(const grpc::string& addr);
int AddPort(const grpc::string& addr, ServerCredentials* creds);
// Start the server.
bool Start();

Expand Down Expand Up @@ -114,13 +113,11 @@ class Server final : private CallHook,
std::list<SyncRequest> sync_methods_;

// Pointer to the c grpc server.
grpc_server* server_;
grpc_server* const server_;

ThreadPoolInterface* thread_pool_;
// Whether the thread pool is created and owned by the server.
bool thread_pool_owned_;
// Whether the server is created with credentials.
bool secure_;
};

} // namespace grpc
Expand Down
16 changes: 10 additions & 6 deletions include/grpc++/server_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ class ServerBuilder {
void RegisterAsyncService(AsynchronousService* service);

// Add a listening port. Can be called multiple times.
void AddPort(const grpc::string& addr);

// Set a ServerCredentials. Can only be called once.
// TODO(yangg) move this to be part of AddPort
void SetCredentials(const std::shared_ptr<ServerCredentials>& creds);
void AddPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
int* selected_port = nullptr);

// Set the thread pool used for running appliation rpc handlers.
// Does not take ownership.
Expand All @@ -79,9 +77,15 @@ class ServerBuilder {
std::unique_ptr<Server> BuildAndStart();

private:
struct Port {
grpc::string addr;
std::shared_ptr<ServerCredentials> creds;
int* selected_port;
};

std::vector<RpcService*> services_;
std::vector<AsynchronousService*> async_services_;
std::vector<grpc::string> ports_;
std::vector<Port> ports_;
std::shared_ptr<ServerCredentials> creds_;
ThreadPoolInterface* thread_pool_ = nullptr;
};
Expand Down
29 changes: 12 additions & 17 deletions include/grpc++/server_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,21 @@

#include <grpc++/config.h>

struct grpc_server_credentials;
struct grpc_server;

namespace grpc {
class Server;

// grpc_server_credentials wrapper class.
class ServerCredentials final {
class ServerCredentials {
public:
~ServerCredentials();
virtual ~ServerCredentials();

private:
explicit ServerCredentials(grpc_server_credentials* c_creds);
friend class ::grpc::Server;

grpc_server_credentials* GetRawCreds();

friend class ServerCredentialsFactory;
friend class Server;

grpc_server_credentials* creds_;
virtual int AddPortToServer(const grpc::string& addr,
grpc_server* server) = 0;
};

// Options to create ServerCredentials with SSL
Expand All @@ -69,13 +66,11 @@ struct SslServerCredentialsOptions {
std::vector<PemKeyCertPair> pem_key_cert_pairs;
};

// Factory for building different types of ServerCredentials
class ServerCredentialsFactory {
public:
// Builds SSL ServerCredentials given SSL specific options
static std::shared_ptr<ServerCredentials> SslCredentials(
const SslServerCredentialsOptions& options);
};
// Builds SSL ServerCredentials given SSL specific options
std::shared_ptr<ServerCredentials> SslServerCredentials(
const SslServerCredentialsOptions& options);

std::shared_ptr<ServerCredentials> InsecureServerCredentials();

} // namespace grpc

Expand Down
3 changes: 3 additions & 0 deletions include/grpc/grpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
grpc_channel *grpc_channel_create(const char *target,
const grpc_channel_args *args);

/* Create a lame client: this client fails every operation attempted on it. */
grpc_channel *grpc_lame_client_channel_create(void);

/* Close and destroy a grpc channel */
void grpc_channel_destroy(grpc_channel *channel);

Expand Down
2 changes: 1 addition & 1 deletion src/core/security/factories.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

#include <string.h>

#include <grpc/grpc.h>
#include "src/core/security/credentials.h"
#include "src/core/security/security_context.h"
#include "src/core/surface/lame_client.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
Expand Down
1 change: 0 additions & 1 deletion src/core/security/security_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "src/core/support/env.h"
#include "src/core/support/file.h"
#include "src/core/support/string.h"
#include "src/core/surface/lame_client.h"
#include "src/core/transport/chttp2/alpn.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
Expand Down
2 changes: 1 addition & 1 deletion src/core/surface/lame_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
*/

#include "src/core/surface/lame_client.h"
#include <grpc/grpc.h>

#include <string.h>

Expand Down
3 changes: 2 additions & 1 deletion src/cpp/client/create_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ChannelArguments;
std::shared_ptr<ChannelInterface> CreateChannel(
const grpc::string &target, const std::unique_ptr<Credentials> &creds,
const ChannelArguments &args) {
return creds->CreateChannel(target, args);
return creds ? creds->CreateChannel(target, args) :
std::shared_ptr<ChannelInterface>(new Channel(target, grpc_lame_client_channel_create()));
}
} // namespace grpc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@
*
*/

#ifndef __GRPC_INTERNAL_SURFACE_LAME_CLIENT_H_
#define __GRPC_INTERNAL_SURFACE_LAME_CLIENT_H_
#include <grpc/grpc_security.h>

#include <grpc/grpc.h>
#include <grpc++/server_credentials.h>

/* Create a lame client: this client fails every operation attempted on it. */
grpc_channel *grpc_lame_client_channel_create(void);
namespace grpc {
namespace {
class InsecureServerCredentialsImpl final : public ServerCredentials {
public:
int AddPortToServer(const grpc::string& addr, grpc_server* server) {
return grpc_server_add_http2_port(server, addr.c_str());
}
};
} // namespace

#endif /* __GRPC_INTERNAL_SURFACE_LAME_CLIENT_H_ */
std::shared_ptr<ServerCredentials> InsecureServerCredentials() {
return std::shared_ptr<ServerCredentials>(new InsecureServerCredentialsImpl());
}

} // namespace grpc
Loading

0 comments on commit 42bc87c

Please sign in to comment.