acl/lib_acl_cpp/include/acl_cpp/redis/redis_pubsub.hpp

209 lines
9.2 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2015-01-23 17:23:04 +08:00
#include <vector>
#include <map>
2017-06-02 14:47:24 +08:00
#include "redis_command.hpp"
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
namespace acl
{
class redis_client;
class redis_result;
class string;
class ACL_CPP_API redis_pubsub : virtual public redis_command
{
public:
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command()
*/
redis_pubsub(void);
2015-03-29 19:27:41 +08:00
/**
* see redis_command::redis_command(redis_client*)
*/
redis_pubsub(redis_client* conn);
2015-03-29 19:27:41 +08:00
/**
2016-05-22 09:10:34 +08:00
* see redis_command::redis_command(redis_client_cluster*, size_t)
2015-03-29 19:27:41 +08:00
*/
redis_pubsub(redis_client_cluster* cluster, size_t max_conns = 0);
2015-03-29 19:27:41 +08:00
virtual ~redis_pubsub(void);
/////////////////////////////////////////////////////////////////////
/**
* channel
2015-04-12 21:37:17 +08:00
* post a message to a channel
* @param channel {const char*}
2015-04-12 21:37:17 +08:00
* the specified channel
* @param msg {const char*}
2015-04-12 21:37:17 +08:00
* the message to be sent
* @param len {size_t}
2015-04-12 21:37:17 +08:00
* the message's length
* @return {int}
2015-04-12 21:37:17 +08:00
* the number of clients that received the message
* -1
2015-04-12 21:37:17 +08:00
* error happened
* 0
2015-04-12 21:37:17 +08:00
* no client subscribe the channel
* >0
2015-04-12 21:37:17 +08:00
* the number of clients that received the message
*/
int publish(const char* channel, const char* msg, size_t len);
2015-01-23 17:23:04 +08:00
2015-01-29 00:38:29 +08:00
/**
*
* subscribeunsubscribepsubscribepunsubscribeget_message
*
2015-04-12 21:37:17 +08:00
* subscribe one or more channel(s). Once the client enters the
* subscribed state it is not supposed to issue any other commands,
* except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE
* and PUNSUBSCRIBE commands
* @param first_channel {const char*}
* NULL
2015-04-12 21:37:17 +08:00
* the first non-NULL channel in the channel list, and the last
* parameter must be NULL indicating the end of the channel list
* @return {int}
2015-04-12 21:37:17 +08:00
* the number of channels subscribed by the current client
2015-01-29 00:38:29 +08:00
*/
int subscribe(const char* first_channel, ...);
2015-01-23 17:23:04 +08:00
int subscribe(const std::vector<const char*>& channels);
int subscribe(const std::vector<string>& channels);
2015-01-23 17:23:04 +08:00
2015-01-29 00:38:29 +08:00
/**
*
2015-04-12 21:37:17 +08:00
* stop listening for messages posted to the given channels
* @param first_channel {const char*}
2015-04-12 21:37:17 +08:00
* the fist channel in channel list, and the last parameter must be
* NULL indicating the end of the channel list
* @return {int}
2015-04-12 21:37:17 +08:00
* the rest channels listened by the current client
2015-01-29 00:38:29 +08:00
*/
int unsubscribe(const char* first_channel, ...);
2015-01-23 17:23:04 +08:00
int unsubscribe(const std::vector<const char*>& channels);
int unsubscribe(const std::vector<string>& channels);
2015-01-23 17:23:04 +08:00
2015-01-29 00:38:29 +08:00
/**
* *
* subscribeunsubscribepsubscribepunsubscribe
* get_message
2015-04-12 21:37:17 +08:00
* listen for messages published to channels matching the give patterns
* @param first_pattern {const char*}
2015-04-12 21:37:17 +08:00
* the first pattern in pattern list, the last parameter must be NULL
* int the variable args
* @return {int}
2015-04-12 21:37:17 +08:00
* the number of channels listened by the current client
2015-01-29 00:38:29 +08:00
*/
2015-01-23 17:23:04 +08:00
int psubscribe(const char* first_pattern, ...);
int psubscribe(const std::vector<const char*>& patterns);
int psubscribe(const std::vector<string>& patterns);
2015-01-29 00:38:29 +08:00
/**
*
2015-04-12 21:37:17 +08:00
* stop listening for messaged posted to channels matching
* the given patterns
* @param first_pattern {const char*}
2015-04-12 21:37:17 +08:00
* the first parttern in a variable args ending with NULL
* @return {int}
2015-04-12 21:37:17 +08:00
* the rest number of channels be listened by the client
2015-01-29 00:38:29 +08:00
*/
2015-01-23 17:23:04 +08:00
int punsubscribe(const char* first_pattern, ...);
int punsubscribe(const std::vector<const char*>& patterns);
int punsubscribe(const std::vector<string>& patterns);
2015-01-29 00:38:29 +08:00
/**
*
* subscribe psubscribe
2015-04-12 21:37:17 +08:00
* get messages posted to channels after SUBSCRIBE or PSUBSCRIBE
* @param channel {string&}
2015-04-12 21:37:17 +08:00
* buffer for storing the channel associate with the msg
* @param msg {string&}
2015-04-12 21:37:17 +08:00
* store the message posted to the channel
* @param message_type {string*} will store messsage or pmessage
* @param pattern {string*} will store pattern set by psubscribe
* @return {bool} false
2015-04-12 21:37:17 +08:00
* true on success, false on error
2015-01-29 00:38:29 +08:00
*/
bool get_message(string& channel, string& msg,
string* message_type = NULL, string* pattern = NULL);
2015-01-29 00:38:29 +08:00
/**
*
*
2015-04-12 21:37:17 +08:00
* Lists the currently active channels.
* @param channels {std::vector<string>*}
2015-04-12 21:37:17 +08:00
* store the active channels
* @param first_pattern {const char*}
* NULL NULL
2015-04-12 21:37:17 +08:00
* the first pattern in a variable args ending with NULL arg, and
* the first arg can be NULL.
* @return {int} -1
2015-04-12 21:37:17 +08:00
* the number of active channels. -1 if error
2015-01-29 00:38:29 +08:00
*
*
* 1 get_value
* 2 get_child (redis_result
* redis_result::argv_to_string
* 3 get_result redis_result
* redis_result::get_child 2
*
* 4 get_children redis_result
* argv_to_string
* 5
2015-01-30 22:58:13 +08:00
*
2015-01-29 00:38:29 +08:00
*/
2015-01-30 22:58:13 +08:00
int pubsub_channels(std::vector<string>* channels,
2015-01-23 17:23:04 +08:00
const char* first_pattern, ...);
int pubsub_channels(const std::vector<const char*>& patterns,
2015-01-30 22:58:13 +08:00
std::vector<string>* channels);
2015-01-23 17:23:04 +08:00
int pubsub_channels(const std::vector<string>& patterns,
2015-01-30 22:58:13 +08:00
std::vector<string>* channels);
2015-01-23 17:23:04 +08:00
2015-01-29 00:38:29 +08:00
/**
*
2015-04-12 21:37:17 +08:00
* Returns the number of subscribers (not counting clients
* subscribed to patterns) for the specified channels.
* @param out {std::map<string, int>&} out->first
* out->second
2015-04-12 21:37:17 +08:00
* store the results
* @param first_pattern {const char*}
* NULL NULL
2015-04-12 21:37:17 +08:00
* the first pattern in a variable args ending with NULL arg, and
* the first arg can be NULL.
* @return {int} -1
2015-01-29 00:38:29 +08:00
*/
2015-01-23 17:23:04 +08:00
int pubsub_numsub(std::map<string, int>& out,
const char* first_channel, ...);
int pubsub_numsub(const std::vector<const char*>& channels,
std::map<string, int>& out);
int pubsub_numsub(const std::vector<string>& channels,
std::map<string, int>& out);
2015-01-29 00:38:29 +08:00
/**
*
*
2015-04-12 21:37:17 +08:00
* Returns the number of subscriptions to patterns.
* @return {int} -1
2015-04-12 21:37:17 +08:00
* the number of patterns all the clients are subscribed to,
* -1 if error.
2015-01-29 00:38:29 +08:00
*/
2015-01-23 17:23:04 +08:00
int pubsub_numpat();
private:
2015-01-23 17:23:04 +08:00
int subop(const char* cmd, const std::vector<const char*>& channels);
int subop_result(const char* cmd, const std::vector<const char*>& channels);
int subop(const char* cmd, const std::vector<string>& channels);
int subop_result(const char* cmd, const std::vector<string>& channels);
int check_channel(const redis_result* obj, const char* cmd,
const char* channel);
2015-01-26 19:58:02 +08:00
int pubsub_numsub(std::map<string, int>& out);
};
} // namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)