From d89291b509e17885744539617a5dd60702029f94 Mon Sep 17 00:00:00 2001 From: Hayk Date: Sun, 15 Nov 2015 00:00:46 -0800 Subject: [PATCH] Add failing binary data pubsub test --- CMakeLists.txt | 3 +++ examples/binary_data_publish.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 0 deletions(-) create mode 100644 examples/binary_data_publish.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ebdbbec..6b081b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,9 @@ if (examples) add_executable(binary_data examples/binary_data.cpp) target_link_libraries(binary_data redox) + add_executable(binary_data_publish examples/binary_data_publish.cpp) + target_link_libraries(binary_data_publish redox) + add_executable(pub_sub examples/pub_sub.cpp) target_link_libraries(pub_sub redox) diff --git a/examples/binary_data_publish.cpp b/examples/binary_data_publish.cpp new file mode 100644 index 0000000..52341e3 --- /dev/null +++ b/examples/binary_data_publish.cpp @@ -0,0 +1,51 @@ +/** +* Basic use of Redox to publish and subscribe binary data. +*/ + +#include +#include +#include +#include +#include "redox.hpp" + +using namespace std; +using redox::Redox; +using redox::Command; + +/** +* Random string generator. +*/ +std::string random_string(size_t length) { + std::string str(length, 0); + std::generate_n(str.begin(), length, []{ return (unsigned char)rand(); }); + return str; +} + +int main(int argc, char* argv[]) { + + redox::Redox rdx; // Initialize Redox + redox::Subscriber sub; // Initialize Subscriber + + if(!rdx.connect("localhost", 6379)) return 1; + if(!sub.connect("localhost", 6379)) return 1; + + string binary_key = random_string(100); + string binary_data = random_string(10000); + + cout << "binary data size " << binary_data.length() << endl; + + sub.subscribe("test", [binary_data](const string& topic, const string& msg) { + cout << "msg data size " << msg.length() << endl; + if(msg == binary_data) cout << "Binary data matches!" << endl; + }); + + this_thread::sleep_for( chrono::milliseconds(1000) ); + + rdx.publish("test", binary_data); + cout << "published!" << endl; + this_thread::sleep_for( chrono::milliseconds(1000) ); + + rdx.disconnect(); + sub.disconnect(); + return 0; +} -- libgit2 0.21.4