Commit 14a7f431c5bb134d1c4991836872a634957511ef
Merge pull request #35 from hmartiro/bugfix/binary_data_pubsub
Bugfix/binary data pubsub
Showing
4 changed files
with
62 additions
and
3 deletions
.gitignore
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 | + | |
| 46 | + this_thread::sleep_for( chrono::milliseconds(1000) ); | |
| 47 | + | |
| 48 | + rdx.disconnect(); | |
| 49 | + sub.disconnect(); | |
| 50 | + return 0; | |
| 51 | +} | ... | ... |
src/subscriber.cpp
| ... | ... | @@ -152,15 +152,17 @@ void Subscriber::subscribeBase(const string cmd_name, const string topic, |
| 152 | 152 | // Message for subscribe |
| 153 | 153 | else if ((reply->type == REDIS_REPLY_ARRAY) && (reply->elements == 3)) { |
| 154 | 154 | char *msg = reply->element[2]->str; |
| 155 | + int len = reply->element[2]->len; | |
| 155 | 156 | if (msg && msg_callback) |
| 156 | - msg_callback(topic, reply->element[2]->str); | |
| 157 | + msg_callback(topic, string(msg, len)); | |
| 157 | 158 | } |
| 158 | 159 | |
| 159 | 160 | // Message for psubscribe |
| 160 | 161 | else if ((reply->type == REDIS_REPLY_ARRAY) && (reply->elements == 4)) { |
| 161 | - char *msg = reply->element[2]->str; | |
| 162 | + char *msg = reply->element[3]->str; | |
| 163 | + int len = reply->element[3]->len; | |
| 162 | 164 | if (msg && msg_callback) |
| 163 | - msg_callback(reply->element[2]->str, reply->element[3]->str); | |
| 165 | + msg_callback(reply->element[2]->str, string(msg, len)); | |
| 164 | 166 | } |
| 165 | 167 | |
| 166 | 168 | else | ... | ... |