blob: 458050af739f799f0a58a1c8d555aab31ffaa96a [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
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 wang84232512015-02-12 17:29:18 -080038#include "examples/pubsub/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 {
Chen wang84232512015-02-12 17:29:18 -080051namespace pubsub {
Chen Wang86af8cf2015-01-21 18:05:40 -080052
Chen Wang04f1aa82015-01-30 18:26:16 -080053Publisher::Publisher(std::shared_ptr<ChannelInterface> channel)
Yang Gao884ed082015-03-25 10:45:23 -070054 : stub_(PublisherService::NewStub(channel)) {}
Chen Wang86af8cf2015-01-21 18:05:40 -080055
Yang Gao884ed082015-03-25 10:45:23 -070056void Publisher::Shutdown() { stub_.reset(); }
Chen Wang04f1aa82015-01-30 18:26:16 -080057
Chen Wang405392c2015-02-03 10:28:39 -080058Status Publisher::CreateTopic(const grpc::string& topic) {
Chen Wang86af8cf2015-01-21 18:05:40 -080059 Topic request;
60 Topic response;
61 request.set_name(topic);
Chen Wangd34c6902015-01-22 15:12:20 -080062 ClientContext context;
Chen Wang86af8cf2015-01-21 18:05:40 -080063
64 return stub_->CreateTopic(&context, request, &response);
65}
66
Chen Wang405392c2015-02-03 10:28:39 -080067Status Publisher::ListTopics(const grpc::string& project_id,
68 std::vector<grpc::string>* topics) {
Chen Wang3cc1ad62015-01-28 17:51:32 -080069 ListTopicsRequest request;
70 ListTopicsResponse response;
71 ClientContext context;
72
Chen Wangcde34e02015-02-03 10:51:24 -080073 std::ostringstream ss;
Chen Wang405392c2015-02-03 10:28:39 -080074 ss << "cloud.googleapis.com/project in (/projects/" << project_id << ")";
75 request.set_query(ss.str());
76
77 Status s = stub_->ListTopics(&context, request, &response);
78
79 tech::pubsub::Topic topic;
80 for (int i = 0; i < response.topic_size(); i++) {
81 topic = response.topic(i);
82 topics->push_back(topic.name());
83 }
84
85 return s;
Chen Wang3cc1ad62015-01-28 17:51:32 -080086}
87
Chen Wang405392c2015-02-03 10:28:39 -080088Status Publisher::GetTopic(const grpc::string& topic) {
Chen Wang3cc1ad62015-01-28 17:51:32 -080089 GetTopicRequest request;
90 Topic response;
91 ClientContext context;
92
93 request.set_topic(topic);
94
95 return stub_->GetTopic(&context, request, &response);
96}
97
Chen Wang405392c2015-02-03 10:28:39 -080098Status Publisher::DeleteTopic(const grpc::string& topic) {
Chen Wang3cc1ad62015-01-28 17:51:32 -080099 DeleteTopicRequest request;
100 proto2::Empty response;
101 ClientContext context;
102
103 request.set_topic(topic);
104
105 return stub_->DeleteTopic(&context, request, &response);
106}
107
Chen Wang405392c2015-02-03 10:28:39 -0800108Status Publisher::Publish(const grpc::string& topic, const grpc::string& data) {
Chen Wang04f1aa82015-01-30 18:26:16 -0800109 PublishRequest request;
110 proto2::Empty response;
111 ClientContext context;
112
113 request.mutable_message()->set_data(data);
114 request.set_topic(topic);
115
116 return stub_->Publish(&context, request, &response);
117}
118
Chen wang84232512015-02-12 17:29:18 -0800119} // namespace pubsub
Chen Wang86af8cf2015-01-21 18:05:40 -0800120} // namespace examples
Craig Tiller190d3602015-02-18 09:23:38 -0800121} // namespace grpc