+/* =========================== Pubsub implementation ======================== */
+
+/* Subscribe a client to a class. Returns 1 if the operation succeeded, or
+ * 0 if the client was already subscribed to that class. */
+static int pubsubSubscribe(redisClient *c, robj *class) {
+ struct dictEntry *de;
+ list *clients = NULL;
+ int retval = 0;
+
+ /* Add the class to the client -> classes hash table */
+ if (dictAdd(c->pubsub_classes,class,NULL) == DICT_OK) {
+ retval = 1;
+ incrRefCount(class);
+ /* Add the client to the class -> list of clients hash table */
+ de = dictFind(server.pubsub_classes,class);
+ if (de == NULL) {
+ clients = listCreate();
+ dictAdd(server.pubsub_classes,class,clients);
+ incrRefCount(class);
+ } else {
+ clients = dictGetEntryVal(de);
+ }
+ listAddNodeTail(clients,c);
+ }
+ /* Notify the client */
+ addReply(c,shared.mbulk3);
+ addReply(c,shared.subscribebulk);
+ addReplyBulk(c,class);
+ addReplyLong(c,dictSize(c->pubsub_classes));
+ return retval;
+}
+
+/* Unsubscribe a client from a class. Returns 1 if the operation succeeded, or
+ * 0 if the client was not subscribed to the specified class. */
+static int pubsubUnsubscribe(redisClient *c, robj *class, int notify) {
+ struct dictEntry *de;
+ list *clients;
+ listNode *ln;
+ int retval = 0;
+
+ /* Remove the class from the client -> classes hash table */
+ if (dictDelete(c->pubsub_classes,class) == DICT_OK) {
+ retval = 1;
+ /* Remove the client from the class -> clients list hash table */
+ de = dictFind(server.pubsub_classes,class);
+ assert(de != NULL);
+ clients = dictGetEntryVal(de);
+ ln = listSearchKey(clients,c);
+ assert(ln != NULL);
+ listDelNode(clients,ln);
+ }
+ /* Notify the client */
+ if (notify) {
+ addReply(c,shared.mbulk3);
+ addReply(c,shared.unsubscribebulk);
+ addReplyBulk(c,class);
+ addReplyLong(c,dictSize(c->pubsub_classes));
+ }
+ return retval;
+}
+
+/* Unsubscribe from all the classes. Return the number of classes the
+ * client was subscribed to. */
+static int pubsubUnsubscribeAll(redisClient *c, int notify) {
+ dictIterator *di = dictGetIterator(c->pubsub_classes);
+ dictEntry *de;
+ int count = 0;
+
+ while((de = dictNext(di)) != NULL) {
+ robj *class = dictGetEntryKey(de);
+
+ count += pubsubUnsubscribe(c,class,notify);
+ }
+ dictReleaseIterator(di);
+ return count;
+}
+
+/* Publish a message */
+static int pubsubPublishMessage(robj *class, robj *message) {
+ int receivers = 0;
+ struct dictEntry *de;
+
+ de = dictFind(server.pubsub_classes,class);
+ if (de) {
+ list *list = dictGetEntryVal(de);
+ listNode *ln;
+ listIter li;
+
+ listRewind(list,&li);
+ while ((ln = listNext(&li)) != NULL) {
+ redisClient *c = ln->value;
+
+ addReply(c,shared.mbulk3);
+ addReply(c,shared.messagebulk);
+ addReplyBulk(c,class);
+ addReplyBulk(c,message);
+ receivers++;
+ }
+ }
+ return receivers;
+}
+
+static void subscribeCommand(redisClient *c) {
+ int j;
+
+ for (j = 1; j < c->argc; j++)
+ pubsubSubscribe(c,c->argv[j]);
+}
+
+static void unsubscribeCommand(redisClient *c) {
+ if (c->argc == 1) {
+ pubsubUnsubscribeAll(c,1);
+ return;
+ } else {
+ int j;
+
+ for (j = 1; j < c->argc; j++)
+ pubsubUnsubscribe(c,c->argv[j],1);
+ }
+}
+
+static void publishCommand(redisClient *c) {
+ int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);
+ addReplyLong(c,receivers);
+}
+