Skip to content

Commit

Permalink
Added utility function to ping a cassandra server to see if it is up.
Browse files Browse the repository at this point in the history
  • Loading branch information
posulliv committed Feb 22, 2011
1 parent e6aaa64 commit b4731fe
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
# | | |
# +------+ | +---+
# | | |
Expand Down
15 changes: 15 additions & 0 deletions libcassandra/cassandra_util.h
Original file line number Diff line number Diff line change
@@ -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 */
7 changes: 5 additions & 2 deletions libcassandra/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
53 changes: 53 additions & 0 deletions libcassandra/util/ping.cc
Original file line number Diff line number Diff line change
@@ -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 */

26 changes: 26 additions & 0 deletions libcassandra/util/ping.h
Original file line number Diff line number Diff line change
@@ -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 */
4 changes: 2 additions & 2 deletions tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit b4731fe

Please sign in to comment.