Commit d89291b509e17885744539617a5dd60702029f94

Authored by Hayk
1 parent d43a11d1

Add failing binary data pubsub test

CMakeLists.txt
... ... @@ -142,6 +142,9 @@ if (examples)
142 142 add_executable(binary_data examples/binary_data.cpp)
143 143 target_link_libraries(binary_data redox)
144 144  
  145 + add_executable(binary_data_publish examples/binary_data_publish.cpp)
  146 + target_link_libraries(binary_data_publish redox)
  147 +
145 148 add_executable(pub_sub examples/pub_sub.cpp)
146 149 target_link_libraries(pub_sub redox)
147 150  
... ...
examples/binary_data_publish.cpp 0 → 100644
  1 +/**
  2 +* Basic use of Redox to publish and subscribe binary data.
  3 +*/
  4 +
  5 +#include <iostream>
  6 +#include <algorithm>
  7 +#include <random>
  8 +#include <chrono>
  9 +#include "redox.hpp"
  10 +
  11 +using namespace std;
  12 +using redox::Redox;
  13 +using redox::Command;
  14 +
  15 +/**
  16 +* Random string generator.
  17 +*/
  18 +std::string random_string(size_t length) {
  19 + std::string str(length, 0);
  20 + std::generate_n(str.begin(), length, []{ return (unsigned char)rand(); });
  21 + return str;
  22 +}
  23 +
  24 +int main(int argc, char* argv[]) {
  25 +
  26 + redox::Redox rdx; // Initialize Redox
  27 + redox::Subscriber sub; // Initialize Subscriber
  28 +
  29 + if(!rdx.connect("localhost", 6379)) return 1;
  30 + if(!sub.connect("localhost", 6379)) return 1;
  31 +
  32 + string binary_key = random_string(100);
  33 + string binary_data = random_string(10000);
  34 +
  35 + cout << "binary data size " << binary_data.length() << endl;
  36 +
  37 + sub.subscribe("test", [binary_data](const string& topic, const string& msg) {
  38 + cout << "msg data size " << msg.length() << endl;
  39 + if(msg == binary_data) cout << "Binary data matches!" << endl;
  40 + });
  41 +
  42 + this_thread::sleep_for( chrono::milliseconds(1000) );
  43 +
  44 + rdx.publish("test", binary_data);
  45 + cout << "published!" << endl;
  46 + this_thread::sleep_for( chrono::milliseconds(1000) );
  47 +
  48 + rdx.disconnect();
  49 + sub.disconnect();
  50 + return 0;
  51 +}
... ...