Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Add implementation for the CLIENT KILL command #91

Merged
merged 6 commits into from
Aug 14, 2017

Conversation

YiuRULE
Copy link
Contributor

@YiuRULE YiuRULE commented Jul 12, 2017

The goal of this PR is to propose an implementation of the CLIENT KILL command

This solution handles all syntax written in the document, the first one using CLIENT KILL IP: port, and the other one using filters, we use type for deducing which one we need to call for handling compatibility with the server on < 2.8.12 version.

If we have only an address, a port, and an optional lamba, we choose the CLIENT KILL IP: port, if we have at least one filter we choose the second one.

Lot of prototypes are implemented, but two are important, the first one with only a variadic template for filters, and the other one with std::string host, int port, ... prototype.

Lot of stuffs are handled in the compile time, for example the solution launch a static_assert if the lambda is not the last argument when we call the function member for keeping a sense with the other function member of the API. Also, if we call the function member using only a lambda, etc...

Also the implementation is create in an another file, because we use template, we don't really want to polluate the header file.

If we send a bool, we check the SKIPME option

if we send an integral, it's the client ID.

if we send a client_kill::type who has several value like normal, master, pubsub and slave correspond to the type of the client.

Here an example of utilisation of client_kill


#include <cpp_redis/cpp_redis>

#include <iostream>
#include <sstream>
#include <string>

int main()
{

  cpp_redis::redis_client client;
  client.connect("localhost", 6379);

   // client kill ip:port
  client.client_list([&client](cpp_redis::reply& reply) {

      std::string addr;
      std::stringstream ss(reply.as_string());

      ss >> addr >> addr;

      std::string host = std::string(addr.begin() + addr.find('=') + 1, addr.begin() + addr.find(':'));
      int port = std::stoi(std::string(addr.begin() + addr.find(':') + 1, addr.end()));

      client.client_kill(host, port, [](cpp_redis::reply& reply) {
          std::cout << reply << std::endl; // OK
      });

      client.commit();
  });

  client.commit();
  std::this_thread::sleep_for(std::chrono::seconds(1));

  if (not client.is_connected()) // in case if the client didn't kill himself during the test
    client.connect("localhost", 6379);

  // client kill filter
  client.client_list([&client](cpp_redis::reply& reply) {

      std::string id_str;
      std::stringstream ss(reply.as_string());

      ss >> id_str;

      uint64_t id = std::stoi(std::string(id_str.begin() + id_str.find('=') + 1, id_str.end()));
      client.client_kill(id, false, cpp_redis::client_kill::type::normal, [](cpp_redis::reply& reply) {
          std::cout << reply << std::endl; // 1
      });

      client.commit();
  });

  client.commit();

  std::this_thread::sleep_for(std::chrono::seconds(1));
  return 0;
}

@Cylix
Copy link
Owner

Cylix commented Aug 13, 2017

Hey,

Sorry for the late answer and thanks for the great work!
I just checked on windows and it compiles perfectly fine, Travis is ok and the code is clean, so it's super cool :)
I'll just push a small change on your branch to adjust coding style (run clang-format, adjust the client_kill by removing the inheritance but instead using an ipp file).

I should merge this by tomorrow!

Thanks again :)

@Cylix
Copy link
Owner

Cylix commented Aug 14, 2017

Changes are OK, waiting for travis.

@Cylix
Copy link
Owner

Cylix commented Aug 14, 2017

Good to go, merging!

@Cylix Cylix merged commit a73f286 into Cylix:master Aug 14, 2017
@rbaxter1
Copy link

rbaxter1 commented Jun 19, 2018

This checkin-specifically variadic_template.hpp-breaks compatibility with Visual Studio 2013. I think c++14 is now required.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants