Minimal support for subscribe/psubscribe in redis-cli

This commit is contained in:
antirez 2010-04-27 16:07:31 +02:00
parent 095e805e89
commit 249c3a7dd1

View File

@ -54,6 +54,8 @@ static struct config {
long repeat; long repeat;
int dbnum; int dbnum;
int interactive; int interactive;
int monitor_mode;
int pubsub_mode;
char *auth; char *auth;
} config; } config;
@ -165,6 +167,8 @@ static struct redisCommand cmdTable[] = {
{"config",-2,REDIS_CMD_BULK}, {"config",-2,REDIS_CMD_BULK},
{"subscribe",-2,REDIS_CMD_INLINE}, {"subscribe",-2,REDIS_CMD_INLINE},
{"unsubscribe",-1,REDIS_CMD_INLINE}, {"unsubscribe",-1,REDIS_CMD_INLINE},
{"psubscribe",-2,REDIS_CMD_INLINE},
{"punsubscribe",-1,REDIS_CMD_INLINE},
{"publish",3,REDIS_CMD_BULK}, {"publish",3,REDIS_CMD_BULK},
{NULL,0,0} {NULL,0,0}
}; };
@ -349,7 +353,6 @@ static int selectDb(int fd) {
static int cliSendCommand(int argc, char **argv, int repeat) { static int cliSendCommand(int argc, char **argv, int repeat) {
struct redisCommand *rc = lookupCommand(argv[0]); struct redisCommand *rc = lookupCommand(argv[0]);
int fd, j, retval = 0; int fd, j, retval = 0;
int read_forever = 0;
sds cmd; sds cmd;
if (!rc) { if (!rc) {
@ -362,7 +365,9 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
return 1; return 1;
} }
if (!strcasecmp(rc->name,"monitor")) read_forever = 1; if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1;
if (!strcasecmp(rc->name,"subscribe") ||
!strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1;
if ((fd = cliConnect()) == -1) return 1; if ((fd = cliConnect()) == -1) return 1;
/* Select db number */ /* Select db number */
@ -402,10 +407,18 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
anetWrite(fd,cmd,sdslen(cmd)); anetWrite(fd,cmd,sdslen(cmd));
sdsfree(cmd); sdsfree(cmd);
while (read_forever) { while (config.monitor_mode) {
cliReadSingleLineReply(fd,0); cliReadSingleLineReply(fd,0);
} }
if (config.pubsub_mode) {
printf("Reading messages... (press Ctrl-c to quit)\n");
while (1) {
cliReadReply(fd);
printf("\n");
}
}
retval = cliReadReply(fd); retval = cliReadReply(fd);
if (retval) { if (retval) {
return retval; return retval;
@ -592,6 +605,8 @@ int main(int argc, char **argv) {
config.repeat = 1; config.repeat = 1;
config.dbnum = 0; config.dbnum = 0;
config.interactive = 0; config.interactive = 0;
config.monitor_mode = 0;
config.pubsub_mode = 0;
config.auth = NULL; config.auth = NULL;
firstarg = parseOptions(argc,argv); firstarg = parseOptions(argc,argv);