-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathriser-cli.cpp
executable file
·230 lines (204 loc) · 5.54 KB
/
riser-cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* riser.cpp
* Created by oceanwavewyt on 13-1-20.
* Copyright (c) 2012-2013, oceanwavewyt <oceanwavewyt at gmail dot com>
* All rights reserved.
*
* This soft wrap a serice for leveldb store, and support multi-threading
* client access it by the memcache extension of php, and telnet. Currently,
* only support put, get and del operation. additionally, it is support bloom
* filter algorithm for key.
*
*/
#include "prefine.h"
#include "gent_util.h"
#include <queue>
#include <linenoise.h>
#include <hiredis.h>
#include <sds.h>
#include <net.h>
int port = 3555;
char host[100] = "127.0.0.1";
class GentRedisClient
{
public:
GentRedisClient(string ip, int port, int timeout = 2000);
virtual ~GentRedisClient();
bool ExecuteCmd(const char *cmd, size_t len, string &response);
redisReply* ExecuteCmd(const char *cmd, size_t len);
private:
int m_timeout;
int m_serverPort;
string m_setverIp;
CommLock m_lock;
std::queue<redisContext *> m_clients;
time_t m_beginInvalidTime;
static const int m_maxReconnectInterval = 3;
redisContext* CreateContext();
void ReleaseContext(redisContext *ctx, bool active);
bool CheckStatus(redisContext *ctx);
};
GentRedisClient::GentRedisClient(string ip, int port, int timeout)
{
m_timeout = timeout;
m_serverPort = port;
m_setverIp = ip;
m_beginInvalidTime = 0;
}
GentRedisClient::~GentRedisClient()
{
AutoLock lock(&m_lock);
while(!m_clients.empty())
{
redisContext *ctx = m_clients.front();
redisFree(ctx);
m_clients.pop();
}
}
bool GentRedisClient::ExecuteCmd(const char *cmd, size_t len,string &response)
{
redisReply *reply = ExecuteCmd(cmd, len);
if(reply == NULL) return false;
if(reply->type == REDIS_REPLY_INTEGER)
{
char c[20]={0};
snprintf(c,20,"%lld",reply->integer);
response = c;
return true;
}
else if(reply->type == REDIS_REPLY_STRING)
{
response.assign(reply->str, reply->len);
return true;
}
else if(reply->type == REDIS_REPLY_STATUS)
{
response.assign(reply->str, reply->len);
return true;
}
else if(reply->type == REDIS_REPLY_NIL)
{
response = "(nil)";
return true;
}
else if(reply->type == REDIS_REPLY_ERROR)
{
response.assign(reply->str, reply->len);
return true;
}
else if(reply->type == REDIS_REPLY_ARRAY)
{
size_t i;
for(i=0; i<reply->elements; i++) {
redisReply *r =(redisReply *)reply->element[i];
if(r->type == REDIS_REPLY_STRING) {
cout << i<<") \""<< r->str << "\""<<endl;
}
}
return true;
}
else
{
response = "Undefine Reply Type";
return true;
}
}
redisReply* GentRedisClient::ExecuteCmd(const char *cmd, size_t len)
{
redisContext *ctx = CreateContext();
if(ctx == NULL) return NULL;
redisReply *reply = (redisReply*)redisCommand(ctx,cmd);
ReleaseContext(ctx, reply != NULL);
return reply;
}
redisContext* GentRedisClient::CreateContext()
{
{
AutoLock lock(&m_lock);
if(!m_clients.empty())
{
redisContext *ctx = m_clients.front();
m_clients.pop();
return ctx;
}
}
time_t now = time(NULL);
if(now < m_beginInvalidTime + m_maxReconnectInterval) return NULL;
struct timeval tv;
tv.tv_sec = m_timeout / 1000;
tv.tv_usec = (m_timeout % 1000) * 1000;;
redisContext *ctx = redisConnectWithTimeout(m_setverIp.c_str(), m_serverPort, tv);
if(ctx == NULL || ctx->err != 0)
{
if(ctx != NULL) redisFree(ctx);
m_beginInvalidTime = time(NULL);
cout << "connect failed" <<endl;
return NULL;
}
return ctx;
}
void GentRedisClient::ReleaseContext(redisContext *ctx, bool active)
{
if(ctx == NULL) return;
if(!active) {redisFree(ctx); return;}
AutoLock lock(&m_lock);
m_clients.push(ctx);
}
bool GentRedisClient::CheckStatus(redisContext *ctx)
{
redisReply *reply = (redisReply*)redisCommand(ctx, "ping");
if(reply == NULL) return false;
if(reply->type != REDIS_REPLY_STATUS) return false;
if(strcasecmp(reply->str,"PONG") != 0) return false;
return true;
}
void prompt(const char *h, int p)
{
}
int main(int argc, char **argv)
{
int ch;
while((ch = getopt(argc,argv,"h:vdsp:"))!= -1) {
switch (ch) {
case 'h':
memcpy(host, optarg, strlen(optarg));
break;
case 'p':
port = atoi(optarg);
break;
default:
break;
}
}
string h = host;
GentRedisClient client(h, port);
char prompt[200]={0};
char historypath[200]={0};
snprintf(prompt,200,"%s:%d> ",host,port);
char *home = getenv("HOME");
int history = 0;
if (home != NULL && *home != '\0') {
snprintf(historypath,200, "%s/.riserhistory",home);
linenoiseHistoryLoad(historypath);
history = 1;
}
char *line;
while((line = linenoise(prompt)) != NULL) {
if (line[0] != '\0') {
string data = line;
string res;
data = GentUtil::Trim(data);
if (history){
linenoiseHistoryAdd(line);
linenoiseHistorySave(historypath);
}
free(line);
if(client.ExecuteCmd(data.c_str(), data.size(),res)) {
cout << res <<endl;
}else{
break;
}
}
}
return 0;
}