aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-07-12 19:57:52 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-07-12 19:57:52 +0200
commitdeb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d (patch)
tree8344812a3cc89b188a28799fa2c0f8e000ada586
parent1dcff1b442e9d99feb473e282d22b08b9197bb3b (diff)
downloadmqtt-cassandra-bridge-deb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d.tar.gz
mqtt-cassandra-bridge-deb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d.tar.bz2
mqtt-cassandra-bridge-deb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d.tar.xz
mqtt-cassandra-bridge-deb0c6cf01cb2b9994c77a6dd31341be8d1f1f4d.zip
o Adding command line options to configure cassandra and mqtt servers.
-rw-r--r--CMakeLists.txt5
-rw-r--r--main.cpp50
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 <iostream>
-#include <chrono>
#include <thread>
-#include <vector>
#include <boost/lexical_cast.hpp>
+#include <boost/program_options.hpp>
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<string>(&cassandra_cluster)->default_value("127.0.0.1"));
+ all.add_options()("mqtt-broker-host", po::value<string>(&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;