From 89219162dd613b58da8f3cd418f4825a5d566da5 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Tue, 7 Apr 2015 18:01:18 -0700 Subject: [PATCH 1/6] Refactoring std::chrono out. --- include/grpc++/client_context.h | 23 +++++++++++------- include/grpc++/completion_queue.h | 13 ++++++---- include/grpc++/credentials.h | 5 ++-- include/grpc++/server_context.h | 13 ++++++---- {src/cpp/util => include/grpc++}/time.h | 30 ++++++++++++++++++++++++ src/cpp/client/channel.cc | 3 +-- src/cpp/client/client_context.cc | 15 ++---------- src/cpp/client/secure_credentials.cc | 12 +++++----- src/cpp/common/completion_queue.cc | 9 +------ src/cpp/server/server.cc | 4 ++-- src/cpp/server/server_context.cc | 8 +++---- src/cpp/util/time.cc | 7 +++++- test/cpp/client/credentials_test.cc | 2 +- test/cpp/end2end/async_end2end_test.cc | 4 ++-- test/cpp/end2end/end2end_test.cc | 16 ++++++------- test/cpp/end2end/generic_end2end_test.cc | 2 +- test/cpp/interop/client.cc | 5 ++-- test/cpp/util/time_test.cc | 3 +-- 18 files changed, 101 insertions(+), 73 deletions(-) rename {src/cpp/util => include/grpc++}/time.h (77%) diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index 4e7f5a7be0e..19630c9b544 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -34,15 +34,13 @@ #ifndef GRPCXX_CLIENT_CONTEXT_H #define GRPCXX_CLIENT_CONTEXT_H -#include #include #include #include #include #include - -using std::chrono::system_clock; +#include struct grpc_call; struct grpc_completion_queue; @@ -87,8 +85,19 @@ class ClientContext { return trailing_metadata_; } - void set_absolute_deadline(const system_clock::time_point& deadline); - system_clock::time_point absolute_deadline(); + template + void set_deadline(const T& deadline) { + TimePoint deadline_tp(deadline); + deadline_ = deadline_tp.raw_time(); + } + +#ifndef GRPC_CXX0X_NO_CHRONO + std::chrono::system_clock::time_point deadline() { + return Timespec2Timepoint(deadline_); + } +#endif // !GRPC_CXX0X_NO_CHRONO + + gpr_timespec raw_deadline() { return deadline_; } void set_authority(const grpc::string& authority) { authority_ = authority; } @@ -125,14 +134,12 @@ class ClientContext { grpc_completion_queue* cq() { return cq_; } void set_cq(grpc_completion_queue* cq) { cq_ = cq; } - gpr_timespec RawDeadline() { return absolute_deadline_; } - grpc::string authority() { return authority_; } bool initial_metadata_received_; grpc_call* call_; grpc_completion_queue* cq_; - gpr_timespec absolute_deadline_; + gpr_timespec deadline_; grpc::string authority_; std::multimap send_initial_metadata_; std::multimap recv_initial_metadata_; diff --git a/include/grpc++/completion_queue.h b/include/grpc++/completion_queue.h index e6a8c6fe552..ba390c96e9d 100644 --- a/include/grpc++/completion_queue.h +++ b/include/grpc++/completion_queue.h @@ -34,9 +34,9 @@ #ifndef GRPCXX_COMPLETION_QUEUE_H #define GRPCXX_COMPLETION_QUEUE_H -#include -#include #include +#include +#include struct grpc_completion_queue; @@ -82,10 +82,13 @@ class CompletionQueue { // Nonblocking (until deadline) read from queue. // Cannot rely on result of tag or ok if return is TIMEOUT - NextStatus AsyncNext(void** tag, bool* ok, - std::chrono::system_clock::time_point deadline); + template + NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) { + TimePoint deadline_tp(deadline); + return AsyncNextInternal(tag, ok, deadline_tp.raw_time()); + } - // Blocking (until deadline) read from queue. + // Blocking read from queue. // Returns false if the queue is ready for destruction, true if event bool Next(void** tag, bool* ok) { diff --git a/include/grpc++/credentials.h b/include/grpc++/credentials.h index 2ac3eec95cd..15bfef55ab7 100644 --- a/include/grpc++/credentials.h +++ b/include/grpc++/credentials.h @@ -34,7 +34,6 @@ #ifndef GRPCXX_CREDENTIALS_H #define GRPCXX_CREDENTIALS_H -#include #include #include @@ -103,7 +102,7 @@ std::unique_ptr ComputeEngineCredentials(); // grpc_max_auth_token_lifetime or will be cropped to this value. std::unique_ptr ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, - std::chrono::seconds token_lifetime); + long token_lifetime); // Builds JWT credentials. // json_key is the JSON key string containing the client's private key. @@ -111,7 +110,7 @@ std::unique_ptr ServiceAccountCredentials( // this credentials. It should not exceed grpc_max_auth_token_lifetime or // will be cropped to this value. std::unique_ptr JWTCredentials( - const grpc::string& json_key, std::chrono::seconds token_lifetime); + const grpc::string& json_key, long token_lifetime); // Builds refresh token credentials. // json_refresh_token is the JSON string containing the refresh token along diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index 9e3b80c6411..a62babd9316 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -34,10 +34,11 @@ #ifndef GRPCXX_SERVER_CONTEXT_H #define GRPCXX_SERVER_CONTEXT_H -#include #include +#include #include +#include struct gpr_timespec; struct grpc_metadata; @@ -71,9 +72,13 @@ class ServerContext { ServerContext(); // for async calls ~ServerContext(); - std::chrono::system_clock::time_point absolute_deadline() { - return deadline_; +#ifndef GRPC_CXX0X_NO_CHRONO + std::chrono::system_clock::time_point deadline() { + return Timespec2Timepoint(deadline_); } +#endif // !GRPC_CXX0X_NO_CHRONO + + gpr_timespec raw_deadline() { return deadline_; } void AddInitialMetadata(const grpc::string& key, const grpc::string& value); void AddTrailingMetadata(const grpc::string& key, const grpc::string& value); @@ -110,7 +115,7 @@ class ServerContext { CompletionOp* completion_op_; - std::chrono::system_clock::time_point deadline_; + gpr_timespec deadline_; grpc_call* call_; CompletionQueue* cq_; bool sent_initial_metadata_; diff --git a/src/cpp/util/time.h b/include/grpc++/time.h similarity index 77% rename from src/cpp/util/time.h rename to include/grpc++/time.h index 8b7fcf55f78..470471ab8b4 100644 --- a/src/cpp/util/time.h +++ b/include/grpc++/time.h @@ -34,6 +34,23 @@ #ifndef GRPC_INTERNAL_CPP_UTIL_TIME_H #define GRPC_INTERNAL_CPP_UTIL_TIME_H +#include + +namespace grpc { + +template +class TimePoint { + public: + TimePoint(const T& time) : time_(time) { } + gpr_timespec raw_time() const { return time_; } + private: + gpr_timespec time_; +}; + +} // namespace grpc + +#ifndef GRPC_CXX0X_NO_CHRONO + #include #include @@ -46,6 +63,19 @@ void Timepoint2Timespec(const std::chrono::system_clock::time_point& from, std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t); +template <> +class TimePoint { + public: + TimePoint(const std::chrono::system_clock::time_point& time) { + Timepoint2Timespec(time, &time_); + } + gpr_timespec raw_time() const { return time_; } + private: + gpr_timespec time_; +}; + } // namespace grpc +#endif // !GRPC_CXX0X_NO_CHRONO + #endif // GRPC_INTERNAL_CPP_UTIL_TIME_H diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 5380d3a232c..aaad0420260 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -33,7 +33,6 @@ #include "src/cpp/client/channel.h" -#include #include #include @@ -64,7 +63,7 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context, context->authority().empty() ? target_.c_str() : context->authority().c_str(), - context->RawDeadline()); + context->raw_deadline()); context->set_call(c_call); return Call(c_call, this, cq); } diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index de9f8c7201d..70c9cb4c3b4 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -34,9 +34,7 @@ #include #include -#include "src/cpp/util/time.h" - -using std::chrono::system_clock; +#include namespace grpc { @@ -44,7 +42,7 @@ ClientContext::ClientContext() : initial_metadata_received_(false), call_(nullptr), cq_(nullptr), - absolute_deadline_(gpr_inf_future) {} + deadline_(gpr_inf_future) {} ClientContext::~ClientContext() { if (call_) { @@ -64,15 +62,6 @@ ClientContext::~ClientContext() { } } -void ClientContext::set_absolute_deadline( - const system_clock::time_point& deadline) { - Timepoint2Timespec(deadline, &absolute_deadline_); -} - -system_clock::time_point ClientContext::absolute_deadline() { - return Timespec2Timepoint(absolute_deadline_); -} - void ClientContext::AddMetadata(const grpc::string& meta_key, const grpc::string& meta_value) { send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index d6f9acc6755..85785ed2756 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -96,27 +96,27 @@ std::unique_ptr ComputeEngineCredentials() { // Builds service account credentials. std::unique_ptr ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, - std::chrono::seconds token_lifetime) { - if (token_lifetime.count() <= 0) { + long token_lifetime) { + if (token_lifetime <= 0) { gpr_log(GPR_ERROR, "Trying to create ServiceAccountCredentials " "with non-positive lifetime"); return WrapCredentials(nullptr); } - gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count()); + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime); return WrapCredentials(grpc_service_account_credentials_create( json_key.c_str(), scope.c_str(), lifetime)); } // Builds JWT credentials. std::unique_ptr JWTCredentials( - const grpc::string& json_key, std::chrono::seconds token_lifetime) { - if (token_lifetime.count() <= 0) { + const grpc::string& json_key, long token_lifetime) { + if (token_lifetime <= 0) { gpr_log(GPR_ERROR, "Trying to create JWTCredentials with non-positive lifetime"); return WrapCredentials(nullptr); } - gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count()); + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime); return WrapCredentials( grpc_jwt_credentials_create(json_key.c_str(), lifetime)); } diff --git a/src/cpp/common/completion_queue.cc b/src/cpp/common/completion_queue.cc index cea2d24831c..3c37e91e1fc 100644 --- a/src/cpp/common/completion_queue.cc +++ b/src/cpp/common/completion_queue.cc @@ -36,7 +36,7 @@ #include #include -#include "src/cpp/util/time.h" +#include namespace grpc { @@ -77,13 +77,6 @@ CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal( } } -CompletionQueue::NextStatus CompletionQueue::AsyncNext( - void** tag, bool* ok, std::chrono::system_clock::time_point deadline) { - gpr_timespec gpr_deadline; - Timepoint2Timespec(deadline, &gpr_deadline); - return AsyncNextInternal(tag, ok, gpr_deadline); -} - bool CompletionQueue::Pluck(CompletionQueueTag* tag) { std::unique_ptr ev; diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 046133c5eb1..2833f78a755 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -45,9 +45,9 @@ #include #include #include +#include #include "src/cpp/proto/proto_utils.h" -#include "src/cpp/util/time.h" namespace grpc { @@ -348,7 +348,7 @@ class Server::AsyncRequest GRPC_FINAL : public CompletionQueueTag { ServerContext* ctx = ctx_ ? ctx_ : generic_ctx_; GPR_ASSERT(ctx); if (*status) { - ctx->deadline_ = Timespec2Timepoint(call_details_.deadline); + ctx->deadline_ = call_details_.deadline; for (size_t i = 0; i < array_.count; i++) { ctx->client_metadata_.insert(std::make_pair( grpc::string(array_.metadata[i].key), diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index ffd6d30d5d4..6b5e41d0a82 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -33,11 +33,11 @@ #include -#include -#include #include #include -#include "src/cpp/util/time.h" +#include +#include +#include namespace grpc { @@ -99,7 +99,7 @@ ServerContext::ServerContext() ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata* metadata, size_t metadata_count) : completion_op_(nullptr), - deadline_(Timespec2Timepoint(deadline)), + deadline_(deadline), call_(nullptr), cq_(nullptr), sent_initial_metadata_(false) { diff --git a/src/cpp/util/time.cc b/src/cpp/util/time.cc index 059ea72abf6..1fef2a56def 100644 --- a/src/cpp/util/time.cc +++ b/src/cpp/util/time.cc @@ -31,9 +31,12 @@ * */ -#include "src/cpp/util/time.h" +#include + +#ifndef GRPC_CXX0X_NO_CHRONO #include +#include using std::chrono::duration_cast; using std::chrono::nanoseconds; @@ -68,3 +71,5 @@ system_clock::time_point Timespec2Timepoint(gpr_timespec t) { } } // namespace grpc + +#endif // !GRPC_CXX0X_NO_CHRONO diff --git a/test/cpp/client/credentials_test.cc b/test/cpp/client/credentials_test.cc index 24251f297be..d7ea09a5f1f 100644 --- a/test/cpp/client/credentials_test.cc +++ b/test/cpp/client/credentials_test.cc @@ -47,7 +47,7 @@ class CredentialsTest : public ::testing::Test { TEST_F(CredentialsTest, InvalidServiceAccountCreds) { std::unique_ptr bad1 = - ServiceAccountCredentials("", "", std::chrono::seconds(1)); + ServiceAccountCredentials("", "", 1); EXPECT_EQ(nullptr, bad1.get()); } diff --git a/test/cpp/end2end/async_end2end_test.cc b/test/cpp/end2end/async_end2end_test.cc index 9938fcf12eb..4fa4382211d 100644 --- a/test/cpp/end2end/async_end2end_test.cc +++ b/test/cpp/end2end/async_end2end_test.cc @@ -35,9 +35,9 @@ #include #include "test/core/util/test_config.h" +#include "test/core/util/port.h" #include "test/cpp/util/echo_duplicate.pb.h" #include "test/cpp/util/echo.pb.h" -#include "src/cpp/util/time.h" #include #include #include @@ -50,7 +50,7 @@ #include #include #include -#include "test/core/util/port.h" +#include #include #include diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 0d5db046df4..2fa3f0a32fc 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -34,10 +34,10 @@ #include #include +#include "test/core/util/port.h" #include "test/core/util/test_config.h" #include "test/cpp/util/echo_duplicate.pb.h" #include "test/cpp/util/echo.pb.h" -#include "src/cpp/util/time.h" #include "src/cpp/server/thread_pool.h" #include #include @@ -50,7 +50,7 @@ #include #include #include -#include "test/core/util/port.h" +#include #include #include @@ -72,8 +72,8 @@ void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, EchoResponse* response) { if (request->has_param() && request->param().echo_deadline()) { gpr_timespec deadline = gpr_inf_future; - if (context->absolute_deadline() != system_clock::time_point::max()) { - Timepoint2Timespec(context->absolute_deadline(), &deadline); + if (context->deadline() != system_clock::time_point::max()) { + Timepoint2Timespec(context->deadline(), &deadline); } response->mutable_param()->set_request_deadline(deadline.tv_sec); } @@ -245,7 +245,7 @@ TEST_F(End2endTest, RpcDeadlineExpires) { ClientContext context; std::chrono::system_clock::time_point deadline = std::chrono::system_clock::now() + std::chrono::microseconds(10); - context.set_absolute_deadline(deadline); + context.set_deadline(deadline); Status s = stub_->Echo(&context, request, &response); EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code()); } @@ -260,7 +260,7 @@ TEST_F(End2endTest, RpcLongDeadline) { ClientContext context; std::chrono::system_clock::time_point deadline = std::chrono::system_clock::now() + std::chrono::hours(1); - context.set_absolute_deadline(deadline); + context.set_deadline(deadline); Status s = stub_->Echo(&context, request, &response); EXPECT_EQ(response.message(), request.message()); EXPECT_TRUE(s.IsOk()); @@ -277,7 +277,7 @@ TEST_F(End2endTest, EchoDeadline) { ClientContext context; std::chrono::system_clock::time_point deadline = std::chrono::system_clock::now() + std::chrono::seconds(100); - context.set_absolute_deadline(deadline); + context.set_deadline(deadline); Status s = stub_->Echo(&context, request, &response); EXPECT_EQ(response.message(), request.message()); EXPECT_TRUE(s.IsOk()); @@ -428,7 +428,7 @@ TEST_F(End2endTest, DiffPackageServices) { // rpc and stream should fail on bad credentials. TEST_F(End2endTest, BadCredentials) { std::unique_ptr bad_creds = - ServiceAccountCredentials("", "", std::chrono::seconds(1)); + ServiceAccountCredentials("", "", 1); EXPECT_EQ(nullptr, bad_creds.get()); std::shared_ptr channel = CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); diff --git a/test/cpp/end2end/generic_end2end_test.cc b/test/cpp/end2end/generic_end2end_test.cc index 9cdd8c94d12..25f2c990bc3 100644 --- a/test/cpp/end2end/generic_end2end_test.cc +++ b/test/cpp/end2end/generic_end2end_test.cc @@ -35,7 +35,6 @@ #include #include "src/cpp/proto/proto_utils.h" -#include "src/cpp/util/time.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" #include "test/cpp/util/echo.pb.h" @@ -55,6 +54,7 @@ #include #include #include +#include #include #include diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index de6c6b7b77e..5971dc1aeab 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -138,8 +138,7 @@ std::shared_ptr CreateChannelForTestCase( std::unique_ptr creds; GPR_ASSERT(FLAGS_enable_ssl); grpc::string json_key = GetServiceAccountJsonKey(); - creds = ServiceAccountCredentials(json_key, FLAGS_oauth_scope, - std::chrono::hours(1)); + creds = ServiceAccountCredentials(json_key, FLAGS_oauth_scope, 3600); return CreateTestChannel(host_port, FLAGS_server_host_override, FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); } else if (test_case == "compute_engine_creds") { @@ -152,7 +151,7 @@ std::shared_ptr CreateChannelForTestCase( std::unique_ptr creds; GPR_ASSERT(FLAGS_enable_ssl); grpc::string json_key = GetServiceAccountJsonKey(); - creds = JWTCredentials(json_key, std::chrono::hours(1)); + creds = JWTCredentials(json_key, 3600); return CreateTestChannel(host_port, FLAGS_server_host_override, FLAGS_enable_ssl, FLAGS_use_prod_roots, creds); } else { diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc index 4641fdb4dae..767cb51c925 100644 --- a/test/cpp/util/time_test.cc +++ b/test/cpp/util/time_test.cc @@ -31,11 +31,10 @@ * */ -#include "src/cpp/util/time.h" - #include #include +#include #include using std::chrono::duration_cast; From a05b8b7b25511b2a5c9312579be4897e9fab0a97 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Wed, 8 Apr 2015 09:07:20 +0200 Subject: [PATCH 2/6] Removing a few more references to #include --- examples/pubsub/main.cc | 1 - test/cpp/end2end/async_end2end_test.cc | 1 - test/cpp/end2end/end2end_test.cc | 1 - test/cpp/end2end/generic_end2end_test.cc | 1 - test/cpp/interop/client.cc | 1 - test/cpp/util/time_test.cc | 2 -- 6 files changed, 7 deletions(-) diff --git a/examples/pubsub/main.cc b/examples/pubsub/main.cc index cc5076f0a55..3d1f89d96c8 100644 --- a/examples/pubsub/main.cc +++ b/examples/pubsub/main.cc @@ -31,7 +31,6 @@ * */ -#include #include #include #include diff --git a/test/cpp/end2end/async_end2end_test.cc b/test/cpp/end2end/async_end2end_test.cc index 4fa4382211d..698f1bb5473 100644 --- a/test/cpp/end2end/async_end2end_test.cc +++ b/test/cpp/end2end/async_end2end_test.cc @@ -31,7 +31,6 @@ * */ -#include #include #include "test/core/util/test_config.h" diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 2fa3f0a32fc..0e79e21ee0e 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -31,7 +31,6 @@ * */ -#include #include #include "test/core/util/port.h" diff --git a/test/cpp/end2end/generic_end2end_test.cc b/test/cpp/end2end/generic_end2end_test.cc index 25f2c990bc3..3551251d1d6 100644 --- a/test/cpp/end2end/generic_end2end_test.cc +++ b/test/cpp/end2end/generic_end2end_test.cc @@ -31,7 +31,6 @@ * */ -#include #include #include "src/cpp/proto/proto_utils.h" diff --git a/test/cpp/interop/client.cc b/test/cpp/interop/client.cc index 5971dc1aeab..a4641b667e7 100644 --- a/test/cpp/interop/client.cc +++ b/test/cpp/interop/client.cc @@ -31,7 +31,6 @@ * */ -#include #include #include #include diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc index 767cb51c925..a3cfb1c961c 100644 --- a/test/cpp/util/time_test.cc +++ b/test/cpp/util/time_test.cc @@ -31,8 +31,6 @@ * */ -#include - #include #include #include From b7bbffe9f6a57d5eddc52be200adf4ab7f247ca5 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Wed, 8 Apr 2015 20:26:49 +0200 Subject: [PATCH 3/6] Addressing github comments. --- include/grpc++/credentials.h | 16 ++++++++-------- include/grpc++/time.h | 26 ++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/include/grpc++/credentials.h b/include/grpc++/credentials.h index 15bfef55ab7..a193bba2c74 100644 --- a/include/grpc++/credentials.h +++ b/include/grpc++/credentials.h @@ -97,20 +97,20 @@ std::unique_ptr ComputeEngineCredentials(); // Builds service account credentials. // json_key is the JSON key string containing the client's private key. // scope is a space-delimited list of the requested permissions. -// token_lifetime is the lifetime of each token acquired through this service -// account credentials. It should be positive and should not exceed -// grpc_max_auth_token_lifetime or will be cropped to this value. +// token_lifetime_seconds is the lifetime in seconds of each token acquired +// through this service account credentials. It should be positive and should +// not exceed grpc_max_auth_token_lifetime or will be cropped to this value. std::unique_ptr ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, - long token_lifetime); + long token_lifetime_seconds); // Builds JWT credentials. // json_key is the JSON key string containing the client's private key. -// token_lifetime is the lifetime of each Json Web Token (JWT) created with -// this credentials. It should not exceed grpc_max_auth_token_lifetime or -// will be cropped to this value. +// token_lifetime_seconds is the lifetime in seconds of each Json Web Token +// (JWT) created with this credentials. It should not exceed +// grpc_max_auth_token_lifetime or will be cropped to this value. std::unique_ptr JWTCredentials( - const grpc::string& json_key, long token_lifetime); + const grpc::string& json_key, long token_lifetime_seconds); // Builds refresh token credentials. // json_refresh_token is the JSON string containing the refresh token along diff --git a/include/grpc++/time.h b/include/grpc++/time.h index 470471ab8b4..995e550f4b9 100644 --- a/include/grpc++/time.h +++ b/include/grpc++/time.h @@ -38,11 +38,33 @@ namespace grpc { +/* If you are trying to use CompletionQueue::AsyncNext with a time class that + isn't either gpr_timespec or std::chrono::system_clock::time_point, you + will most likely be looking at that comment as your compiler will have + fired the static_assert below. In order to fix that issue, you have two + potential solutions: + + 1. Use gpr_timespec or std::chrono::system_clock::time_point instead + 2. Specialize the TimePoint class with whichever time class that you + want to use here. See below for two examples of how to do that. + */ + template class TimePoint { public: - TimePoint(const T& time) : time_(time) { } - gpr_timespec raw_time() const { return time_; } + TimePoint(const T& time) { + static_assert(false, "You need a specialization of TimePoint"); + } + gpr_timespec raw_time() { + static_assert(false, "You need a specialization of TimePoint"); + } +}; + +template<> +class TimePoint { + public: + TimePoint(const gpr_timespec& time) : time_(time) { } + gpr_timespec raw_time() { return time_; } private: gpr_timespec time_; }; From 27a0dc0208e90b6463049df16da6c35216eab0aa Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sat, 11 Apr 2015 01:25:22 +0200 Subject: [PATCH 4/6] Adjusting build.json. --- Makefile | 2 ++ build.json | 6 +++--- vsprojects/vs2010/grpc++.vcxproj | 2 +- vsprojects/vs2010/grpc++.vcxproj.filters | 6 +++--- vsprojects/vs2013/grpc++.vcxproj | 2 +- vsprojects/vs2013/grpc++.vcxproj.filters | 6 +++--- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 903e497b776..929452b0845 100644 --- a/Makefile +++ b/Makefile @@ -3283,6 +3283,7 @@ PUBLIC_HEADERS_CXX += \ include/grpc++/status_code_enum.h \ include/grpc++/stream.h \ include/grpc++/thread_pool_interface.h \ + include/grpc++/time.h \ LIBGRPC++_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_SRC)))) @@ -3534,6 +3535,7 @@ PUBLIC_HEADERS_CXX += \ include/grpc++/status_code_enum.h \ include/grpc++/stream.h \ include/grpc++/thread_pool_interface.h \ + include/grpc++/time.h \ LIBGRPC++_UNSECURE_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_UNSECURE_SRC)))) diff --git a/build.json b/build.json index d814e4c6bd8..80f2c5cef7d 100644 --- a/build.json +++ b/build.json @@ -37,13 +37,13 @@ "include/grpc++/status.h", "include/grpc++/status_code_enum.h", "include/grpc++/stream.h", - "include/grpc++/thread_pool_interface.h" + "include/grpc++/thread_pool_interface.h", + "include/grpc++/time.h" ], "headers": [ "src/cpp/client/channel.h", "src/cpp/proto/proto_utils.h", - "src/cpp/server/thread_pool.h", - "src/cpp/util/time.h" + "src/cpp/server/thread_pool.h" ], "src": [ "src/cpp/client/channel.cc", diff --git a/vsprojects/vs2010/grpc++.vcxproj b/vsprojects/vs2010/grpc++.vcxproj index 0ee433163c8..1ef4a5158ba 100644 --- a/vsprojects/vs2010/grpc++.vcxproj +++ b/vsprojects/vs2010/grpc++.vcxproj @@ -102,12 +102,12 @@ + - diff --git a/vsprojects/vs2010/grpc++.vcxproj.filters b/vsprojects/vs2010/grpc++.vcxproj.filters index ed93daee044..284e31fae17 100644 --- a/vsprojects/vs2010/grpc++.vcxproj.filters +++ b/vsprojects/vs2010/grpc++.vcxproj.filters @@ -159,6 +159,9 @@ include\grpc++ + + include\grpc++ + @@ -170,9 +173,6 @@ src\cpp\server - - src\cpp\util - diff --git a/vsprojects/vs2013/grpc++.vcxproj b/vsprojects/vs2013/grpc++.vcxproj index d545a949cb3..c85cb9414e8 100644 --- a/vsprojects/vs2013/grpc++.vcxproj +++ b/vsprojects/vs2013/grpc++.vcxproj @@ -104,12 +104,12 @@ + - diff --git a/vsprojects/vs2013/grpc++.vcxproj.filters b/vsprojects/vs2013/grpc++.vcxproj.filters index ed93daee044..284e31fae17 100644 --- a/vsprojects/vs2013/grpc++.vcxproj.filters +++ b/vsprojects/vs2013/grpc++.vcxproj.filters @@ -159,6 +159,9 @@ include\grpc++ + + include\grpc++ + @@ -170,9 +173,6 @@ src\cpp\server - - src\cpp\util - From e24dde5736ea833b95765f8ddefb737538dec223 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 24 Apr 2015 00:43:09 +0200 Subject: [PATCH 5/6] Adressing comments. --- include/grpc++/time.h | 6 +++--- src/cpp/client/secure_credentials.cc | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/grpc++/time.h b/include/grpc++/time.h index 1fc4ad799c6..ffcba4e35aa 100644 --- a/include/grpc++/time.h +++ b/include/grpc++/time.h @@ -40,13 +40,13 @@ namespace grpc { /* If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec or std::chrono::system_clock::time_point, you - will most likely be looking at that comment as your compiler will have - fired an error below. In order to fix that issue, you have two potential + will most likely be looking at this comment as your compiler will have + fired an error below. In order to fix this issue, you have two potential solutions: 1. Use gpr_timespec or std::chrono::system_clock::time_point instead 2. Specialize the TimePoint class with whichever time class that you - want to use here. See below for two examples of how to do that. + want to use here. See below for two examples of how to do this. */ template diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index 9e83ac8dcdf..48bf7430b27 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -81,27 +81,27 @@ std::unique_ptr ComputeEngineCredentials() { // Builds service account credentials. std::unique_ptr ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, - long token_lifetime) { - if (token_lifetime <= 0) { + long token_lifetime_seconds) { + if (token_lifetime_seconds <= 0) { gpr_log(GPR_ERROR, "Trying to create ServiceAccountCredentials " "with non-positive lifetime"); return WrapCredentials(nullptr); } - gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime); + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds); return WrapCredentials(grpc_service_account_credentials_create( json_key.c_str(), scope.c_str(), lifetime)); } // Builds JWT credentials. std::unique_ptr JWTCredentials( - const grpc::string& json_key, long token_lifetime) { - if (token_lifetime <= 0) { + const grpc::string& json_key, long token_lifetime_seconds) { + if (token_lifetime_seconds <= 0) { gpr_log(GPR_ERROR, "Trying to create JWTCredentials with non-positive lifetime"); return WrapCredentials(nullptr); } - gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime); + gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds); return WrapCredentials( grpc_jwt_credentials_create(json_key.c_str(), lifetime)); } From 76e49fd80557c33595508686f3450982e63f0fce Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 24 Apr 2015 01:12:18 +0200 Subject: [PATCH 6/6] Forgot to rename header guard. --- include/grpc++/time.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/grpc++/time.h b/include/grpc++/time.h index ffcba4e35aa..f9b2ce5cab1 100644 --- a/include/grpc++/time.h +++ b/include/grpc++/time.h @@ -31,8 +31,8 @@ * */ -#ifndef GRPC_INTERNAL_CPP_UTIL_TIME_H -#define GRPC_INTERNAL_CPP_UTIL_TIME_H +#ifndef GRPCXX_TIME_H +#define GRPCXX_TIME_H #include @@ -103,4 +103,4 @@ class TimePoint { #endif // !GRPC_CXX0X_NO_CHRONO -#endif // GRPC_INTERNAL_CPP_UTIL_TIME_H +#endif // GRPCXX_TIME_H