|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2017 Simon Ninon <[email protected]> |
| 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 | + |
| 23 | +#include <functional> |
| 24 | +#include <iostream> |
| 25 | + |
| 26 | +namespace cpp_redis { |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +typename std::enable_if<std::is_same<T, redis_client::client_type>::value>::type |
| 30 | +redis_client::client_kill_unpack_arg(std::vector<std::string>& redis_cmd, reply_callback_t&, client_type type) { |
| 31 | + redis_cmd.emplace_back("TYPE"); |
| 32 | + std::string type_string; |
| 33 | + |
| 34 | + switch (type) { |
| 35 | + case client_type::normal: type_string = "normal"; break; |
| 36 | + case client_type::master: type_string = "master"; break; |
| 37 | + case client_type::pubsub: type_string = "pubsub"; break; |
| 38 | + case client_type::slave: type_string = "slave"; break; |
| 39 | + } |
| 40 | + |
| 41 | + redis_cmd.emplace_back(type_string); |
| 42 | +} |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +typename std::enable_if<std::is_same<T, bool>::value>::type |
| 46 | +redis_client::client_kill_unpack_arg(std::vector<std::string>& redis_cmd, reply_callback_t&, bool skip) { |
| 47 | + redis_cmd.emplace_back("SKIPME"); |
| 48 | + redis_cmd.emplace_back(skip ? "yes" : "no"); |
| 49 | +} |
| 50 | + |
| 51 | +template <typename T> |
| 52 | +typename std::enable_if<std::is_integral<T>::value>::type |
| 53 | +redis_client::client_kill_unpack_arg(std::vector<std::string>& redis_cmd, reply_callback_t&, uint64_t id) { |
| 54 | + redis_cmd.emplace_back("ID"); |
| 55 | + redis_cmd.emplace_back(std::to_string(id)); |
| 56 | +} |
| 57 | + |
| 58 | +template <typename T> |
| 59 | +typename std::enable_if<std::is_class<T>::value>::type |
| 60 | +redis_client::client_kill_unpack_arg(std::vector<std::string>&, reply_callback_t& reply_callback, const T& cb) { |
| 61 | + reply_callback = cb; |
| 62 | +} |
| 63 | + |
| 64 | +template <typename T, typename... Ts> |
| 65 | +void |
| 66 | +redis_client::client_kill_impl(std::vector<std::string>& redis_cmd, reply_callback_t& reply, const T& arg, const Ts&... args) { |
| 67 | + static_assert(!std::is_class<T>::value, "Reply callback should be in the end of the argument list"); |
| 68 | + client_kill_unpack_arg<T>(redis_cmd, reply, arg); |
| 69 | + client_kill_impl(redis_cmd, reply, args...); |
| 70 | +}; |
| 71 | + |
| 72 | +template <typename T> |
| 73 | +void |
| 74 | +redis_client::client_kill_impl(std::vector<std::string>& redis_cmd, reply_callback_t& reply, const T& arg) { |
| 75 | + client_kill_unpack_arg<T>(redis_cmd, reply, arg); |
| 76 | +}; |
| 77 | + |
| 78 | +template <typename T, typename... Ts> |
| 79 | +inline redis_client& |
| 80 | +redis_client::client_kill(const T& arg, const Ts&... args) { |
| 81 | + static_assert(helpers::is_different_types<T, Ts...>::value, "Should only have one distinct value per filter type"); |
| 82 | + static_assert(!(std::is_class<T>::value && std::is_same<T, typename helpers::back<T, Ts...>::type>::value), "Should have at least one filter"); |
| 83 | + |
| 84 | + std::vector<std::string> redis_cmd({"CLIENT", "KILL"}); |
| 85 | + reply_callback_t reply_cb = nullptr; |
| 86 | + client_kill_impl<T, Ts...>(redis_cmd, reply_cb, arg, args...); |
| 87 | + |
| 88 | + return send(redis_cmd, reply_cb); |
| 89 | +} |
| 90 | + |
| 91 | +template <typename T, typename... Ts> |
| 92 | +inline redis_client& |
| 93 | +redis_client::client_kill(const std::string& host, int port, const T& arg, const Ts&... args) { |
| 94 | + static_assert(helpers::is_different_types<T, Ts...>::value, "Should only have one distinct value per filter type"); |
| 95 | + std::vector<std::string> redis_cmd({"CLIENT", "KILL"}); |
| 96 | + |
| 97 | + //! If we have other type than lambda, then it's a filter |
| 98 | + if (!std::is_class<T>::value) { |
| 99 | + redis_cmd.emplace_back("ADDR"); |
| 100 | + } |
| 101 | + |
| 102 | + redis_cmd.emplace_back(host + ":" + std::to_string(port)); |
| 103 | + reply_callback_t reply_cb = nullptr; |
| 104 | + client_kill_impl<T, Ts...>(redis_cmd, reply_cb, arg, args...); |
| 105 | + |
| 106 | + return send(redis_cmd, reply_cb); |
| 107 | +} |
| 108 | + |
| 109 | +inline redis_client& |
| 110 | +redis_client::client_kill(const std::string& host, int port) { |
| 111 | + return client_kill(host, port, reply_callback_t(nullptr)); |
| 112 | +} |
| 113 | + |
| 114 | +template <typename... Ts> |
| 115 | +inline redis_client& |
| 116 | +redis_client::client_kill(const char* host, int port, const Ts&... args) { |
| 117 | + return client_kill(std::string(host), port, args...); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace cpp_redis |
0 commit comments