blob: 29f6635b7c9221baf8367973c5f23ad81d986fae [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Chen Wang86af8cf2015-01-21 18:05:40 -08004 * 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 wang84232512015-02-12 17:29:18 -080036#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080037
Chen Wang86af8cf2015-01-21 18:05:40 -080038using tech::pubsub::Topic;
Chen Wang3cc1ad62015-01-28 17:51:32 -080039using tech::pubsub::DeleteTopicRequest;
40using tech::pubsub::GetTopicRequest;
Chen Wang04f1aa82015-01-30 18:26:16 -080041using tech::pubsub::SubscriberService;
Chen Wang3cc1ad62015-01-28 17:51:32 -080042using tech::pubsub::ListTopicsRequest;
43using tech::pubsub::ListTopicsResponse;
Chen Wang04f1aa82015-01-30 18:26:16 -080044using tech::pubsub::PublishRequest;
45using tech::pubsub::PubsubMessage;
Chen Wang86af8cf2015-01-21 18:05:40 -080046
47namespace grpc {
48namespace examples {
Chen wang84232512015-02-12 17:29:18 -080049namespace pubsub {
Chen Wang86af8cf2015-01-21 18:05:40 -080050
Chen Wang04f1aa82015-01-30 18:26:16 -080051Subscriber::Subscriber(std::shared_ptr<ChannelInterface> channel)
52 : stub_(SubscriberService::NewStub(channel)) {
Chen Wang86af8cf2015-01-21 18:05:40 -080053}
54
Chen Wang04f1aa82015-01-30 18:26:16 -080055void Subscriber::Shutdown() {
56 stub_.reset();
Chen Wang86af8cf2015-01-21 18:05:40 -080057}
58
Chen Wang405392c2015-02-03 10:28:39 -080059Status Subscriber::CreateSubscription(const grpc::string& topic,
60 const grpc::string& name) {
Chen Wang04f1aa82015-01-30 18:26:16 -080061 tech::pubsub::Subscription request;
62 tech::pubsub::Subscription response;
Chen Wang3cc1ad62015-01-28 17:51:32 -080063 ClientContext context;
64
65 request.set_topic(topic);
Chen Wang04f1aa82015-01-30 18:26:16 -080066 request.set_name(name);
Chen Wang3cc1ad62015-01-28 17:51:32 -080067
Chen Wang04f1aa82015-01-30 18:26:16 -080068 return stub_->CreateSubscription(&context, request, &response);
Chen Wang3cc1ad62015-01-28 17:51:32 -080069}
70
Chen Wang405392c2015-02-03 10:28:39 -080071Status Subscriber::GetSubscription(const grpc::string& name,
72 grpc::string* topic) {
Chen Wang04f1aa82015-01-30 18:26:16 -080073 tech::pubsub::GetSubscriptionRequest request;
74 tech::pubsub::Subscription response;
Chen Wang3cc1ad62015-01-28 17:51:32 -080075 ClientContext context;
76
Chen Wang04f1aa82015-01-30 18:26:16 -080077 request.set_subscription(name);
Chen Wang3cc1ad62015-01-28 17:51:32 -080078
Chen Wang04f1aa82015-01-30 18:26:16 -080079 Status s = stub_->GetSubscription(&context, request, &response);
80 *topic = response.topic();
81 return s;
Chen Wang3cc1ad62015-01-28 17:51:32 -080082}
83
Chen Wang405392c2015-02-03 10:28:39 -080084Status Subscriber::DeleteSubscription(const grpc::string& name) {
Chen Wangb532ef82015-02-02 10:45:17 -080085 tech::pubsub::DeleteSubscriptionRequest request;
86 proto2::Empty response;
87 ClientContext context;
88
89 request.set_subscription(name);
90
91 return stub_->DeleteSubscription(&context, request, &response);
92}
93
Chen Wang405392c2015-02-03 10:28:39 -080094Status Subscriber::Pull(const grpc::string& name, grpc::string* data) {
Chen Wang0010cda2015-02-01 20:44:33 -080095 tech::pubsub::PullRequest request;
96 tech::pubsub::PullResponse response;
97 ClientContext context;
98
99 request.set_subscription(name);
100 Status s = stub_->Pull(&context, request, &response);
101 if (s.IsOk()) {
102 tech::pubsub::PubsubEvent event = response.pubsub_event();
103 if (event.has_message()) {
104 *data = event.message().data();
105 }
106 tech::pubsub::AcknowledgeRequest ack;
107 proto2::Empty empty;
108 ClientContext ack_context;
109 ack.set_subscription(name);
110 ack.add_ack_id(response.ack_id());
111 stub_->Acknowledge(&ack_context, ack, &empty);
112 }
113 return s;
114}
115
Chen wang84232512015-02-12 17:29:18 -0800116} // namespace pubsub
Chen Wang86af8cf2015-01-21 18:05:40 -0800117} // namespace examples
Craig Tiller190d3602015-02-18 09:23:38 -0800118} // namespace grpc