blob: 18c81c426dc744993f8e1ed83118ce2e7de47a4b [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
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 Wang862e23c2015-01-28 14:42:26 -080034#include <chrono>
35#include <fstream>
36#include <memory>
37#include <sstream>
38#include <string>
39#include <thread>
40
Chen Wang86af8cf2015-01-21 18:05:40 -080041#include <grpc/grpc.h>
42#include <grpc/support/log.h>
43#include <google/gflags.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080044#include <grpc++/channel_interface.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080045#include <grpc++/create_channel.h>
Chen Wang56794702015-01-27 18:36:19 -080046#include <grpc++/credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080047#include <grpc++/status.h>
Chen Wang819f7552015-01-22 14:52:49 -080048
Chen wang84232512015-02-12 17:29:18 -080049#include "examples/pubsub/publisher.h"
50#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080051#include "test/cpp/util/create_test_channel.h"
52
Chen Wang74b32322015-01-26 11:23:16 -080053DEFINE_int32(server_port, 443, "Server port.");
54DEFINE_string(server_host,
55 "pubsub-staging.googleapis.com", "Server host to connect to");
Chen Wangb532ef82015-02-02 10:45:17 -080056DEFINE_string(project_id, "", "GCE project id such as stoked-keyword-656");
Chen Wang862e23c2015-01-28 14:42:26 -080057DEFINE_string(service_account_key_file, "",
58 "Path to service account json key file.");
Chen Wang04f1aa82015-01-30 18:26:16 -080059DEFINE_string(oauth_scope,
60 "https://www.googleapis.com/auth/cloud-platform",
61 "Scope for OAuth tokens.");
62
63namespace {
64
Chen Wangb532ef82015-02-02 10:45:17 -080065const char kTopic[] = "testtopics";
66const char kSubscriptionName[] = "testsubscription";
67const char kMessageData[] = "Test Data";
Chen Wang04f1aa82015-01-30 18:26:16 -080068
69} // namespace
Chen Wang862e23c2015-01-28 14:42:26 -080070
71grpc::string GetServiceAccountJsonKey() {
Chen Wang405392c2015-02-03 10:28:39 -080072 grpc::string json_key;
Chen Wang862e23c2015-01-28 14:42:26 -080073 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 Wang86af8cf2015-01-21 18:05:40 -080081
Chen Wang86af8cf2015-01-21 18:05:40 -080082int main(int argc, char** argv) {
83 grpc_init();
Chen Wang86af8cf2015-01-21 18:05:40 -080084 google::ParseCommandLineFlags(&argc, &argv, true);
Chen wang84232512015-02-12 17:29:18 -080085 gpr_log(GPR_INFO, "Start PUBSUB client");
Chen Wang86af8cf2015-01-21 18:05:40 -080086
Chen Wangb532ef82015-02-02 10:45:17 -080087 std::ostringstream ss;
Chen Wang86af8cf2015-01-21 18:05:40 -080088
Chen Wang862e23c2015-01-28 14:42:26 -080089 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 Wangb532ef82015-02-02 10:45:17 -080098 ss << FLAGS_server_host << ":" << FLAGS_server_port;
Chen Wang819f7552015-01-22 14:52:49 -080099 std::shared_ptr<grpc::ChannelInterface> channel(
Chen Wang56794702015-01-27 18:36:19 -0800100 grpc::CreateTestChannel(
Chen Wangb532ef82015-02-02 10:45:17 -0800101 ss.str(),
Chen Wang56794702015-01-27 18:36:19 -0800102 FLAGS_server_host,
103 true, // enable SSL
104 true, // use prod roots
105 creds));
Chen Wang86af8cf2015-01-21 18:05:40 -0800106
Chen wang84232512015-02-12 17:29:18 -0800107 grpc::examples::pubsub::Publisher publisher(channel);
108 grpc::examples::pubsub::Subscriber subscriber(channel);
Chen Wang3cc1ad62015-01-28 17:51:32 -0800109
Chen Wangb532ef82015-02-02 10:45:17 -0800110 GPR_ASSERT(FLAGS_project_id != "");
111 ss.str("");
112 ss << "/topics/" << FLAGS_project_id << "/" << kTopic;
113 grpc::string topic = ss.str();
Chen Wang04f1aa82015-01-30 18:26:16 -0800114
Chen Wangb532ef82015-02-02 10:45:17 -0800115 ss.str("");
116 ss << FLAGS_project_id << "/" << kSubscriptionName;
117 grpc::string subscription_name = ss.str();
118
Chen Wang405392c2015-02-03 10:28:39 -0800119 // Clean up test topic and subcription if they exist before.
Chen Wangb532ef82015-02-02 10:45:17 -0800120 grpc::string subscription_topic;
121 if (subscriber.GetSubscription(
122 subscription_name, &subscription_topic).IsOk()) {
123 subscriber.DeleteSubscription(subscription_name);
124 }
Chen Wang04f1aa82015-01-30 18:26:16 -0800125 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 Wang3cc1ad62015-01-28 17:51:32 -0800130 GPR_ASSERT(s.IsOk());
131
Chen Wang04f1aa82015-01-30 18:26:16 -0800132 s = publisher.GetTopic(topic);
133 gpr_log(GPR_INFO, "Get topic returns code %d, %s",
134 s.code(), s.details().c_str());
Chen Wang3cc1ad62015-01-28 17:51:32 -0800135 GPR_ASSERT(s.IsOk());
136
Chen Wang405392c2015-02-03 10:28:39 -0800137 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 Wangb532ef82015-02-02 10:45:17 -0800149 s = subscriber.CreateSubscription(topic, subscription_name);
Chen Wang04f1aa82015-01-30 18:26:16 -0800150 gpr_log(GPR_INFO, "create subscrption returns code %d, %s",
151 s.code(), s.details().c_str());
152 GPR_ASSERT(s.IsOk());
153
Chen Wangb532ef82015-02-02 10:45:17 -0800154 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 Wang04f1aa82015-01-30 18:26:16 -0800169 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 Wang86af8cf2015-01-21 18:05:40 -0800175 channel.reset();
176 grpc_shutdown();
177 return 0;
178}