This repository was archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.cpp
197 lines (162 loc) · 6.66 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (c) 2013 Matthew Stump
This file is part of libcql.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <boost/bind.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <libcql/cql.hpp>
#include <libcql/cql_error.hpp>
#include <libcql/cql_event.hpp>
#include <libcql/cql_client.hpp>
#include <libcql/cql_client_factory.hpp>
#include <libcql/cql_client_pool.hpp>
#include <libcql/cql_client_pool_factory.hpp>
#include <libcql/cql_execute.hpp>
#include <libcql/cql_result.hpp>
// helper function to print query results
void
print_rows(
cql::cql_result_t& result)
{
while (result.next()) {
for (size_t i = 0; i < result.column_count(); ++i) {
cql::cql_byte_t* data = NULL;
cql::cql_int_t size = 0;
result.get_data(i, &data, size);
std::cout.write(reinterpret_cast<char*>(data), size);
std::cout << " | ";
}
std::cout << std::endl;
}
}
// This function is called asynchronously every time an event is logged
void
log_callback(
const cql::cql_short_t,
const std::string& message)
{
std::cout << "LOG: " << message << std::endl;
}
// This is a non-SSL client factory
struct client_functor_t
{
public:
client_functor_t(boost::asio::io_service& service,
cql::cql_client_t::cql_log_callback_t log_callback) :
_io_service(service),
_log_callback(log_callback)
{}
cql::cql_client_t*
operator()()
{
// called every time the pool needs to initiate a new connection to a host
return cql::cql_client_factory_t::create_cql_client_t(_io_service, _log_callback);
}
private:
boost::asio::io_service& _io_service;
cql::cql_client_t::cql_log_callback_t _log_callback;
};
// This is an SSL client factory
struct client_ssl_functor_t
{
public:
client_ssl_functor_t(boost::asio::io_service& service,
boost::asio::ssl::context& context,
cql::cql_client_t::cql_log_callback_t log_callback) :
_io_service(service),
_ssl_ctx(context),
_log_callback(log_callback)
{}
cql::cql_client_t*
operator()()
{
// called every time the pool needs to initiate a new connection to a host
return cql::cql_client_factory_t::create_cql_client_t(_io_service, _ssl_ctx, _log_callback);
}
private:
boost::asio::io_service& _io_service;
boost::asio::ssl::context& _ssl_ctx;
cql::cql_client_t::cql_log_callback_t _log_callback;
};
int
main(int argc,
char**)
{
try
{
// Initialize the IO service, this allows us to perform network operations asyncronously
boost::asio::io_service io_service;
// Typically async operations are performed in the thread performing the request, because we want synchronous behavior
// we're going to spawn a thread whose sole purpose is to perform network communication, and we'll use this thread to
// initiate and check the status of requests.
//
// Also, typically the boost::asio::io_service::run will exit as soon as it's work is done, which we want to prevent
// because it's in it's own thread. Using boost::asio::io_service::work prevents the thread from exiting.
std::auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(io_service));
boost::thread thread(boost::bind(static_cast<size_t(boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io_service));
// decide which client factory we want, SSL or non-SSL. This is a hack, if you pass any commandline arg to the
// binary it will use the SSL factory, non-SSL by default
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
cql::cql_client_pool_t::cql_client_callback_t client_factory;
if (argc > 1) {
client_factory = client_ssl_functor_t(io_service, ctx, &log_callback);
}
else {
client_factory = client_functor_t(io_service, &log_callback);
}
// Construct the pool
std::auto_ptr<cql::cql_client_pool_t> pool(cql::cql_client_pool_factory_t::create_client_pool_t(client_factory, NULL, NULL));
// Add a client to the pool, this operation returns a future.
boost::shared_future<cql::cql_future_connection_t> connect_future = pool->add_client("localhost", 9042);
// Wait until the connection is complete, or has failed.
connect_future.wait();
// Check whether or not the connection was successful.
std::cout << "connect successfull? ";
if (!connect_future.get().error.is_err()) {
// The connection succeeded
std::cout << "TRUE" << std::endl;
// execute a query, switch keyspaces
boost::shared_future<cql::cql_future_result_t> future = pool->query("USE system;", cql::CQL_CONSISTENCY_ONE);
// wait for the query to execute
future.wait();
// check whether the query succeeded
std::cout << "switch keyspace successfull? " << (!future.get().error.is_err() ? "true" : "false") << std::endl;
// execute a query, select all rows from the keyspace
future = pool->query("SELECT * from schema_keyspaces;", cql::CQL_CONSISTENCY_ONE);
// wait for the query to execute
future.wait();
// check whether the query succeeded
std::cout << "select successfull? " << (!future.get().error.is_err() ? "true" : "false") << std::endl;
if (future.get().result) {
// print the rows return by the successful query
print_rows(*future.get().result);
}
// close the connection pool
pool->close();
}
else {
// The connection failed
std::cout << "FALSE" << std::endl;
}
work.reset();
thread.join();
std::cout << "THE END" << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}