From deb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 12 Jul 2015 19:57:52 +0200 Subject: o Adding command line options to configure cassandra and mqtt servers. --- CMakeLists.txt | 5 ++++- main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 878abf9..9c379c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,14 @@ cmake_minimum_required(VERSION 3.2) project(mqtt_cassandra_bridge) -include(ExternalProject) +find_package(Boost COMPONENTS regex system program_options REQUIRED) add_executable(mqtt_cassandra_bridge main.cpp) +target_link_libraries(mqtt_cassandra_bridge PUBLIC ${Boost_LIBRARIES}) target_compile_options(mqtt_cassandra_bridge PUBLIC "-std=c++14") +include(ExternalProject) + # Cassandra set(CPP_DRIVER ${CMAKE_CURRENT_BINARY_DIR}/cpp-driver) ExternalProject_Add(cpp-driver diff --git a/main.cpp b/main.cpp index 806c047..0cd5622 100644 --- a/main.cpp +++ b/main.cpp @@ -1,20 +1,19 @@ #include "mosquittopp.h" #include "cassandra.h" #include "trygvis/sensor/io.h" -#include -#include #include -#include #include +#include using namespace std; using namespace std::chrono; using namespace trygvis::sensor; using namespace trygvis::sensor::io; using namespace boost; +namespace po = boost::program_options; static bool should_run; -static auto mqtt_broker_host = "test.mosquitto.org"; +static string mqtt_broker_host; static auto mqtt_broker_port = 1883; static auto queue_name = "/trygvis"; static string keyspace_name = "soil_moisture"; @@ -44,7 +43,7 @@ public: on_message_(on_message_) { cout << "Connecting to " << mqtt_broker_host << ":" << mqtt_broker_port << endl; loop_start(); - connect_async(mqtt_broker_host, mqtt_broker_port, 10); + connect_async(mqtt_broker_host.c_str(), mqtt_broker_port, 10); } ~mqtt_client() { @@ -57,7 +56,7 @@ private: bool subscribed = false; void on_connect(int rc) override { - cout << "Connected to MQTT broker" << endl; + cout << "Connected to MQTT broker, rc=" << rc << endl; // should_run = false; int qos = 0; if (!subscribed) { @@ -69,7 +68,8 @@ private: void on_disconnect(int rc) override { subscribed = false; - cout << "Oops, disconnected" << endl; + + cout << "Oops, disconnected, rc=" << rc << endl; } void on_publish(int mid) override { @@ -280,15 +280,45 @@ void on_message(const struct mosquitto_message *message) { }); } -int main() { +int main(int argc, const char **argv) { mqtt_lib mqtt_lib(); + string cassandra_cluster; + po::options_description all("Options"); + all.add_options()("cassandra-cluster", po::value(&cassandra_cluster)->default_value("127.0.0.1")); + all.add_options()("mqtt-broker-host", po::value(&mqtt_broker_host)->default_value("trygvis.io")); + + po::variables_map vm; + try { + auto parsed = po::parse_command_line(argc, argv, all); + po::store(parsed, vm); + po::notify(vm); + auto unrecognized = po::collect_unrecognized(parsed.options, po::include_positional); + + if (vm.count("help")) { + cerr << all << "\n"; + return EXIT_FAILURE; + } + + if (unrecognized.size()) { + cerr << "Unrecognized option: " << unrecognized.at(0) << "\n"; + return EXIT_FAILURE; + } + + } catch (po::required_option &e) { + cerr << "Missing required option: " << e.get_option_name() << endl; + cerr << all << endl; + } catch (po::unknown_option &e) { + cerr << e.what() << endl; + return EXIT_FAILURE; + } + mqtt_client mqtt_client(on_message); CassFuture *connect_future = nullptr; CassCluster *cluster = cass_cluster_new(); CassSession *session = cass_session_new(); - cass_cluster_set_contact_points(cluster, "127.0.0.1"); // , 127.0.0.2, 127.0.0.3 + cass_cluster_set_contact_points(cluster, cassandra_cluster.c_str()); connect_future = cass_session_connect(session, cluster); @@ -304,7 +334,7 @@ int main() { should_run = true; while (should_run) { cout << "sleeping.." << endl; - std::this_thread::sleep_for(10s); + std::this_thread::sleep_for(60s); } return 0; -- cgit v1.2.3