Logo RedisPubSub Project   v0.9.0
RedisPubSub is an utility with Publisher/Subscirber model based on Redis for J-PARC/MLF.
RedisPubSub.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #ifndef REDISPUBSUB_H
4 #define REDISPUBSUB_H
5 
24 #include <iostream>
26 #include <vector>
27 #include <stdint.h>
28 #include <string>
29 #include <cstring> // std::memcpy
30 
32 #include "hiredis/hiredis.h"
33 
35 #include <memory>
36 using std::string;
37 using std::vector;
38 
40 
45 {
46  void operator()(redisContext* context) {
47  redisFree(context);
48  }
49 };
50 
52 
57 {
58  void operator()(redisReply* reply) {
59  freeReplyObject(reply);
60  }
61 };
62 
64 
68 {
69  string m_host;
70  int32_t m_port;
71  std::shared_ptr<redisContext> m_context;
72 
73 public:
77 
81  RedisPublisher(const string& host, int32_t port = 6379);
84 
86 
91  int32_t init();
92 
94 
103  int32_t publish(const string& name, const char* src_buf, int32_t size);
104 };
105 
107 
112 {
113 protected:
114  string m_host;
115  int32_t m_port;
116  std::shared_ptr<redisContext> m_context;
117 
118 public:
120  RedisSubscriber();
122 
126  RedisSubscriber(const string& host, int32_t port = 6379);
128  virtual ~RedisSubscriber();
129 
131 
135  void setHost(const string& host, int32_t port);
136 
137 };
138 
140 
145 {
146 
147 public:
150 
152 
156  RedisSyncSubscriber(const string& host, int32_t port = 6379);
157 
159  virtual ~RedisSyncSubscriber();
160 
162 
167  int32_t init();
168 
170 
177  int32_t subscribe(const string& name);
178 
180 
187  int32_t unsubscribe(const string& name);
188 
190 
202  int32_t get(string& name, char* target_buf, int32_t& size,
203  int32_t timeout = 2000);
204 
207 
215  int32_t getChannels(string name, vector<string>& vec);
216 
217 };
218 
219 #endif // REDISPUBSUB_H
int32_t publish(const string &name, const char *src_buf, int32_t size)
Publish data to the Redis server.
Definition: RedisPubSub.cpp:63
Redis context deleter using Redis Publisher/Subscriber Package.
Definition: RedisPubSub.h:44
int32_t getChannels(string name, vector< string > &vec)
Get name of channels from the Redis broker (sync).
Definition: RedisPubSub.cpp:225
Synchronous subscriber using Redis Publisher/Subscriber Package.
Definition: RedisPubSub.h:144
std::shared_ptr< redisContext > m_context
Port number.
Definition: RedisPubSub.h:71
int32_t subscribe(const string &name)
Subscribe data from the Redis broker (sync).
Definition: RedisPubSub.cpp:139
int32_t m_port
Host IP address.
Definition: RedisPubSub.h:115
int32_t unsubscribe(const string &name)
Unsubscribe data from the Redis broker (sync).
Definition: RedisPubSub.cpp:158
string m_host
Definition: RedisPubSub.h:114
void operator()(redisContext *context)
Definition: RedisPubSub.h:46
virtual ~RedisSyncSubscriber()
Destructor.
Definition: RedisPubSub.cpp:117
RedisSyncSubscriber()
Default constructor.
Definition: RedisPubSub.cpp:108
string m_host
Definition: RedisPubSub.h:69
void operator()(redisReply *reply)
Definition: RedisPubSub.h:58
std::shared_ptr< redisContext > m_context
Port number.
Definition: RedisPubSub.h:116
virtual ~RedisSubscriber()
Destructor.
Definition: RedisPubSub.cpp:104
Subscriber using Redis Publisher/Subscriber Package.
Definition: RedisPubSub.h:111
RedisPublisher()
Default constructor.
Definition: RedisPubSub.cpp:27
int32_t init()
Initialize subscriber as synchronous mode and connect to the broker.
Definition: RedisPubSub.cpp:121
int32_t init()
Initialize publisher and connect to broker.
Definition: RedisPubSub.cpp:43
void setHost(const string &host, int32_t port)
Set host address of the broker.
Definition: RedisPubSub.cpp:98
Publisher using Redis Publisher/Subscriber Package.
Definition: RedisPubSub.h:67
Redis reply object deleter using Redis Publisher/Subscriber Package.
Definition: RedisPubSub.h:56
RedisSubscriber()
Redis context.
Definition: RedisPubSub.cpp:89
int32_t m_port
Host IP address.
Definition: RedisPubSub.h:70
~RedisPublisher()
Destructor.
Definition: RedisPubSub.cpp:39