|
1 | | -/* |
2 | | - * |
3 | | - * Created by nick on 11/22/18. |
4 | | - * |
5 | | - * Copyright(c) 2018 Iris. All rights reserved. |
6 | | - * |
7 | | - * Use and copying of this software and preparation of derivative |
8 | | - * works based upon this software are not permitted. Any distribution |
9 | | - * of this software or derivative works must comply with all applicable |
10 | | - * Canadian export control laws. |
11 | | - * |
12 | | - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
13 | | - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
14 | | - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
15 | | - * DISCLAIMED. IN NO EVENT SHALL IRIS OR ITS CONTRIBUTORS |
16 | | - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
17 | | - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
18 | | - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
19 | | - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
20 | | - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
21 | | - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
22 | | - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
23 | | - * SUCH DAMAGE. |
24 | | - * |
25 | | - */ |
26 | | - |
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com> |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | +#include <string> |
27 | 23 | #include <cpp_redis/cpp_redis> |
28 | | -#include <tacopie/tacopie> |
29 | | - |
30 | | -#include <condition_variable> |
31 | | -#include <iostream> |
32 | | -#include <mutex> |
33 | | -#include <signal.h> |
34 | 24 |
|
35 | 25 | #ifdef _WIN32 |
36 | 26 | #include <Winsock2.h> |
37 | 27 | #endif /* _WIN32 */ |
38 | 28 |
|
39 | | -std::condition_variable should_exit; |
40 | | - |
41 | | -void |
42 | | -sigint_handler(int) { |
43 | | - should_exit.notify_all(); |
44 | | -} |
45 | | - |
46 | 29 | int |
47 | 30 | main(void) { |
48 | 31 | #ifdef _WIN32 |
49 | 32 | //! Windows netword DLL init |
50 | | - WORD version = MAKEWORD(2, 2); |
51 | | - WSADATA data; |
| 33 | + WORD version = MAKEWORD(2, 2); |
| 34 | + WSADATA data; |
52 | 35 |
|
53 | | - if (WSAStartup(version, &data) != 0) { |
54 | | - std::cerr << "WSAStartup() failure" << std::endl; |
55 | | - return -1; |
56 | | - } |
| 36 | + if (WSAStartup(version, &data) != 0) { |
| 37 | + std::cerr << "WSAStartup() failure" << std::endl; |
| 38 | + return -1; |
| 39 | + } |
57 | 40 | #endif /* _WIN32 */ |
58 | 41 |
|
59 | 42 | //! Enable logging |
60 | 43 | cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger); |
61 | 44 |
|
62 | | - cpp_redis::consumer_options_t opts; |
63 | | - |
64 | | - opts.group_name = "groupone"; |
65 | | - opts.session_name = "sessone"; |
66 | | - |
67 | | - cpp_redis::consumer sub(opts); |
68 | | - |
69 | | - |
70 | | - sub.connect("127.0.0.1", 6379, [](const std::string& host, std::size_t port, cpp_redis::consumer::connect_state status) { |
71 | | - if (status == cpp_redis::consumer::connect_state::dropped) { |
72 | | - std::cout << "client disconnected from " << host << ":" << port << std::endl; |
73 | | - should_exit.notify_all(); |
74 | | - } |
| 45 | + cpp_redis::client client; |
| 46 | + |
| 47 | + client.connect("127.0.0.1", 6379, |
| 48 | + [](const std::string &host, std::size_t port, cpp_redis::client::connect_state status) { |
| 49 | + if (status == cpp_redis::client::connect_state::dropped) { |
| 50 | + std::cout << "client disconnected from " << host << ":" << port << std::endl; |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + auto replcmd = [](cpp_redis::reply &reply) { |
| 55 | + std::cout << "set hello 42: " << reply << std::endl; |
| 56 | + // if (reply.is_string()) |
| 57 | + // do_something_with_string(reply.as_string()) |
| 58 | + }; |
| 59 | + |
| 60 | + const std::string group_name = "groupone"; |
| 61 | + const std::string session_name = "sessone"; |
| 62 | + const std::string consumer_name = "ABCD"; |
| 63 | + |
| 64 | + std::multimap<std::string, std::string> ins; |
| 65 | + ins.insert(std::pair<std::string, std::string>{"message", "hello"}); |
| 66 | + |
| 67 | + client.xadd(session_name, "*", ins, replcmd); |
| 68 | + client.xgroup_create(session_name, group_name, replcmd); |
| 69 | + |
| 70 | + client.sync_commit(); |
| 71 | + |
| 72 | + client.xrange(session_name, {"-", "+", 10}, replcmd); |
| 73 | + |
| 74 | + client.xreadgroup({group_name, |
| 75 | + consumer_name, |
| 76 | + {{session_name}, {">"}}, |
| 77 | + 1, // Count |
| 78 | + 0, // block milli |
| 79 | + false, // no ack |
| 80 | + }, [](cpp_redis::reply &reply) { |
| 81 | + std::cout << "set hello 42: " << reply << std::endl; |
| 82 | + cpp_redis::xstream_reply msg(reply); |
| 83 | + std::cout << "Mes: " << msg[0].Messages[0].Values["message"] << std::endl; |
| 84 | + // if (reply.is_string()) |
| 85 | + // do_something_with_string(reply.as_string()) |
75 | 86 | }); |
76 | 87 |
|
77 | | - //! authentication if server-server requires it |
78 | | - // sub.auth("some_password", [](const cpp_redis::reply& reply) { |
79 | | - // if (reply.is_error()) { std::cerr << "Authentication failed: " << reply.as_string() << std::endl; } |
80 | | - // else { |
81 | | - // std::cout << "successful authentication" << std::endl; |
82 | | - // } |
83 | | - // }); |
| 88 | + // commands are pipelined and only sent when client.commit() is called |
| 89 | + // client.commit(); |
84 | 90 |
|
85 | | - sub.subscribe("sessone", "groupone", [](const std::string& chan, const std::map<std::string, std::string> & msg) { |
86 | | - std::cout << "MESSAGE " << chan << ": " << msg.at("id") << std::endl; |
87 | | - }); |
88 | | - sub.commit(); |
| 91 | + // synchronous commit, no timeout |
| 92 | + client.sync_commit(); |
89 | 93 |
|
90 | | - signal(SIGINT, &sigint_handler); |
91 | | - std::mutex mtx; |
92 | | - std::unique_lock<std::mutex> l(mtx); |
93 | | - should_exit.wait(l); |
| 94 | + // synchronous commit, timeout |
| 95 | + // client.sync_commit(std::chrono::milliseconds(100)); |
94 | 96 |
|
95 | 97 | #ifdef _WIN32 |
96 | 98 | WSACleanup(); |
|
0 commit comments