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