mirror of https://github.com/grpc/grpc.git
commit
fd9d0b47e5
45 changed files with 1197 additions and 420 deletions
@ -0,0 +1,26 @@ |
||||
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" |
@ -0,0 +1,118 @@ |
||||
/*
|
||||
* |
||||
* 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
|
@ -0,0 +1,68 @@ |
||||
/*
|
||||
* |
||||
* 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_
|
@ -0,0 +1,157 @@ |
||||
/*
|
||||
* |
||||
* 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; |
||||
} |
@ -0,0 +1,2 @@ |
||||
*.userprefs |
||||
test-results |
@ -0,0 +1,22 @@ |
||||
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#. |
||||
|
@ -0,0 +1,113 @@ |
||||
#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; |
||||
} |
Loading…
Reference in new issue