/* Copyright (c) 2013, 2014 IOnU Security Inc. All rights reserved. Copyright (c) 2015, 2016 Sequence Logic, Inc. Created August 2013 by Kendrick Webster K2Daemon/MongoDB.h - MongoDB database interface common functions */ #pragma once #include "global.h" #include #include #include #include /* The Mongo database connection is shared (global) for this application. Modules that operate on specific databases and collections use this one global shared connection. This namespace also has common/shared utility functions. */ namespace MongoDB { constexpr const char* DEFAULT_HOST = "127.0.0.1"; constexpr uint16_t DEFAULT_PORT = 27017; // Call Connect or ConnectReplica before using any other MongoDB functions // Connect to a single MongoDB server success_t Connect(const char* host = DEFAULT_HOST, int port = DEFAULT_PORT); // Connect to a MongoDB replica set //success_t ConnectReplica(const std::string& replica_set_name, const host_list_t& hosts); success_t ConnectReplica(const std::string& replica_set_name, const std::string& hosts); // Disconnect from MongoDB server or replica set void Disconnect(void); // Reconnect, this needs to be done if connection errors occur, see // http://api.mongodb.org/c/current/connections.html // http://devhost.ionu.int:8080/jira/browse/IONU-1163 // Note that this may fail when the primary of a replica set is lost and Mongo hasn't yet // elected a new primary. If it does fail, calling other mongo API functions can lock up // the thread (as of ver 0.8.1 of the Mongo C driver). success_t Reconnect(void); // Returns connection datastruct, NULL if not connected (or if MongoDB is temporarilly down) mongoc_client_t* GetConnection(void); // Returns true if MongoDB is temporarilly down (i.e. when the primary of a replica set is // lost and Mongo hasn't yet elected a new primary) bool IsTemporaryOutage(void); // returns human-readable text (suitable for logging) for a MongoDB error code const char* ErrorString(mongoc_error_code_t e); // Call periodically to do background tasks (i.e. reconnect to MongoDB after a transient connection issue causes a disconnect) void Timer(void); }