Skip to content
Snippets Groups Projects
Commit 30fd935a authored by Craig Tiller's avatar Craig Tiller
Browse files

Merge github.com:google/grpc into async-api

parents d642dcf9 78d1fd07
No related branches found
No related tags found
No related merge requests found
Showing
with 984 additions and 198 deletions
...@@ -17,6 +17,11 @@ coverage ...@@ -17,6 +17,11 @@ coverage
# python compiled objects # python compiled objects
*.pyc *.pyc
#eclipse project files
.cproject
.project
.settings
# cache for run_tests.py # cache for run_tests.py
.run_tests_cache .run_tests_cache
......
This diff is collapsed.
...@@ -435,13 +435,26 @@ ...@@ -435,13 +435,26 @@
"examples/tips/label.proto", "examples/tips/label.proto",
"examples/tips/empty.proto", "examples/tips/empty.proto",
"examples/tips/pubsub.proto", "examples/tips/pubsub.proto",
"examples/tips/client.cc" "examples/tips/publisher.cc",
"examples/tips/subscriber.cc"
], ],
"deps": [ "deps": [
"grpc++", "grpc++",
"grpc", "grpc",
"gpr" "gpr"
] ]
},
{
"name": "grpc_csharp_ext",
"build": "all",
"language": "c",
"deps": [
"gpr",
"grpc"
],
"src": [
"src/csharp/ext/grpc_csharp_ext.c"
]
} }
], ],
"targets": [ "targets": [
...@@ -1570,31 +1583,32 @@ ...@@ -1570,31 +1583,32 @@
"run": false "run": false
}, },
{ {
"name": "qps_client", "name": "tips_client",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"test/cpp/qps/qpstest.proto", "examples/tips/main.cc"
"test/cpp/qps/client.cc"
], ],
"deps": [ "deps": [
"tips_client_lib",
"grpc++_test_util", "grpc++_test_util",
"grpc_test_util", "grpc_test_util",
"grpc++", "grpc++",
"grpc", "grpc",
"gpr_test_util", "gpr_test_util",
"gpr" "gpr"
] ],
"run": false
}, },
{ {
"name": "qps_server", "name": "tips_publisher_test",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"test/cpp/qps/qpstest.proto", "examples/tips/publisher_test.cc"
"test/cpp/qps/server.cc"
], ],
"deps": [ "deps": [
"tips_client_lib",
"grpc++_test_util", "grpc++_test_util",
"grpc_test_util", "grpc_test_util",
"grpc++", "grpc++",
...@@ -1604,30 +1618,32 @@ ...@@ -1604,30 +1618,32 @@
] ]
}, },
{ {
"name": "ruby_plugin", "name": "tips_subscriber_test",
"build": "protoc", "build": "test",
"language": "c++", "language": "c++",
"headers": [
"src/compiler/cpp_generator.h",
"src/compiler/cpp_generator_helpers-inl.h",
"src/compiler/cpp_generator_map-inl.h",
"src/compiler/cpp_generator_string-inl.h"
],
"src": [ "src": [
"src/compiler/ruby_generator.cc", "examples/tips/subscriber_test.cc"
"src/compiler/ruby_plugin.cc"
], ],
"deps": [], "deps": [
"secure": false "tips_client_lib",
"grpc++_test_util",
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
}, },
{ {
"name": "status_test", "name": "qps_client",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"test/cpp/util/status_test.cc" "test/cpp/qps/qpstest.proto",
"test/cpp/qps/client.cc"
], ],
"deps": [ "deps": [
"grpc++_test_util",
"grpc_test_util", "grpc_test_util",
"grpc++", "grpc++",
"grpc", "grpc",
...@@ -1636,11 +1652,12 @@ ...@@ -1636,11 +1652,12 @@
] ]
}, },
{ {
"name": "sync_client_async_server_test", "name": "qps_server",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"test/cpp/end2end/sync_client_async_server_test.cc" "test/cpp/qps/qpstest.proto",
"test/cpp/qps/server.cc"
], ],
"deps": [ "deps": [
"grpc++_test_util", "grpc++_test_util",
...@@ -1652,11 +1669,28 @@ ...@@ -1652,11 +1669,28 @@
] ]
}, },
{ {
"name": "thread_pool_test", "name": "ruby_plugin",
"build": "protoc",
"language": "c++",
"headers": [
"src/compiler/cpp_generator.h",
"src/compiler/cpp_generator_helpers-inl.h",
"src/compiler/cpp_generator_map-inl.h",
"src/compiler/cpp_generator_string-inl.h"
],
"src": [
"src/compiler/ruby_generator.cc",
"src/compiler/ruby_plugin.cc"
],
"deps": [],
"secure": false
},
{
"name": "status_test",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"test/cpp/server/thread_pool_test.cc" "test/cpp/util/status_test.cc"
], ],
"deps": [ "deps": [
"grpc_test_util", "grpc_test_util",
...@@ -1667,33 +1701,29 @@ ...@@ -1667,33 +1701,29 @@
] ]
}, },
{ {
"name": "tips_client", "name": "sync_client_async_server_test",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"examples/tips/client_main.cc" "test/cpp/end2end/sync_client_async_server_test.cc"
], ],
"deps": [ "deps": [
"tips_client_lib",
"grpc++_test_util", "grpc++_test_util",
"grpc_test_util", "grpc_test_util",
"grpc++", "grpc++",
"grpc", "grpc",
"gpr_test_util", "gpr_test_util",
"gpr" "gpr"
], ]
"run": false
}, },
{ {
"name": "tips_client_test", "name": "thread_pool_test",
"build": "test", "build": "test",
"language": "c++", "language": "c++",
"src": [ "src": [
"examples/tips/client_test.cc" "test/cpp/server/thread_pool_test.cc"
], ],
"deps": [ "deps": [
"tips_client_lib",
"grpc++_test_util",
"grpc_test_util", "grpc_test_util",
"grpc++", "grpc++",
"grpc", "grpc",
......
C++ Client implementation for Cloud Pub/Sub service (TIPS)
(https://developers.google.com/apis-explorer/#p/pubsub/v1beta1/).
"Google Cloud Pub/Sub" API needs to be enabled at
https://console.developers.google.com/project to open the access for a client.
Select the project name, select the "APIs" under "APIs & auth", and turn
on "Google Cloud Pub/Sub" API.
To run the client from Google Compute Engine (GCE), the GCE instance needs to
be created with scope "https://www.googleapis.com/auth/cloud-platform" as below:
gcloud compute instances create instance-name
--image debian-7 --scopes https://www.googleapis.com/auth/cloud-platform
To run the client from GCE:
make tips_client
bins/opt/tips_client --project_id="your project id"
A service account credential is required to run the client from other
environments, which can be generated as a JSON key file from
https://console.developers.google.com/project/. To run the client with a service
account credential:
bins/opt/tips_client
--project_id="your project id"
--service_account_key_file="absolute path to the JSON key file"
// This file will be moved to a new location.
syntax = "proto2"; syntax = "proto2";
package proto2; package proto2;
......
// This file will be moved to a new location.
// Labels provide a way to associate user-defined metadata with various // Labels provide a way to associate user-defined metadata with various
// objects. Labels may be used to organize objects into non-hierarchical // objects. Labels may be used to organize objects into non-hierarchical
// groups; think metadata tags attached to mp3s. // groups; think metadata tags attached to mp3s.
......
...@@ -46,18 +46,30 @@ ...@@ -46,18 +46,30 @@
#include <grpc++/credentials.h> #include <grpc++/credentials.h>
#include <grpc++/status.h> #include <grpc++/status.h>
#include "examples/tips/client.h" #include "examples/tips/publisher.h"
#include "examples/tips/subscriber.h"
#include "test/cpp/util/create_test_channel.h" #include "test/cpp/util/create_test_channel.h"
DEFINE_int32(server_port, 443, "Server port."); DEFINE_int32(server_port, 443, "Server port.");
DEFINE_string(server_host, DEFINE_string(server_host,
"pubsub-staging.googleapis.com", "Server host to connect to"); "pubsub-staging.googleapis.com", "Server host to connect to");
DEFINE_string(project_id, "", "GCE project id such as stoked-keyword-656");
DEFINE_string(service_account_key_file, "", DEFINE_string(service_account_key_file, "",
"Path to service account json key file."); "Path to service account json key file.");
DEFINE_string(oauth_scope, "", "Scope for OAuth tokens."); DEFINE_string(oauth_scope,
"https://www.googleapis.com/auth/cloud-platform",
"Scope for OAuth tokens.");
namespace {
const char kTopic[] = "testtopics";
const char kSubscriptionName[] = "testsubscription";
const char kMessageData[] = "Test Data";
} // namespace
grpc::string GetServiceAccountJsonKey() { grpc::string GetServiceAccountJsonKey() {
static grpc::string json_key; grpc::string json_key;
if (json_key.empty()) { if (json_key.empty()) {
std::ifstream json_key_file(FLAGS_service_account_key_file); std::ifstream json_key_file(FLAGS_service_account_key_file);
std::stringstream key_stream; std::stringstream key_stream;
...@@ -72,10 +84,7 @@ int main(int argc, char** argv) { ...@@ -72,10 +84,7 @@ int main(int argc, char** argv) {
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
gpr_log(GPR_INFO, "Start TIPS client"); gpr_log(GPR_INFO, "Start TIPS client");
const int host_port_buf_size = 1024; std::ostringstream ss;
char host_port[host_port_buf_size];
snprintf(host_port, host_port_buf_size, "%s:%d", FLAGS_server_host.c_str(),
FLAGS_server_port);
std::unique_ptr<grpc::Credentials> creds; std::unique_ptr<grpc::Credentials> creds;
if (FLAGS_service_account_key_file != "") { if (FLAGS_service_account_key_file != "") {
...@@ -86,28 +95,83 @@ int main(int argc, char** argv) { ...@@ -86,28 +95,83 @@ int main(int argc, char** argv) {
creds = grpc::CredentialsFactory::ComputeEngineCredentials(); creds = grpc::CredentialsFactory::ComputeEngineCredentials();
} }
ss << FLAGS_server_host << ":" << FLAGS_server_port;
std::shared_ptr<grpc::ChannelInterface> channel( std::shared_ptr<grpc::ChannelInterface> channel(
grpc::CreateTestChannel( grpc::CreateTestChannel(
host_port, ss.str(),
FLAGS_server_host, FLAGS_server_host,
true, // enable SSL true, // enable SSL
true, // use prod roots true, // use prod roots
creds)); creds));
grpc::examples::tips::Client client(channel); grpc::examples::tips::Publisher publisher(channel);
grpc::examples::tips::Subscriber subscriber(channel);
GPR_ASSERT(FLAGS_project_id != "");
ss.str("");
ss << "/topics/" << FLAGS_project_id << "/" << kTopic;
grpc::string topic = ss.str();
ss.str("");
ss << FLAGS_project_id << "/" << kSubscriptionName;
grpc::string subscription_name = ss.str();
// Clean up test topic and subcription if they exist before.
grpc::string subscription_topic;
if (subscriber.GetSubscription(
subscription_name, &subscription_topic).IsOk()) {
subscriber.DeleteSubscription(subscription_name);
}
if (publisher.GetTopic(topic).IsOk()) publisher.DeleteTopic(topic);
grpc::Status s = publisher.CreateTopic(topic);
gpr_log(GPR_INFO, "Create topic returns code %d, %s",
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
grpc::Status s = client.CreateTopic("/topics/stoked-keyword-656/testtopics"); s = publisher.GetTopic(topic);
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str()); gpr_log(GPR_INFO, "Get topic returns code %d, %s",
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk()); GPR_ASSERT(s.IsOk());
s = client.GetTopic("/topics/stoked-keyword-656/testtopics"); std::vector<grpc::string> topics;
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str()); s = publisher.ListTopics(FLAGS_project_id, &topics);
gpr_log(GPR_INFO, "List topic returns code %d, %s",
s.code(), s.details().c_str());
bool topic_found = false;
for (unsigned int i = 0; i < topics.size(); i++) {
if (topics[i] == topic) topic_found = true;
gpr_log(GPR_INFO, "topic: %s", topics[i].c_str());
}
GPR_ASSERT(s.IsOk());
GPR_ASSERT(topic_found);
s = subscriber.CreateSubscription(topic, subscription_name);
gpr_log(GPR_INFO, "create subscrption returns code %d, %s",
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
s = publisher.Publish(topic, kMessageData);
gpr_log(GPR_INFO, "Publish %s returns code %d, %s",
kMessageData, s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
grpc::string data;
s = subscriber.Pull(subscription_name, &data);
gpr_log(GPR_INFO, "Pull %s", data.c_str());
s = subscriber.DeleteSubscription(subscription_name);
gpr_log(GPR_INFO, "Delete subscription returns code %d, %s",
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk()); GPR_ASSERT(s.IsOk());
s = client.DeleteTopic("/topics/stoked-keyword-656/testtopics"); s = publisher.DeleteTopic(topic);
gpr_log(GPR_INFO, "return code %d, %s", s.code(), s.details().c_str()); gpr_log(GPR_INFO, "Delete topic returns code %d, %s",
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk()); GPR_ASSERT(s.IsOk());
subscriber.Shutdown();
publisher.Shutdown();
channel.reset(); channel.reset();
grpc_shutdown(); grpc_shutdown();
return 0; return 0;
......
...@@ -31,9 +31,11 @@ ...@@ -31,9 +31,11 @@
* *
*/ */
#include <sstream>
#include <grpc++/client_context.h> #include <grpc++/client_context.h>
#include "examples/tips/client.h" #include "examples/tips/publisher.h"
using tech::pubsub::Topic; using tech::pubsub::Topic;
using tech::pubsub::DeleteTopicRequest; using tech::pubsub::DeleteTopicRequest;
...@@ -41,16 +43,22 @@ using tech::pubsub::GetTopicRequest; ...@@ -41,16 +43,22 @@ using tech::pubsub::GetTopicRequest;
using tech::pubsub::PublisherService; using tech::pubsub::PublisherService;
using tech::pubsub::ListTopicsRequest; using tech::pubsub::ListTopicsRequest;
using tech::pubsub::ListTopicsResponse; using tech::pubsub::ListTopicsResponse;
using tech::pubsub::PublishRequest;
using tech::pubsub::PubsubMessage;
namespace grpc { namespace grpc {
namespace examples { namespace examples {
namespace tips { namespace tips {
Client::Client(std::shared_ptr<ChannelInterface> channel) Publisher::Publisher(std::shared_ptr<ChannelInterface> channel)
: stub_(PublisherService::NewStub(channel)) { : stub_(PublisherService::NewStub(channel)) {
} }
Status Client::CreateTopic(grpc::string topic) { void Publisher::Shutdown() {
stub_.reset();
}
Status Publisher::CreateTopic(const grpc::string& topic) {
Topic request; Topic request;
Topic response; Topic response;
request.set_name(topic); request.set_name(topic);
...@@ -59,15 +67,28 @@ Status Client::CreateTopic(grpc::string topic) { ...@@ -59,15 +67,28 @@ Status Client::CreateTopic(grpc::string topic) {
return stub_->CreateTopic(&context, request, &response); return stub_->CreateTopic(&context, request, &response);
} }
Status Client::ListTopics() { Status Publisher::ListTopics(const grpc::string& project_id,
std::vector<grpc::string>* topics) {
ListTopicsRequest request; ListTopicsRequest request;
ListTopicsResponse response; ListTopicsResponse response;
ClientContext context; ClientContext context;
return stub_->ListTopics(&context, request, &response); std::ostringstream ss;
ss << "cloud.googleapis.com/project in (/projects/" << project_id << ")";
request.set_query(ss.str());
Status s = stub_->ListTopics(&context, request, &response);
tech::pubsub::Topic topic;
for (int i = 0; i < response.topic_size(); i++) {
topic = response.topic(i);
topics->push_back(topic.name());
}
return s;
} }
Status Client::GetTopic(grpc::string topic) { Status Publisher::GetTopic(const grpc::string& topic) {
GetTopicRequest request; GetTopicRequest request;
Topic response; Topic response;
ClientContext context; ClientContext context;
...@@ -77,7 +98,7 @@ Status Client::GetTopic(grpc::string topic) { ...@@ -77,7 +98,7 @@ Status Client::GetTopic(grpc::string topic) {
return stub_->GetTopic(&context, request, &response); return stub_->GetTopic(&context, request, &response);
} }
Status Client::DeleteTopic(grpc::string topic) { Status Publisher::DeleteTopic(const grpc::string& topic) {
DeleteTopicRequest request; DeleteTopicRequest request;
proto2::Empty response; proto2::Empty response;
ClientContext context; ClientContext context;
...@@ -87,6 +108,17 @@ Status Client::DeleteTopic(grpc::string topic) { ...@@ -87,6 +108,17 @@ Status Client::DeleteTopic(grpc::string topic) {
return stub_->DeleteTopic(&context, request, &response); return stub_->DeleteTopic(&context, request, &response);
} }
Status Publisher::Publish(const grpc::string& topic, const grpc::string& data) {
PublishRequest request;
proto2::Empty response;
ClientContext context;
request.mutable_message()->set_data(data);
request.set_topic(topic);
return stub_->Publish(&context, request, &response);
}
} // namespace tips } // namespace tips
} // namespace examples } // namespace examples
} // namespace grpc } // namespace grpc
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
* *
*/ */
#ifndef __GRPCPP_EXAMPLES_TIPS_CLIENT_H_ #ifndef __GRPCPP_EXAMPLES_TIPS_PUBLISHER_H_
#define __GRPCPP_EXAMPLES_TIPS_CLIENT_H_ #define __GRPCPP_EXAMPLES_TIPS_PUBLISHER_H_
#include <grpc++/channel_interface.h> #include <grpc++/channel_interface.h>
#include <grpc++/status.h> #include <grpc++/status.h>
...@@ -43,13 +43,18 @@ namespace grpc { ...@@ -43,13 +43,18 @@ namespace grpc {
namespace examples { namespace examples {
namespace tips { namespace tips {
class Client { class Publisher {
public: public:
Client(std::shared_ptr<grpc::ChannelInterface> channel); Publisher(std::shared_ptr<ChannelInterface> channel);
Status CreateTopic(grpc::string topic); void Shutdown();
Status GetTopic(grpc::string topic);
Status DeleteTopic(grpc::string topic); Status CreateTopic(const grpc::string& topic);
Status ListTopics(); Status GetTopic(const grpc::string& topic);
Status DeleteTopic(const grpc::string& topic);
Status ListTopics(const grpc::string& project_id,
std::vector<grpc::string>* topics);
Status Publish(const grpc::string& topic, const grpc::string& data);
private: private:
std::unique_ptr<tech::pubsub::PublisherService::Stub> stub_; std::unique_ptr<tech::pubsub::PublisherService::Stub> stub_;
...@@ -59,4 +64,4 @@ class Client { ...@@ -59,4 +64,4 @@ class Client {
} // namespace examples } // namespace examples
} // namespace grpc } // namespace grpc
#endif // __GRPCPP_EXAMPLES_TIPS_CLIENT_H_ #endif // __GRPCPP_EXAMPLES_TIPS_PUBLISHER_H_
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#include <grpc++/status.h> #include <grpc++/status.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "examples/tips/client.h" #include "examples/tips/publisher.h"
#include "test/core/util/port.h" #include "test/core/util/port.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
...@@ -51,9 +51,11 @@ namespace grpc { ...@@ -51,9 +51,11 @@ namespace grpc {
namespace testing { namespace testing {
namespace { namespace {
const char kProjectId[] = "project id";
const char kTopic[] = "test topic"; const char kTopic[] = "test topic";
const char kMessageData[] = "test message data";
class PublishServiceImpl : public tech::pubsub::PublisherService::Service { class PublisherServiceImpl : public tech::pubsub::PublisherService::Service {
public: public:
Status CreateTopic(::grpc::ServerContext* context, Status CreateTopic(::grpc::ServerContext* context,
const ::tech::pubsub::Topic* request, const ::tech::pubsub::Topic* request,
...@@ -61,34 +63,81 @@ class PublishServiceImpl : public tech::pubsub::PublisherService::Service { ...@@ -61,34 +63,81 @@ class PublishServiceImpl : public tech::pubsub::PublisherService::Service {
EXPECT_EQ(request->name(), kTopic); EXPECT_EQ(request->name(), kTopic);
return Status::OK; return Status::OK;
} }
Status Publish(ServerContext* context,
const ::tech::pubsub::PublishRequest* request,
::proto2::Empty* response) override {
EXPECT_EQ(request->message().data(), kMessageData);
return Status::OK;
}
Status GetTopic(ServerContext* context,
const ::tech::pubsub::GetTopicRequest* request,
::tech::pubsub::Topic* response) override {
EXPECT_EQ(request->topic(), kTopic);
return Status::OK;
}
Status ListTopics(ServerContext* context,
const ::tech::pubsub::ListTopicsRequest* request,
::tech::pubsub::ListTopicsResponse* response) override {
std::ostringstream ss;
ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")";
EXPECT_EQ(request->query(), ss.str());
response->add_topic()->set_name(kTopic);
return Status::OK;
}
Status DeleteTopic(ServerContext* context,
const ::tech::pubsub::DeleteTopicRequest* request,
::proto2::Empty* response) override {
EXPECT_EQ(request->topic(), kTopic);
return Status::OK;
}
}; };
class End2endTest : public ::testing::Test { class PublisherTest : public ::testing::Test {
protected: protected:
// Setup a server and a client for PublisherService.
void SetUp() override { void SetUp() override {
int port = grpc_pick_unused_port_or_die(); int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port; server_address_ << "localhost:" << port;
// Setup server
ServerBuilder builder; ServerBuilder builder;
builder.AddPort(server_address_.str()); builder.AddPort(server_address_.str());
builder.RegisterService(service_.service()); builder.RegisterService(service_.service());
server_ = builder.BuildAndStart(); server_ = builder.BuildAndStart();
channel_ = CreateChannel(server_address_.str(), ChannelArguments()); channel_ = CreateChannel(server_address_.str(), ChannelArguments());
publisher_.reset(new grpc::examples::tips::Publisher(channel_));
} }
void TearDown() override { server_->Shutdown(); } void TearDown() override {
server_->Shutdown();
publisher_->Shutdown();
}
std::unique_ptr<Server> server_;
std::ostringstream server_address_; std::ostringstream server_address_;
PublishServiceImpl service_; std::unique_ptr<Server> server_;
PublisherServiceImpl service_;
std::shared_ptr<ChannelInterface> channel_; std::shared_ptr<ChannelInterface> channel_;
std::unique_ptr<grpc::examples::tips::Publisher> publisher_;
}; };
TEST_F(End2endTest, CreateTopic) { TEST_F(PublisherTest, TestPublisher) {
grpc::examples::tips::Client client(channel_); EXPECT_TRUE(publisher_->CreateTopic(kTopic).IsOk());
client.CreateTopic(kTopic);
EXPECT_TRUE(publisher_->Publish(kTopic, kMessageData).IsOk());
EXPECT_TRUE(publisher_->GetTopic(kTopic).IsOk());
std::vector<grpc::string> topics;
EXPECT_TRUE(publisher_->ListTopics(kProjectId, &topics).IsOk());
EXPECT_EQ(topics.size(), 1);
EXPECT_EQ(topics[0], kTopic);
} }
} // namespace } // namespace
......
// This file will be moved to a new location.
// Specification of the Pubsub API. // Specification of the Pubsub API.
syntax = "proto2"; syntax = "proto2";
......
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc++/client_context.h>
#include "examples/tips/subscriber.h"
using tech::pubsub::Topic;
using tech::pubsub::DeleteTopicRequest;
using tech::pubsub::GetTopicRequest;
using tech::pubsub::SubscriberService;
using tech::pubsub::ListTopicsRequest;
using tech::pubsub::ListTopicsResponse;
using tech::pubsub::PublishRequest;
using tech::pubsub::PubsubMessage;
namespace grpc {
namespace examples {
namespace tips {
Subscriber::Subscriber(std::shared_ptr<ChannelInterface> channel)
: stub_(SubscriberService::NewStub(channel)) {
}
void Subscriber::Shutdown() {
stub_.reset();
}
Status Subscriber::CreateSubscription(const grpc::string& topic,
const grpc::string& name) {
tech::pubsub::Subscription request;
tech::pubsub::Subscription response;
ClientContext context;
request.set_topic(topic);
request.set_name(name);
return stub_->CreateSubscription(&context, request, &response);
}
Status Subscriber::GetSubscription(const grpc::string& name,
grpc::string* topic) {
tech::pubsub::GetSubscriptionRequest request;
tech::pubsub::Subscription response;
ClientContext context;
request.set_subscription(name);
Status s = stub_->GetSubscription(&context, request, &response);
*topic = response.topic();
return s;
}
Status Subscriber::DeleteSubscription(const grpc::string& name) {
tech::pubsub::DeleteSubscriptionRequest request;
proto2::Empty response;
ClientContext context;
request.set_subscription(name);
return stub_->DeleteSubscription(&context, request, &response);
}
Status Subscriber::Pull(const grpc::string& name, grpc::string* data) {
tech::pubsub::PullRequest request;
tech::pubsub::PullResponse response;
ClientContext context;
request.set_subscription(name);
Status s = stub_->Pull(&context, request, &response);
if (s.IsOk()) {
tech::pubsub::PubsubEvent event = response.pubsub_event();
if (event.has_message()) {
*data = event.message().data();
}
tech::pubsub::AcknowledgeRequest ack;
proto2::Empty empty;
ClientContext ack_context;
ack.set_subscription(name);
ack.add_ack_id(response.ack_id());
stub_->Acknowledge(&ack_context, ack, &empty);
}
return s;
}
} // namespace tips
} // namespace examples
} // namespace grpc
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __GRPCPP_EXAMPLES_TIPS_SUBSCRIBER_H_
#define __GRPCPP_EXAMPLES_TIPS_SUBSCRIBER_H_
#include <grpc++/channel_interface.h>
#include <grpc++/status.h>
#include "examples/tips/pubsub.pb.h"
namespace grpc {
namespace examples {
namespace tips {
class Subscriber {
public:
Subscriber(std::shared_ptr<ChannelInterface> channel);
void Shutdown();
Status CreateSubscription(const grpc::string& topic,
const grpc::string& name);
Status GetSubscription(const grpc::string& name, grpc::string* topic);
Status DeleteSubscription(const grpc::string& name);
Status Pull(const grpc::string& name, grpc::string* data);
private:
std::unique_ptr<tech::pubsub::SubscriberService::Stub> stub_;
};
} // namespace tips
} // namespace examples
} // namespace grpc
#endif // __GRPCPP_EXAMPLES_TIPS_SUBSCRIBER_H_
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc++/channel_arguments.h>
#include <grpc++/channel_interface.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/status.h>
#include <gtest/gtest.h>
#include "examples/tips/subscriber.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
namespace grpc {
namespace testing {
namespace {
const char kTopic[] = "test topic";
const char kSubscriptionName[] = "subscription name";
const char kData[] = "Message data";
class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service {
public:
Status CreateSubscription(ServerContext* context,
const tech::pubsub::Subscription* request,
tech::pubsub::Subscription* response) override {
EXPECT_EQ(request->topic(), kTopic);
EXPECT_EQ(request->name(), kSubscriptionName);
return Status::OK;
}
Status GetSubscription(ServerContext* context,
const tech::pubsub::GetSubscriptionRequest* request,
tech::pubsub::Subscription* response) override {
EXPECT_EQ(request->subscription(), kSubscriptionName);
response->set_topic(kTopic);
return Status::OK;
}
Status DeleteSubscription(
ServerContext* context,
const tech::pubsub::DeleteSubscriptionRequest* request,
proto2::Empty* response) override {
EXPECT_EQ(request->subscription(), kSubscriptionName);
return Status::OK;
}
Status Pull(ServerContext* context,
const tech::pubsub::PullRequest* request,
tech::pubsub::PullResponse* response) override {
EXPECT_EQ(request->subscription(), kSubscriptionName);
response->set_ack_id("1");
response->mutable_pubsub_event()->mutable_message()->set_data(kData);
return Status::OK;
}
Status Acknowledge(ServerContext* context,
const tech::pubsub::AcknowledgeRequest* request,
proto2::Empty* response) override {
return Status::OK;
}
};
class SubscriberTest : public ::testing::Test {
protected:
// Setup a server and a client for SubscriberService.
void SetUp() override {
int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddPort(server_address_.str());
builder.RegisterService(service_.service());
server_ = builder.BuildAndStart();
channel_ = CreateChannel(server_address_.str(), ChannelArguments());
subscriber_.reset(new grpc::examples::tips::Subscriber(channel_));
}
void TearDown() override {
server_->Shutdown();
subscriber_->Shutdown();
}
std::ostringstream server_address_;
std::unique_ptr<Server> server_;
SubscriberServiceImpl service_;
std::shared_ptr<ChannelInterface> channel_;
std::unique_ptr<grpc::examples::tips::Subscriber> subscriber_;
};
TEST_F(SubscriberTest, TestSubscriber) {
EXPECT_TRUE(subscriber_->CreateSubscription(kTopic,
kSubscriptionName).IsOk());
grpc::string topic;
EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName,
&topic).IsOk());
EXPECT_EQ(topic, kTopic);
grpc::string data;
EXPECT_TRUE(subscriber_->Pull(kSubscriptionName,
&data).IsOk());
EXPECT_TRUE(subscriber_->DeleteSubscription(kSubscriptionName).IsOk());
}
} // namespace
} // namespace testing
} // namespace grpc
int main(int argc, char** argv) {
grpc_test_init(argc, argv);
grpc_init();
::testing::InitGoogleTest(&argc, argv);
gpr_log(GPR_INFO, "Start test ...");
int result = RUN_ALL_TESTS();
grpc_shutdown();
return result;
}
...@@ -51,6 +51,7 @@ typedef struct delayed_callback { ...@@ -51,6 +51,7 @@ typedef struct delayed_callback {
static gpr_mu g_mu; static gpr_mu g_mu;
static gpr_cv g_cv; static gpr_cv g_cv;
static gpr_cv g_rcv;
static delayed_callback *g_cbs_head = NULL; static delayed_callback *g_cbs_head = NULL;
static delayed_callback *g_cbs_tail = NULL; static delayed_callback *g_cbs_tail = NULL;
static int g_shutdown; static int g_shutdown;
...@@ -86,6 +87,7 @@ void grpc_iomgr_init(void) { ...@@ -86,6 +87,7 @@ void grpc_iomgr_init(void) {
gpr_thd_id id; gpr_thd_id id;
gpr_mu_init(&g_mu); gpr_mu_init(&g_mu);
gpr_cv_init(&g_cv); gpr_cv_init(&g_cv);
gpr_cv_init(&g_rcv);
grpc_alarm_list_init(gpr_now()); grpc_alarm_list_init(gpr_now());
g_refs = 0; g_refs = 0;
grpc_iomgr_platform_init(); grpc_iomgr_platform_init();
...@@ -115,7 +117,7 @@ void grpc_iomgr_shutdown(void) { ...@@ -115,7 +117,7 @@ void grpc_iomgr_shutdown(void) {
gpr_mu_lock(&g_mu); gpr_mu_lock(&g_mu);
} }
if (g_refs) { if (g_refs) {
if (gpr_cv_wait(&g_cv, &g_mu, shutdown_deadline) && g_cbs_head == NULL) { if (gpr_cv_wait(&g_rcv, &g_mu, shutdown_deadline) && g_cbs_head == NULL) {
gpr_log(GPR_DEBUG, gpr_log(GPR_DEBUG,
"Failed to free %d iomgr objects before shutdown deadline: " "Failed to free %d iomgr objects before shutdown deadline: "
"memory leaks are likely", "memory leaks are likely",
...@@ -126,12 +128,14 @@ void grpc_iomgr_shutdown(void) { ...@@ -126,12 +128,14 @@ void grpc_iomgr_shutdown(void) {
} }
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
grpc_kick_poller();
gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future); gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future);
grpc_iomgr_platform_shutdown(); grpc_iomgr_platform_shutdown();
grpc_alarm_list_shutdown(); grpc_alarm_list_shutdown();
gpr_mu_destroy(&g_mu); gpr_mu_destroy(&g_mu);
gpr_cv_destroy(&g_cv); gpr_cv_destroy(&g_cv);
gpr_cv_destroy(&g_rcv);
} }
void grpc_iomgr_ref(void) { void grpc_iomgr_ref(void) {
...@@ -143,7 +147,7 @@ void grpc_iomgr_ref(void) { ...@@ -143,7 +147,7 @@ void grpc_iomgr_ref(void) {
void grpc_iomgr_unref(void) { void grpc_iomgr_unref(void) {
gpr_mu_lock(&g_mu); gpr_mu_lock(&g_mu);
if (0 == --g_refs) { if (0 == --g_refs) {
gpr_cv_signal(&g_cv); gpr_cv_signal(&g_rcv);
} }
gpr_mu_unlock(&g_mu); gpr_mu_unlock(&g_mu);
} }
......
...@@ -31,44 +31,17 @@ ...@@ -31,44 +31,17 @@
* *
*/ */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#ifdef GPR_CPU_LINUX #ifdef GPR_CPU_LINUX
#include "src/core/support/cpu.h" #include "src/core/support/cpu.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#define GRPC_GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#define GRPC_USE_GNU
#endif
#ifndef __USE_MISC
#define __USE_MISC
#define GRPC_USE_MISC
#endif
#include <sched.h> #include <sched.h>
#ifdef GRPC_GNU_SOURCE
#undef _GNU_SOURCE
#undef GRPC_GNU_SOURCE
#endif
#ifdef GRPC_USE_GNU
#undef __USE_GNU
#undef GRPC_USE_GNU
#endif
#ifdef GRPC_USE_MISC
#undef __USE_MISC
#undef GRPC_USE_MISC
#endif
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
......
*.userprefs
test-results
gRPC C#
=======
A C# implementation of gRPC, Google's RPC library.
EXPERIMENTAL ONLY
-----------------
**This gRPC C# implementation is work-in-progress and is not expected to work yet.**
- The implementation is a wrapper around gRPC C core library
- Code only runs under mono currently, building gGRPC C core library under Windows
is in progress.
- It is very possible that some parts of the code will be heavily refactored or
completely rewritten.
CONTENTS
--------
- ext:
The extension library that wraps C API to be more digestible by C#.
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <grpc/support/slice.h>
#include <string.h>
grpc_byte_buffer *string_to_byte_buffer(const char *buffer, size_t len) {
gpr_slice slice = gpr_slice_from_copied_buffer(buffer, len);
grpc_byte_buffer *bb = grpc_byte_buffer_create(&slice, 1);
gpr_slice_unref(slice);
return bb;
}
void grpc_call_start_write_from_copied_buffer(grpc_call *call,
const char *buffer, size_t len,
void *tag, gpr_uint32 flags) {
grpc_byte_buffer *byte_buffer = string_to_byte_buffer(buffer, len);
GPR_ASSERT(grpc_call_start_write_old(call, byte_buffer, tag, flags) ==
GRPC_CALL_OK);
grpc_byte_buffer_destroy(byte_buffer);
}
grpc_completion_type grpc_event_type(const grpc_event *event) {
return event->type;
}
grpc_op_error grpc_event_write_accepted(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_WRITE_ACCEPTED);
return event->data.invoke_accepted;
}
grpc_op_error grpc_event_finish_accepted(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_FINISH_ACCEPTED);
return event->data.finish_accepted;
}
grpc_status_code grpc_event_finished_status(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_FINISHED);
return event->data.finished.status;
}
const char *grpc_event_finished_details(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_FINISHED);
return event->data.finished.details;
}
gpr_intptr grpc_event_read_length(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_READ);
if (!event->data.read) {
return -1;
}
return grpc_byte_buffer_length(event->data.read);
}
/*
* Copies data from read event to a buffer. Fatal error occurs if
* buffer is too small.
*/
void grpc_event_read_copy_to_buffer(const grpc_event *event, char *buffer,
size_t buffer_len) {
grpc_byte_buffer_reader *reader;
gpr_slice slice;
size_t offset = 0;
GPR_ASSERT(event->type == GRPC_READ);
reader = grpc_byte_buffer_reader_create(event->data.read);
GPR_ASSERT(event->data.read);
while (grpc_byte_buffer_reader_next(reader, &slice)) {
size_t len = GPR_SLICE_LENGTH(slice);
GPR_ASSERT(offset + len <= buffer_len);
memcpy(buffer + offset, GPR_SLICE_START_PTR(slice),
GPR_SLICE_LENGTH(slice));
offset += len;
gpr_slice_unref(slice);
}
grpc_byte_buffer_reader_destroy(reader);
}
grpc_call *grpc_event_call(const grpc_event *event) {
/* we only allow this for newly incoming server calls. */
GPR_ASSERT(event->type == GRPC_SERVER_RPC_NEW);
return event->call;
}
const char *grpc_event_server_rpc_new_method(const grpc_event *event) {
GPR_ASSERT(event->type == GRPC_SERVER_RPC_NEW);
return event->data.server_rpc_new.method;
}
grpc_completion_type grpc_completion_queue_next_with_callback(
grpc_completion_queue *cq) {
grpc_event *ev;
grpc_completion_type t;
void (*callback)(grpc_event *);
ev = grpc_completion_queue_next(cq, gpr_inf_future);
t = ev->type;
if (ev->tag) {
/* call the callback in ev->tag */
/* C forbids to cast object pointers to function pointers, so
* we cast to intptr first.
*/
callback = (void (*)(grpc_event *))(gpr_intptr)ev->tag;
(*callback)(ev);
}
grpc_event_finish(ev);
/* return completion type to allow some handling for events that have no
* tag - such as GRPC_QUEUE_SHUTDOWN
*/
return t;
}
...@@ -7,31 +7,25 @@ Directory structure is as generated by the PHP utility ...@@ -7,31 +7,25 @@ Directory structure is as generated by the PHP utility
## ENVIRONMENT ## ENVIRONMENT
To build a PHP environment that works with this extension, download and extract Install `php5` and `php5-dev`.
PHP 5.5 (5.6 may also work), configure it, and install it:
```bash To run the tests, additionally install `php5-readline` and `phpunit`.
apt-get install libxml2 libxml2-dev
curl http://php.net/get/php-5.5.16.tar.gz Alternatively, build and install PHP 5.5 or later from source with standard
tar -xf php-5.5.16.tar.gz configuration options.
cd php-5.5.16
./configure --with-zlib=/usr --with-libxml-dir=ext/libxml --with-openssl=/usr/local/ssl
make
make install
```
To also download and install the patched protoc and PHP code generator: To also download and install protoc and the PHP code generator.
```bash ```bash
apt-get install -y procps apt-get install -y procps
curl -sSL https://get.rvm.io | sudo bash -s stable --ruby curl -sSL https://get.rvm.io | sudo bash -s stable --ruby
git clone sso://team/one-platform-grpc-team/protobuf git clone git@github.com:google/protobuf.git
cd protobuf cd protobuf
./configure ./configure
make make
make install make install
git clone sso://team/one-platform-grpc-team/grpc-php-protobuf-php git clone git@github.com:murgatroid99/Protobuf-PHP.git
cd grpc-php-protobuf-php cd Protobuf-PHP
rake pear:package version=1.0 rake pear:package version=1.0
pear install Protobuf-1.0.tgz pear install Protobuf-1.0.tgz
``` ```
...@@ -52,5 +46,4 @@ This repo now has PHPUnit tests, which can by run by executing ...@@ -52,5 +46,4 @@ This repo now has PHPUnit tests, which can by run by executing
There is also a generated code test (`./bin/run_gen_code_test.sh`), which tests There is also a generated code test (`./bin/run_gen_code_test.sh`), which tests
the stub `./tests/generated_code/math.php` against a running localhost server the stub `./tests/generated_code/math.php` against a running localhost server
serving the math service. That stub is generated from serving the math service. That stub is generated from
`./tests/generated_code/math.proto` with the head of the repo `./tests/generated_code/math.proto`.
`sso://team/one-platform-grpc-team/grpc-php-protobuf-php`.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment