From 97ec1b6d99fa48e46d9ba15ddbf434df2008dfdb Mon Sep 17 00:00:00 2001 From: rodri Date: Sun, 25 Jul 2021 23:01:56 +0000 Subject: added a #s command interface for monitoring and control. --- muswd.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/muswd.c b/muswd.c index b3667e2..b7cbbbd 100644 --- a/muswd.c +++ b/muswd.c @@ -143,6 +143,69 @@ threadsim(void *) } } +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) { @@ -180,6 +243,7 @@ threadmain(int argc, char *argv[]) lobby = newlobby(); inittheparty(); + threadcreate(threadctl, nil, 4096); threadcreate(threadlisten, adir, 4096); threadcreate(threadsim, nil, 4096); threadexits(nil); -- cgit v1.2.3