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 | |
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> |
| 43 | #include <google/gflags.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> |
Chen Wang | 819f755 | 2015-01-22 14:52:49 -0800 | [diff] [blame] | 48 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 49 | #include "examples/pubsub/publisher.h" |
| 50 | #include "examples/pubsub/subscriber.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 51 | #include "test/cpp/util/create_test_channel.h" |
| 52 | |
Chen Wang | 74b3232 | 2015-01-26 11:23:16 -0800 | [diff] [blame] | 53 | DEFINE_int32(server_port, 443, "Server port."); |
| 54 | DEFINE_string(server_host, |
| 55 | "pubsub-staging.googleapis.com", "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 | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 57 | DEFINE_string(service_account_key_file, "", |
| 58 | "Path to service account json key file."); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 59 | DEFINE_string(oauth_scope, |
| 60 | "https://www.googleapis.com/auth/cloud-platform", |
| 61 | "Scope for OAuth tokens."); |
| 62 | |
| 63 | namespace { |
| 64 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 65 | const char kTopic[] = "testtopics"; |
| 66 | const char kSubscriptionName[] = "testsubscription"; |
| 67 | const char kMessageData[] = "Test Data"; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 68 | |
| 69 | } // namespace |
Chen Wang | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 70 | |
| 71 | grpc::string GetServiceAccountJsonKey() { |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 72 | grpc::string json_key; |
Chen Wang | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 73 | if (json_key.empty()) { |
| 74 | std::ifstream json_key_file(FLAGS_service_account_key_file); |
| 75 | std::stringstream key_stream; |
| 76 | key_stream << json_key_file.rdbuf(); |
| 77 | json_key = key_stream.str(); |
| 78 | } |
| 79 | return json_key; |
| 80 | } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 81 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 82 | int main(int argc, char** argv) { |
| 83 | grpc_init(); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 84 | google::ParseCommandLineFlags(&argc, &argv, true); |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 85 | gpr_log(GPR_INFO, "Start PUBSUB client"); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 86 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 87 | std::ostringstream ss; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 88 | |
Chen Wang | 862e23c | 2015-01-28 14:42:26 -0800 | [diff] [blame] | 89 | std::unique_ptr<grpc::Credentials> creds; |
| 90 | if (FLAGS_service_account_key_file != "") { |
| 91 | grpc::string json_key = GetServiceAccountJsonKey(); |
| 92 | creds = grpc::CredentialsFactory::ServiceAccountCredentials( |
| 93 | json_key, FLAGS_oauth_scope, std::chrono::hours(1)); |
| 94 | } else { |
| 95 | creds = grpc::CredentialsFactory::ComputeEngineCredentials(); |
| 96 | } |
| 97 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 98 | ss << FLAGS_server_host << ":" << FLAGS_server_port; |
Chen Wang | 819f755 | 2015-01-22 14:52:49 -0800 | [diff] [blame] | 99 | std::shared_ptr<grpc::ChannelInterface> channel( |
Chen Wang | 5679470 | 2015-01-27 18:36:19 -0800 | [diff] [blame] | 100 | grpc::CreateTestChannel( |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 101 | ss.str(), |
Chen Wang | 5679470 | 2015-01-27 18:36:19 -0800 | [diff] [blame] | 102 | FLAGS_server_host, |
| 103 | true, // enable SSL |
| 104 | true, // use prod roots |
| 105 | creds)); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 106 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 107 | grpc::examples::pubsub::Publisher publisher(channel); |
| 108 | grpc::examples::pubsub::Subscriber subscriber(channel); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 109 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 110 | GPR_ASSERT(FLAGS_project_id != ""); |
| 111 | ss.str(""); |
| 112 | ss << "/topics/" << FLAGS_project_id << "/" << kTopic; |
| 113 | grpc::string topic = ss.str(); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 114 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 115 | ss.str(""); |
| 116 | ss << FLAGS_project_id << "/" << kSubscriptionName; |
| 117 | grpc::string subscription_name = ss.str(); |
| 118 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 119 | // Clean up test topic and subcription if they exist before. |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 120 | grpc::string subscription_topic; |
| 121 | if (subscriber.GetSubscription( |
| 122 | subscription_name, &subscription_topic).IsOk()) { |
| 123 | subscriber.DeleteSubscription(subscription_name); |
| 124 | } |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 125 | if (publisher.GetTopic(topic).IsOk()) publisher.DeleteTopic(topic); |
| 126 | |
| 127 | grpc::Status s = publisher.CreateTopic(topic); |
| 128 | gpr_log(GPR_INFO, "Create topic returns code %d, %s", |
| 129 | s.code(), s.details().c_str()); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 130 | GPR_ASSERT(s.IsOk()); |
| 131 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 132 | s = publisher.GetTopic(topic); |
| 133 | gpr_log(GPR_INFO, "Get topic returns code %d, %s", |
| 134 | s.code(), s.details().c_str()); |
Chen Wang | 3cc1ad6 | 2015-01-28 17:51:32 -0800 | [diff] [blame] | 135 | GPR_ASSERT(s.IsOk()); |
| 136 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 137 | std::vector<grpc::string> topics; |
| 138 | s = publisher.ListTopics(FLAGS_project_id, &topics); |
| 139 | gpr_log(GPR_INFO, "List topic returns code %d, %s", |
| 140 | s.code(), s.details().c_str()); |
| 141 | bool topic_found = false; |
| 142 | for (unsigned int i = 0; i < topics.size(); i++) { |
| 143 | if (topics[i] == topic) topic_found = true; |
| 144 | gpr_log(GPR_INFO, "topic: %s", topics[i].c_str()); |
| 145 | } |
| 146 | GPR_ASSERT(s.IsOk()); |
| 147 | GPR_ASSERT(topic_found); |
| 148 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 149 | s = subscriber.CreateSubscription(topic, subscription_name); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 150 | gpr_log(GPR_INFO, "create subscrption returns code %d, %s", |
| 151 | s.code(), s.details().c_str()); |
| 152 | GPR_ASSERT(s.IsOk()); |
| 153 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 154 | s = publisher.Publish(topic, kMessageData); |
| 155 | gpr_log(GPR_INFO, "Publish %s returns code %d, %s", |
| 156 | kMessageData, s.code(), s.details().c_str()); |
| 157 | GPR_ASSERT(s.IsOk()); |
| 158 | |
| 159 | grpc::string data; |
| 160 | s = subscriber.Pull(subscription_name, &data); |
| 161 | gpr_log(GPR_INFO, "Pull %s", data.c_str()); |
| 162 | |
| 163 | s = subscriber.DeleteSubscription(subscription_name); |
| 164 | gpr_log(GPR_INFO, "Delete subscription returns code %d, %s", |
| 165 | s.code(), s.details().c_str()); |
| 166 | GPR_ASSERT(s.IsOk()); |
| 167 | |
| 168 | s = publisher.DeleteTopic(topic); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 169 | gpr_log(GPR_INFO, "Delete topic returns code %d, %s", |
| 170 | s.code(), s.details().c_str()); |
| 171 | GPR_ASSERT(s.IsOk()); |
| 172 | |
| 173 | subscriber.Shutdown(); |
| 174 | publisher.Shutdown(); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 175 | channel.reset(); |
| 176 | grpc_shutdown(); |
| 177 | return 0; |
| 178 | } |