binary_data_publish.cpp
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Basic use of Redox to publish and subscribe binary data.
*/
#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>
#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;
}