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 | |
Chen Wang | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 34 | #include <fstream> |
| 35 | #include <memory> |
| 36 | #include <sstream> |
| 37 | #include <string> |
| 38 | #include <thread> |
| 39 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 40 | #include <grpc/grpc.h> |
| 41 | #include <grpc/support/log.h> |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 42 | #include <gflags/gflags.h> |
Yang Gao | 7795324 | 2015-02-27 14:28:05 -0800 | [diff] [blame] | 43 | #include <grpc++/channel_arguments.h> |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 44 | #include <grpc++/channel_interface.h> |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 45 | #include <grpc++/create_channel.h> |
Chen Wang | 5679470 | 2015-01-27 18:36:19 -0800 | [diff] [blame] | 46 | #include <grpc++/credentials.h> |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 47 | #include <grpc++/status.h> |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 48 | #include "test/cpp/util/test_config.h" |
Chen Wang | 819f755 | 2015-01-22 14:52:49 -0800 | [diff] [blame] | 49 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 50 | #include "examples/pubsub/publisher.h" |
| 51 | #include "examples/pubsub/subscriber.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 52 | |
Chen Wang | 74b3232 | 2015-01-26 11:23:16 -0800 | [diff] [blame] | 53 | DEFINE_int32(server_port, 443, "Server port."); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 54 | DEFINE_string(server_host, "pubsub-staging.googleapis.com", |
| 55 | "Server host to connect to"); |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 56 | DEFINE_string(project_id, "", "GCE project id such as stoked-keyword-656"); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 57 | |
| 58 | namespace { |
| 59 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 60 | const char kTopic[] = "testtopics"; |
| 61 | const char kSubscriptionName[] = "testsubscription"; |
| 62 | const char kMessageData[] = "Test Data"; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 63 | |
| 64 | } // namespace |
Chen Wang | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 65 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 66 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 67 | grpc::testing::InitTest(&argc, &argv, true); |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 68 | gpr_log(GPR_INFO, "Start PUBSUB client"); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 69 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 70 | std::ostringstream ss; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 71 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 72 | ss << FLAGS_server_host << ":" << FLAGS_server_port; |
Yang Gao | 7795324 | 2015-02-27 14:28:05 -0800 | [diff] [blame] | 73 | |
Craig Tiller | 5589c35 | 2015-03-03 10:04:51 -0800 | [diff] [blame] | 74 | std::unique_ptr<grpc::Credentials> creds = grpc::GoogleDefaultCredentials(); |
Yang Gao | 7795324 | 2015-02-27 14:28:05 -0800 | [diff] [blame] | 75 | std::shared_ptr<grpc::ChannelInterface> channel = |
| 76 | grpc::CreateChannel(ss.str(), creds, grpc::ChannelArguments()); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 77 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 78 | grpc::examples::pubsub::Publisher publisher(channel); |
| 79 | grpc::examples::pubsub::Subscriber subscriber(channel); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 80 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 81 | GPR_ASSERT(FLAGS_project_id != ""); |
| 82 | ss.str(""); |
| 83 | ss << "/topics/" << FLAGS_project_id << "/" << kTopic; |
| 84 | grpc::string topic = ss.str(); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 85 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 86 | ss.str(""); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 87 | ss << FLAGS_project_id << "/" << kSubscriptionName; |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 88 | grpc::string subscription_name = ss.str(); |
| 89 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 90 | // Clean up test topic and subcription if they exist before. |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 91 | grpc::string subscription_topic; |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 92 | if (subscriber.GetSubscription(subscription_name, &subscription_topic) |
| 93 | .IsOk()) { |
Yang Gao | 0706ce8 | 2015-02-27 14:31:02 -0800 | [diff] [blame] | 94 | subscriber.DeleteSubscription(subscription_name); |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 95 | } |
Yang Gao | 7795324 | 2015-02-27 14:28:05 -0800 | [diff] [blame] | 96 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 97 | if (publisher.GetTopic(topic).IsOk()) publisher.DeleteTopic(topic); |
| 98 | |
| 99 | grpc::Status s = publisher.CreateTopic(topic); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 100 | gpr_log(GPR_INFO, "Create topic returns code %d, %s", s.code(), |
| 101 | s.details().c_str()); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 102 | GPR_ASSERT(s.IsOk()); |
| 103 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 104 | s = publisher.GetTopic(topic); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 105 | gpr_log(GPR_INFO, "Get topic returns code %d, %s", s.code(), |
| 106 | s.details().c_str()); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 107 | GPR_ASSERT(s.IsOk()); |
| 108 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 109 | std::vector<grpc::string> topics; |
| 110 | s = publisher.ListTopics(FLAGS_project_id, &topics); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 111 | gpr_log(GPR_INFO, "List topic returns code %d, %s", s.code(), |
| 112 | s.details().c_str()); |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 113 | bool topic_found = false; |
| 114 | for (unsigned int i = 0; i < topics.size(); i++) { |
| 115 | if (topics[i] == topic) topic_found = true; |
| 116 | gpr_log(GPR_INFO, "topic: %s", topics[i].c_str()); |
| 117 | } |
| 118 | GPR_ASSERT(s.IsOk()); |
| 119 | GPR_ASSERT(topic_found); |
| 120 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 121 | s = subscriber.CreateSubscription(topic, subscription_name); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 122 | gpr_log(GPR_INFO, "create subscrption returns code %d, %s", s.code(), |
| 123 | s.details().c_str()); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 124 | GPR_ASSERT(s.IsOk()); |
| 125 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 126 | s = publisher.Publish(topic, kMessageData); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 127 | gpr_log(GPR_INFO, "Publish %s returns code %d, %s", kMessageData, s.code(), |
| 128 | s.details().c_str()); |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 129 | GPR_ASSERT(s.IsOk()); |
| 130 | |
| 131 | grpc::string data; |
| 132 | s = subscriber.Pull(subscription_name, &data); |
| 133 | gpr_log(GPR_INFO, "Pull %s", data.c_str()); |
| 134 | |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 135 | s = subscriber.DeleteSubscription(subscription_name); |
| 136 | gpr_log(GPR_INFO, "Delete subscription returns code %d, %s", s.code(), |
| 137 | s.details().c_str()); |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 138 | GPR_ASSERT(s.IsOk()); |
| 139 | |
| 140 | s = publisher.DeleteTopic(topic); |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 141 | gpr_log(GPR_INFO, "Delete topic returns code %d, %s", s.code(), |
| 142 | s.details().c_str()); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 143 | GPR_ASSERT(s.IsOk()); |
| 144 | |
| 145 | subscriber.Shutdown(); |
| 146 | publisher.Shutdown(); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 147 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 148 | } |