blob: 085e6c5b6f8f168cedfca2461ca11281d5e7a60c [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
3 * Copyright 2014, Google Inc.
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
Chen Wang405392c2015-02-03 10:28:39 -080034#include <sstream>
35
Chen Wang86af8cf2015-01-21 18:05:40 -080036#include <grpc++/client_context.h>
37
Chen Wang04f1aa82015-01-30 18:26:16 -080038#include "examples/tips/publisher.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080039
Chen Wang86af8cf2015-01-21 18:05:40 -080040using tech::pubsub::Topic;
Chen Wang3cc1ad62015-01-28 17:51:32 -080041using tech::pubsub::DeleteTopicRequest;
42using tech::pubsub::GetTopicRequest;
Chen Wang86af8cf2015-01-21 18:05:40 -080043using tech::pubsub::PublisherService;
Chen Wang3cc1ad62015-01-28 17:51:32 -080044using tech::pubsub::ListTopicsRequest;
45using tech::pubsub::ListTopicsResponse;
Chen Wang04f1aa82015-01-30 18:26:16 -080046using tech::pubsub::PublishRequest;
47using tech::pubsub::PubsubMessage;
Chen Wang86af8cf2015-01-21 18:05:40 -080048
49namespace grpc {
50namespace examples {
51namespace tips {
52
Chen Wang04f1aa82015-01-30 18:26:16 -080053Publisher::Publisher(std::shared_ptr<ChannelInterface> channel)
Chen Wang86af8cf2015-01-21 18:05:40 -080054 : stub_(PublisherService::NewStub(channel)) {
55}
56
Chen Wang04f1aa82015-01-30 18:26:16 -080057void Publisher::Shutdown() {
58 stub_.reset();
59}
60
Chen Wang405392c2015-02-03 10:28:39 -080061Status Publisher::CreateTopic(const grpc::string& topic) {
Chen Wang86af8cf2015-01-21 18:05:40 -080062 Topic request;
63 Topic response;
64 request.set_name(topic);
Chen Wangd34c6902015-01-22 15:12:20 -080065 ClientContext context;
Chen Wang86af8cf2015-01-21 18:05:40 -080066
67 return stub_->CreateTopic(&context, request, &response);
68}
69
Chen Wang405392c2015-02-03 10:28:39 -080070Status Publisher::ListTopics(const grpc::string& project_id,
71 std::vector<grpc::string>* topics) {
Chen Wang3cc1ad62015-01-28 17:51:32 -080072 ListTopicsRequest request;
73 ListTopicsResponse response;
74 ClientContext context;
75
Chen Wang405392c2015-02-03 10:28:39 -080076 std::stringstream ss;
77 ss << "cloud.googleapis.com/project in (/projects/" << project_id << ")";
78 request.set_query(ss.str());
79
80 Status s = stub_->ListTopics(&context, request, &response);
81
82 tech::pubsub::Topic topic;
83 for (int i = 0; i < response.topic_size(); i++) {
84 topic = response.topic(i);
85 topics->push_back(topic.name());
86 }
87
88 return s;
Chen Wang3cc1ad62015-01-28 17:51:32 -080089}
90
Chen Wang405392c2015-02-03 10:28:39 -080091Status Publisher::GetTopic(const grpc::string& topic) {
Chen Wang3cc1ad62015-01-28 17:51:32 -080092 GetTopicRequest request;
93 Topic response;
94 ClientContext context;
95
96 request.set_topic(topic);
97
98 return stub_->GetTopic(&context, request, &response);
99}
100
Chen Wang405392c2015-02-03 10:28:39 -0800101Status Publisher::DeleteTopic(const grpc::string& topic) {
Chen Wang3cc1ad62015-01-28 17:51:32 -0800102 DeleteTopicRequest request;
103 proto2::Empty response;
104 ClientContext context;
105
106 request.set_topic(topic);
107
108 return stub_->DeleteTopic(&context, request, &response);
109}
110
Chen Wang405392c2015-02-03 10:28:39 -0800111Status Publisher::Publish(const grpc::string& topic, const grpc::string& data) {
Chen Wang04f1aa82015-01-30 18:26:16 -0800112 PublishRequest request;
113 proto2::Empty response;
114 ClientContext context;
115
116 request.mutable_message()->set_data(data);
117 request.set_topic(topic);
118
119 return stub_->Publish(&context, request, &response);
120}
121
Chen Wang819f7552015-01-22 14:52:49 -0800122} // namespace tips
Chen Wang86af8cf2015-01-21 18:05:40 -0800123} // namespace examples
124} // namespace grpc