shithub: musw

Download patch

ref: 97ec1b6d99fa48e46d9ba15ddbf434df2008dfdb
parent: bdaa63a1a4604ada3539aeb50e4af144da3ba6f0
author: rodri <rgl@antares-labs.eu>
date: Sun Jul 25 19:01:56 EDT 2021

added a #s command interface for monitoring and control.

--- a/muswd.c
+++ b/muswd.c
@@ -144,6 +144,69 @@
 }
 
 void
+fprintstats(int fd)
+{
+	Party *p;
+	ulong nparties = 0;
+
+	for(p = theparty.next; p != &theparty; p = p->next)
+		nparties++;
+
+	fprint(fd, "curplayers	%lud\n"
+		   "totplayers	%lud\n"
+		   "maxplayers	%lud\n"
+		   "curparties	%lud\n"
+		   "totparties	%lud\n",
+		lobby->nseats, (ulong)0, lobby->cap,
+		nparties, (ulong)0);
+}
+
+void
+fprintstate(int fd)
+{
+	fprint(fd, "x	%g\n"
+		   "v	%g\n",
+		state.x, state.v);
+}
+
+void
+threadctl(void *)
+{
+	int fd, pfd[2], n, ncmdargs;
+	char buf[256], *usr, *cmdargs[2];
+	Ioproc *io;
+
+	if(pipe(pfd) < 0)
+		sysfatal("pipe: %r");
+
+	usr = getenv("user");
+	snprint(buf, sizeof buf, "/srv/muswctl.%s.%d", usr, getpid());
+	free(usr);
+
+	fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600);
+	if(fd < 0)
+		sysfatal("open: %r");
+	fprint(fd, "%d", pfd[0]);
+	close(pfd[0]);
+
+	io = ioproc();
+	while((n = ioread(io, pfd[1], buf, sizeof(buf)-1)) > 0){
+		buf[n] = 0;
+
+		ncmdargs = tokenize(buf, cmdargs, 2);
+		if(ncmdargs == 2){
+			if(strcmp(cmdargs[0], "show") == 0){
+				if(strcmp(cmdargs[1], "stats") == 0)
+					fprintstats(pfd[1]);
+				else if(strcmp(cmdargs[1], "state") == 0)
+					fprintstate(pfd[1]);
+			}
+		}
+	}
+	closeioproc(io);
+}
+
+void
 usage(void)
 {
 	fprint(2, "usage: %s [-d] [-a addr]\n", argv0);
@@ -180,6 +243,7 @@
 	lobby = newlobby();
 	inittheparty();
 
+	threadcreate(threadctl, nil, 4096);
 	threadcreate(threadlisten, adir, 4096);
 	threadcreate(threadsim, nil, 4096);
 	threadexits(nil);