blob: 3a403481fcd81facd0d1ef9ebffce2aa9733dda0 [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Chen Wang86af8cf2015-01-21 18:05:40 -08004 * 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>
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010043#include <gflags/gflags.h>
Yang Gao77953242015-02-27 14:28:05 -080044#include <grpc++/channel_arguments.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080045#include <grpc++/channel_interface.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080046#include <grpc++/create_channel.h>
Chen Wang56794702015-01-27 18:36:19 -080047#include <grpc++/credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080048#include <grpc++/status.h>
Yang Gao103837e2015-04-15 15:23:54 -070049#include "test/cpp/util/test_config.h"
Chen Wang819f7552015-01-22 14:52:49 -080050
Chen wang84232512015-02-12 17:29:18 -080051#include "examples/pubsub/publisher.h"
52#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080053
Chen Wang74b32322015-01-26 11:23:16 -080054DEFINE_int32(server_port, 443, "Server port.");
Yang Gao884ed082015-03-25 10:45:23 -070055DEFINE_string(server_host, "pubsub-staging.googleapis.com",
56 "Server host to connect to");
Chen Wangb532ef82015-02-02 10:45:17 -080057DEFINE_string(project_id, "", "GCE project id such as stoked-keyword-656");
Chen Wang04f1aa82015-01-30 18:26:16 -080058
59namespace {
60
Chen Wangb532ef82015-02-02 10:45:17 -080061const char kTopic[] = "testtopics";
62const char kSubscriptionName[] = "testsubscription";
63const char kMessageData[] = "Test Data";
Chen Wang04f1aa82015-01-30 18:26:16 -080064
65} // namespace
Chen Wang862e23c2015-01-28 14:42:26 -080066
Chen Wang86af8cf2015-01-21 18:05:40 -080067int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -070068 grpc::testing::InitTest(&argc, &argv, true);
Chen wang84232512015-02-12 17:29:18 -080069 gpr_log(GPR_INFO, "Start PUBSUB client");
Chen Wang86af8cf2015-01-21 18:05:40 -080070
Chen Wangb532ef82015-02-02 10:45:17 -080071 std::ostringstream ss;
Chen Wang86af8cf2015-01-21 18:05:40 -080072
Chen Wangb532ef82015-02-02 10:45:17 -080073 ss << FLAGS_server_host << ":" << FLAGS_server_port;
Yang Gao77953242015-02-27 14:28:05 -080074
Craig Tiller5589c352015-03-03 10:04:51 -080075 std::unique_ptr<grpc::Credentials> creds = grpc::GoogleDefaultCredentials();
Yang Gao77953242015-02-27 14:28:05 -080076 std::shared_ptr<grpc::ChannelInterface> channel =
77 grpc::CreateChannel(ss.str(), creds, grpc::ChannelArguments());
Chen Wang86af8cf2015-01-21 18:05:40 -080078
Chen wang84232512015-02-12 17:29:18 -080079 grpc::examples::pubsub::Publisher publisher(channel);
80 grpc::examples::pubsub::Subscriber subscriber(channel);
Chen Wang3cc1ad62015-01-28 17:51:32 -080081
Chen Wangb532ef82015-02-02 10:45:17 -080082 GPR_ASSERT(FLAGS_project_id != "");
83 ss.str("");
84 ss << "/topics/" << FLAGS_project_id << "/" << kTopic;
85 grpc::string topic = ss.str();
Chen Wang04f1aa82015-01-30 18:26:16 -080086
Chen Wangb532ef82015-02-02 10:45:17 -080087 ss.str("");
Yang Gao884ed082015-03-25 10:45:23 -070088 ss << FLAGS_project_id << "/" << kSubscriptionName;
Chen Wangb532ef82015-02-02 10:45:17 -080089 grpc::string subscription_name = ss.str();
90
Chen Wang405392c2015-02-03 10:28:39 -080091 // Clean up test topic and subcription if they exist before.
Chen Wangb532ef82015-02-02 10:45:17 -080092 grpc::string subscription_topic;
Yang Gao884ed082015-03-25 10:45:23 -070093 if (subscriber.GetSubscription(subscription_name, &subscription_topic)
94 .IsOk()) {
Yang Gao0706ce82015-02-27 14:31:02 -080095 subscriber.DeleteSubscription(subscription_name);
Chen Wangb532ef82015-02-02 10:45:17 -080096 }
Yang Gao77953242015-02-27 14:28:05 -080097
Chen Wang04f1aa82015-01-30 18:26:16 -080098 if (publisher.GetTopic(topic).IsOk()) publisher.DeleteTopic(topic);
99
100 grpc::Status s = publisher.CreateTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700101 gpr_log(GPR_INFO, "Create topic returns code %d, %s", s.code(),
102 s.details().c_str());
Chen Wang3cc1ad62015-01-28 17:51:32 -0800103 GPR_ASSERT(s.IsOk());
104
Chen Wang04f1aa82015-01-30 18:26:16 -0800105 s = publisher.GetTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700106 gpr_log(GPR_INFO, "Get topic returns code %d, %s", s.code(),
107 s.details().c_str());
Chen Wang3cc1ad62015-01-28 17:51:32 -0800108 GPR_ASSERT(s.IsOk());
109
Chen Wang405392c2015-02-03 10:28:39 -0800110 std::vector<grpc::string> topics;
111 s = publisher.ListTopics(FLAGS_project_id, &topics);
Yang Gao884ed082015-03-25 10:45:23 -0700112 gpr_log(GPR_INFO, "List topic returns code %d, %s", s.code(),
113 s.details().c_str());
Chen Wang405392c2015-02-03 10:28:39 -0800114 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 Wangb532ef82015-02-02 10:45:17 -0800122 s = subscriber.CreateSubscription(topic, subscription_name);
Yang Gao884ed082015-03-25 10:45:23 -0700123 gpr_log(GPR_INFO, "create subscrption returns code %d, %s", s.code(),
124 s.details().c_str());
Chen Wang04f1aa82015-01-30 18:26:16 -0800125 GPR_ASSERT(s.IsOk());
126
Chen Wangb532ef82015-02-02 10:45:17 -0800127 s = publisher.Publish(topic, kMessageData);
Yang Gao884ed082015-03-25 10:45:23 -0700128 gpr_log(GPR_INFO, "Publish %s returns code %d, %s", kMessageData, s.code(),
129 s.details().c_str());
Chen Wangb532ef82015-02-02 10:45:17 -0800130 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 Gao884ed082015-03-25 10:45:23 -0700136 s = subscriber.DeleteSubscription(subscription_name);
137 gpr_log(GPR_INFO, "Delete subscription returns code %d, %s", s.code(),
138 s.details().c_str());
Chen Wangb532ef82015-02-02 10:45:17 -0800139 GPR_ASSERT(s.IsOk());
140
141 s = publisher.DeleteTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700142 gpr_log(GPR_INFO, "Delete topic returns code %d, %s", s.code(),
143 s.details().c_str());
Chen Wang04f1aa82015-01-30 18:26:16 -0800144 GPR_ASSERT(s.IsOk());
145
146 subscriber.Shutdown();
147 publisher.Shutdown();
Chen Wang86af8cf2015-01-21 18:05:40 -0800148 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800149}