Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | #include <grpc++/client_context.h> |
| 35 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 36 | #include "examples/tips/publisher.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 | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 41 | using tech::pubsub::PublisherService; |
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 { |
| 49 | namespace tips { |
| 50 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 51 | Publisher::Publisher(std::shared_ptr<ChannelInterface> channel) |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 52 | : stub_(PublisherService::NewStub(channel)) { |
| 53 | } |
| 54 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 55 | void Publisher::Shutdown() { |
| 56 | stub_.reset(); |
| 57 | } |
| 58 | |
| 59 | Status Publisher::CreateTopic(grpc::string topic) { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 60 | Topic request; |
| 61 | Topic response; |
| 62 | request.set_name(topic); |
Chen Wang | d34c690 | 2015-01-22 15:12:20 -0800 | [diff] [blame] | 63 | ClientContext context; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 64 | |
| 65 | return stub_->CreateTopic(&context, request, &response); |
| 66 | } |
| 67 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 68 | Status Publisher::ListTopics() { |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 69 | ListTopicsRequest request; |
| 70 | ListTopicsResponse response; |
| 71 | ClientContext context; |
| 72 | |
| 73 | return stub_->ListTopics(&context, request, &response); |
| 74 | } |
| 75 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 76 | Status Publisher::GetTopic(grpc::string topic) { |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 77 | GetTopicRequest request; |
| 78 | Topic response; |
| 79 | ClientContext context; |
| 80 | |
| 81 | request.set_topic(topic); |
| 82 | |
| 83 | return stub_->GetTopic(&context, request, &response); |
| 84 | } |
| 85 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 86 | Status Publisher::DeleteTopic(grpc::string topic) { |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 87 | DeleteTopicRequest request; |
| 88 | proto2::Empty response; |
| 89 | ClientContext context; |
| 90 | |
| 91 | request.set_topic(topic); |
| 92 | |
| 93 | return stub_->DeleteTopic(&context, request, &response); |
| 94 | } |
| 95 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 96 | Status Publisher::Publish(const grpc::string& topic, |
| 97 | const grpc::string& data) { |
| 98 | PublishRequest request; |
| 99 | proto2::Empty response; |
| 100 | ClientContext context; |
| 101 | |
| 102 | request.mutable_message()->set_data(data); |
| 103 | request.set_topic(topic); |
| 104 | |
| 105 | return stub_->Publish(&context, request, &response); |
| 106 | } |
| 107 | |
Chen Wang | 819f755 | 2015-01-22 14:52:49 -0800 | [diff] [blame] | 108 | } // namespace tips |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 109 | } // namespace examples |
| 110 | } // namespace grpc |