Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <grpc++/client_context.h> |
| 35 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 36 | #include "examples/pubsub/subscriber.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 37 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 38 | using tech::pubsub::Topic; |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 39 | using tech::pubsub::DeleteTopicRequest; |
| 40 | using tech::pubsub::GetTopicRequest; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 41 | using tech::pubsub::SubscriberService; |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 42 | using tech::pubsub::ListTopicsRequest; |
| 43 | using tech::pubsub::ListTopicsResponse; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 44 | using tech::pubsub::PublishRequest; |
| 45 | using tech::pubsub::PubsubMessage; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 46 | |
| 47 | namespace grpc { |
| 48 | namespace examples { |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 49 | namespace pubsub { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 50 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 51 | Subscriber::Subscriber(std::shared_ptr<Channel> channel) |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 52 | : stub_(SubscriberService::NewStub(channel)) {} |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 53 | |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 54 | void Subscriber::Shutdown() { stub_.reset(); } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 55 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 56 | Status Subscriber::CreateSubscription(const grpc::string& topic, |
| 57 | const grpc::string& name) { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 58 | tech::pubsub::Subscription request; |
| 59 | tech::pubsub::Subscription response; |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 60 | ClientContext context; |
| 61 | |
| 62 | request.set_topic(topic); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 63 | request.set_name(name); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 64 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 65 | return stub_->CreateSubscription(&context, request, &response); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 68 | Status Subscriber::GetSubscription(const grpc::string& name, |
| 69 | grpc::string* topic) { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 70 | tech::pubsub::GetSubscriptionRequest request; |
| 71 | tech::pubsub::Subscription response; |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 72 | ClientContext context; |
| 73 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 74 | request.set_subscription(name); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 75 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 76 | Status s = stub_->GetSubscription(&context, request, &response); |
| 77 | *topic = response.topic(); |
| 78 | return s; |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 81 | Status Subscriber::DeleteSubscription(const grpc::string& name) { |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 82 | tech::pubsub::DeleteSubscriptionRequest request; |
| 83 | proto2::Empty response; |
| 84 | ClientContext context; |
| 85 | |
| 86 | request.set_subscription(name); |
| 87 | |
| 88 | return stub_->DeleteSubscription(&context, request, &response); |
| 89 | } |
| 90 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 91 | Status Subscriber::Pull(const grpc::string& name, grpc::string* data) { |
Chen Wang | 0010cda | 2015-02-01 20:44:33 -0800 | [diff] [blame] | 92 | tech::pubsub::PullRequest request; |
| 93 | tech::pubsub::PullResponse response; |
| 94 | ClientContext context; |
| 95 | |
| 96 | request.set_subscription(name); |
| 97 | Status s = stub_->Pull(&context, request, &response); |
| 98 | if (s.IsOk()) { |
| 99 | tech::pubsub::PubsubEvent event = response.pubsub_event(); |
| 100 | if (event.has_message()) { |
| 101 | *data = event.message().data(); |
| 102 | } |
| 103 | tech::pubsub::AcknowledgeRequest ack; |
| 104 | proto2::Empty empty; |
| 105 | ClientContext ack_context; |
| 106 | ack.set_subscription(name); |
| 107 | ack.add_ack_id(response.ack_id()); |
| 108 | stub_->Acknowledge(&ack_context, ack, &empty); |
| 109 | } |
| 110 | return s; |
| 111 | } |
| 112 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 113 | } // namespace pubsub |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 114 | } // namespace examples |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 115 | } // namespace grpc |