Permalink
Browse files

Added utility function to ping a cassandra server to see if it is up.

  • Loading branch information...
1 parent e6aaa64 commit b4731fede25559d7bcc3d6dacc917ec115a5c056 @posulliv committed Feb 22, 2011
Showing with 106 additions and 5 deletions.
  1. +4 −0 .gitignore
  2. +1 −1 configure.ac
  3. +15 −0 libcassandra/cassandra_util.h
  4. +5 −2 libcassandra/include.am
  5. +53 −0 libcassandra/util/ping.cc
  6. +26 −0 libcassandra/util/ping.h
  7. +2 −2 tests/main.cc
View
4 .gitignore
@@ -26,6 +26,10 @@ libcassandra/.deps/
libcassandra/.dirstamp
libcassandra/configure.h
libcassandra/.libs/
+libcassandra/util/.deps/
+libcassandra/util/.libs/
+libcassandra/util/.dirstamp
+libcassandra/util/.deps/.dirstamp
libgenthrift/.libs/
libgenthrift/.dirstamp
libgenthrift/configure.h
View
2 configure.ac
@@ -14,7 +14,7 @@ AC_CONFIG_AUX_DIR(config)
PANDORA_CANONICAL_TARGET(less-warnings, warnings-always-on, require-cxx, skip-visibility)
#shared library versioning
-CASSANDRA_LIBRARY_VERSION=1:0:0
+CASSANDRA_LIBRARY_VERSION=2:0:0
# | | |
# +------+ | +---+
# | | |
View
15 libcassandra/cassandra_util.h
@@ -0,0 +1,15 @@
+/*
+ * LibCassandra
+ * Copyright (C) 2010-2011 Padraig O'Sullivan
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license. See
+ * the COPYING file in the parent directory for full text.
+ */
+
+#ifndef __LIBCASSANDRA_UTIL_H
+#define __LIBCASSANDRA_UTIL_H
+
+#include "libcassandra/util/ping.h"
+
+#endif /* __LIBCASSANDRA_UTIL_H */
View
7 libcassandra/include.am
@@ -9,13 +9,15 @@ nobase_include_HEADERS+= \
libcassandra/cassandra.h \
libcassandra/cassandra_factory.h \
libcassandra/cassandra_host.h \
+ libcassandra/cassandra_util.h \
libcassandra/column_definition.h \
libcassandra/column_family_definition.h \
libcassandra/exception.h \
libcassandra/keyspace.h \
libcassandra/keyspace_definition.h \
libcassandra/keyspace_factory.h \
- libcassandra/util_functions.h
+ libcassandra/util_functions.h \
+ libcassandra/util/ping.h
lib_LTLIBRARIES+= libcassandra/libcassandra.la
libcassandra_libcassandra_la_CXXFLAGS= ${AM_CXXFLAGS}
@@ -28,7 +30,8 @@ libcassandra_libcassandra_la_SOURCES = \
libcassandra/keyspace.cc \
libcassandra/keyspace_definition.cc \
libcassandra/keyspace_factory.cc \
- libcassandra/util_functions.cc
+ libcassandra/util_functions.cc \
+ libcassandra/util/ping.cc
libcassandra_libcassandra_la_DEPENDENCIES= libgenthrift/libgenthrift.la
libcassandra_libcassandra_la_LIBADD= $(LIBM) libgenthrift/libgenthrift.la
View
53 libcassandra/util/ping.cc
@@ -0,0 +1,53 @@
+/*
+ * LibCassandra
+ * Copyright (C) 2010-2011 Padraig O'Sullivan
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license. See
+ * the COPYING file in the parent directory for full text.
+ */
+
+#include <string>
+#include <sstream>
+#include <iostream>
+
+#include <protocol/TBinaryProtocol.h>
+#include <transport/TSocket.h>
+#include <transport/TTransportUtils.h>
+
+#include "libgenthrift/Cassandra.h"
+
+#include "libcassandra/util/ping.h"
+
+using namespace std;
+using namespace apache::thrift;
+using namespace apache::thrift::protocol;
+using namespace apache::thrift::transport;
+using namespace org::apache::cassandra;
+using namespace boost;
+
+namespace libcassandra
+{
+
+
+bool util::pingCassandraServer(const string& hostname, int port)
+{
+ try
+ {
+ boost::shared_ptr<TTransport> socket(new TSocket(hostname, port));
+ boost::shared_ptr<TTransport> transport= boost::shared_ptr<TTransport>(new TFramedTransport(socket));
+ boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
+ CassandraClient client(protocol);
+ transport->open(); /* throws an exception */
+ }
+ catch (std::exception&)
+ {
+ return false;
+ }
+
+ return true;
+}
+
+
+} /* end namespace libcassandra */
+
View
26 libcassandra/util/ping.h
@@ -0,0 +1,26 @@
+/*
+ * LibCassandra
+ * Copyright (C) 2010-2011 Padraig O'Sullivan
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license. See
+ * the COPYING file in the parent directory for full text.
+ */
+
+#ifndef __LIBCASSANDRA_UTIL_PING_H
+#define __LIBCASSANDRA_UTIL_PING_H
+
+namespace libcassandra
+{
+
+namespace util
+{
+
+bool pingCassandraServer(const std::string& hostname,
+ int port);
+
+} /* end namespace util */
+
+} /* end namespace libcassandra */
+
+#endif /* __LIBCASSANDRA_UTIL_PING_H */
View
4 tests/main.cc
@@ -32,12 +32,12 @@ int main(int argc, char **argv)
tcp::resolver::query query(tcp::v4(), "localhost", "9160");
tcp::resolver::iterator iterator= resolver.resolve(query);
tcp::socket socket(io_service);
- socket.connect(*iterator);
+ socket.connect(*iterator);
}
catch (exception &)
{
cerr << "Cassandra service is not running on localhost port 9160" << endl;
- return EXIT_FAILURE;
+ return EXIT_FAILURE;
}
/* we have a running server, lets go with the unit tests */

0 comments on commit b4731fe

Please sign in to comment.