shithub: mq

Download patch

ref: 86f2bdceaa324b0d6207593e439d8acd7456945f
parent: 0dd4900441c6f226dbfa00b18018b0178e9ef05a
author: kvik <kvik@a-b.xyz>
date: Tue Sep 15 05:05:35 EDT 2020

mq: implement 'most-recent' stream replay mode

--- a/man/4/mq
+++ b/man/4/mq
@@ -165,7 +165,7 @@
 .B data message | coalesce
 Sets the data mode.
 .TP
-.B replay no | last | all
+.B replay off | last | all
 Sets the queue replay mode.
 .TP
 .B depth <size> | <count>
--- a/src/mq.c
+++ b/src/mq.c
@@ -18,7 +18,7 @@
 	Stream *order;
 
 	/* configuration */
-	int replay;
+	enum {Replayoff, Replaylast, Replayall} replay;
 };
 
 struct Stream {
@@ -85,7 +85,7 @@
 	mq = emalloc(sizeof(Mq));
 	mq->group = (Stream*)listalloc();
 	mq->order = (Stream*)streamalloc(mq);
-	mq->replay = 0;
+	mq->replay = Replayoff;
 
 	ctl = order = nil;
 	if((d = createfile(parent, name, uid, perm, mq)) == nil)
@@ -266,7 +266,7 @@
 	Cmddebug, Cmddebug9p,
 };
 Cmdtab mqcmd[] = {
-	/* replay on|off*/
+	/* replay off|last|all */
 	{Cmdreplay, "replay", 2},
 
 	/* debug on|off */
@@ -293,13 +293,16 @@
 	}
 	switch(t->index){
 	case Cmdreplay: {
-		if(strncmp(cmd->f[1], "on", 2) == 0)
-			mq->replay = 1;
-		else
 		if(strncmp(cmd->f[1], "off", 3) == 0)
-			mq->replay = 0;
+			mq->replay = Replayoff;
 		else
-			e = "usage: replay on|off";
+		if(strncmp(cmd->f[1], "last", 4) == 0)
+			mq->replay = Replaylast;
+		else
+		if(strncmp(cmd->f[1], "all", 3) == 0)
+			mq->replay = Replayall;
+		else
+			e = "usage: replay off|last|all";
 		break;
 	}
 	case Cmddebug: {
@@ -367,10 +370,14 @@
 		Client *c;
 
 		c = r->fid->aux = emalloc(sizeof(Client));
-		if(s->mq->replay)
-			c->cursor = (Write*)s->queue;
-		else
-			c->cursor = (Write*)s->queue->tail;
+		switch(s->mq->replay){
+		case Replayoff:
+			c->cursor = (Write*)s->queue->tail; break;
+		case Replaylast:
+			c->cursor = (Write*)s->queue->tail->tail; break;
+		case Replayall:
+			c->cursor = (Write*)s->queue; break;
+		}
 		break;
 	}}
 	respond(r, nil);