blob: 68620e64c5c280f63632fa68c6f5bdd56b439a49 [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 <fstream>
35#include <memory>
36#include <sstream>
37#include <string>
38#include <thread>
39
Chen Wang86af8cf2015-01-21 18:05:40 -080040#include <grpc/grpc.h>
41#include <grpc/support/log.h>
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010042#include <gflags/gflags.h>
Yang Gao77953242015-02-27 14:28:05 -080043#include <grpc++/channel_arguments.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>
Yang Gao103837e2015-04-15 15:23:54 -070048#include "test/cpp/util/test_config.h"
Chen Wang819f7552015-01-22 14:52:49 -080049
Chen wang84232512015-02-12 17:29:18 -080050#include "examples/pubsub/publisher.h"
51#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080052
Chen Wang74b32322015-01-26 11:23:16 -080053DEFINE_int32(server_port, 443, "Server port.");
Yang Gao884ed082015-03-25 10:45:23 -070054DEFINE_string(server_host, "pubsub-staging.googleapis.com",
55 "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 Wang04f1aa82015-01-30 18:26:16 -080057
58namespace {
59
Chen Wangb532ef82015-02-02 10:45:17 -080060const char kTopic[] = "testtopics";
61const char kSubscriptionName[] = "testsubscription";
62const char kMessageData[] = "Test Data";
Chen Wang04f1aa82015-01-30 18:26:16 -080063
64} // namespace
Chen Wang862e23c2015-01-28 14:42:26 -080065
Chen Wang86af8cf2015-01-21 18:05:40 -080066int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -070067 grpc::testing::InitTest(&argc, &argv, true);
Chen wang84232512015-02-12 17:29:18 -080068 gpr_log(GPR_INFO, "Start PUBSUB client");
Chen Wang86af8cf2015-01-21 18:05:40 -080069
Chen Wangb532ef82015-02-02 10:45:17 -080070 std::ostringstream ss;
Chen Wang86af8cf2015-01-21 18:05:40 -080071
Chen Wangb532ef82015-02-02 10:45:17 -080072 ss << FLAGS_server_host << ":" << FLAGS_server_port;
Yang Gao77953242015-02-27 14:28:05 -080073
Craig Tiller5589c352015-03-03 10:04:51 -080074 std::unique_ptr<grpc::Credentials> creds = grpc::GoogleDefaultCredentials();
Yang Gao77953242015-02-27 14:28:05 -080075 std::shared_ptr<grpc::ChannelInterface> channel =
76 grpc::CreateChannel(ss.str(), creds, grpc::ChannelArguments());
Chen Wang86af8cf2015-01-21 18:05:40 -080077
Chen wang84232512015-02-12 17:29:18 -080078 grpc::examples::pubsub::Publisher publisher(channel);
79 grpc::examples::pubsub::Subscriber subscriber(channel);
Chen Wang3cc1ad62015-01-28 17:51:32 -080080
Chen Wangb532ef82015-02-02 10:45:17 -080081 GPR_ASSERT(FLAGS_project_id != "");
82 ss.str("");
83 ss << "/topics/" << FLAGS_project_id << "/" << kTopic;
84 grpc::string topic = ss.str();
Chen Wang04f1aa82015-01-30 18:26:16 -080085
Chen Wangb532ef82015-02-02 10:45:17 -080086 ss.str("");
Yang Gao884ed082015-03-25 10:45:23 -070087 ss << FLAGS_project_id << "/" << kSubscriptionName;
Chen Wangb532ef82015-02-02 10:45:17 -080088 grpc::string subscription_name = ss.str();
89
Chen Wang405392c2015-02-03 10:28:39 -080090 // Clean up test topic and subcription if they exist before.
Chen Wangb532ef82015-02-02 10:45:17 -080091 grpc::string subscription_topic;
Yang Gao884ed082015-03-25 10:45:23 -070092 if (subscriber.GetSubscription(subscription_name, &subscription_topic)
93 .IsOk()) {
Yang Gao0706ce82015-02-27 14:31:02 -080094 subscriber.DeleteSubscription(subscription_name);
Chen Wangb532ef82015-02-02 10:45:17 -080095 }
Yang Gao77953242015-02-27 14:28:05 -080096
Chen Wang04f1aa82015-01-30 18:26:16 -080097 if (publisher.GetTopic(topic).IsOk()) publisher.DeleteTopic(topic);
98
99 grpc::Status s = publisher.CreateTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700100 gpr_log(GPR_INFO, "Create topic returns code %d, %s", s.code(),
101 s.details().c_str());
Chen Wang3cc1ad62015-01-28 17:51:32 -0800102 GPR_ASSERT(s.IsOk());
103
Chen Wang04f1aa82015-01-30 18:26:16 -0800104 s = publisher.GetTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700105 gpr_log(GPR_INFO, "Get topic returns code %d, %s", s.code(),
106 s.details().c_str());
Chen Wang3cc1ad62015-01-28 17:51:32 -0800107 GPR_ASSERT(s.IsOk());
108
Chen Wang405392c2015-02-03 10:28:39 -0800109 std::vector<grpc::string> topics;
110 s = publisher.ListTopics(FLAGS_project_id, &topics);
Yang Gao884ed082015-03-25 10:45:23 -0700111 gpr_log(GPR_INFO, "List topic returns code %d, %s", s.code(),
112 s.details().c_str());
Chen Wang405392c2015-02-03 10:28:39 -0800113 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 Wangb532ef82015-02-02 10:45:17 -0800121 s = subscriber.CreateSubscription(topic, subscription_name);
Yang Gao884ed082015-03-25 10:45:23 -0700122 gpr_log(GPR_INFO, "create subscrption returns code %d, %s", s.code(),
123 s.details().c_str());
Chen Wang04f1aa82015-01-30 18:26:16 -0800124 GPR_ASSERT(s.IsOk());
125
Chen Wangb532ef82015-02-02 10:45:17 -0800126 s = publisher.Publish(topic, kMessageData);
Yang Gao884ed082015-03-25 10:45:23 -0700127 gpr_log(GPR_INFO, "Publish %s returns code %d, %s", kMessageData, s.code(),
128 s.details().c_str());
Chen Wangb532ef82015-02-02 10:45:17 -0800129 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 Gao884ed082015-03-25 10:45:23 -0700135 s = subscriber.DeleteSubscription(subscription_name);
136 gpr_log(GPR_INFO, "Delete subscription returns code %d, %s", s.code(),
137 s.details().c_str());
Chen Wangb532ef82015-02-02 10:45:17 -0800138 GPR_ASSERT(s.IsOk());
139
140 s = publisher.DeleteTopic(topic);
Yang Gao884ed082015-03-25 10:45:23 -0700141 gpr_log(GPR_INFO, "Delete topic returns code %d, %s", s.code(),
142 s.details().c_str());
Chen Wang04f1aa82015-01-30 18:26:16 -0800143 GPR_ASSERT(s.IsOk());
144
145 subscriber.Shutdown();
146 publisher.Shutdown();
Chen Wang86af8cf2015-01-21 18:05:40 -0800147 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800148}