[mw-devel] MW3 r1302 - in trunk/src: . server
arthur at sucs.org
arthur at sucs.org
Thu Nov 29 15:20:33 GMT 2012
Author: arthur
Date: 2012-11-29 15:20:32 +0000 (Thu, 29 Nov 2012)
New Revision: 1302
Modified:
trunk/src/chattable.c
trunk/src/incoming.c
trunk/src/ipc.c
trunk/src/ipc.h
trunk/src/server/mwserv.c
trunk/src/server/servsock.c
trunk/src/socket.c
trunk/src/socket.h
trunk/src/talker.c
trunk/src/talker.h
Log:
Add a server uptime command
Modified: trunk/src/chattable.c
===================================================================
--- trunk/src/chattable.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/chattable.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -76,6 +76,7 @@
{"ungag" ,0x0002 ,1, "Usage: ungag <user>", "Remove the Gag on the user", t_ungag, 1},
{"unignore" ,0x0000 ,1, "Usage: unignore <user>", "Stop filtering user out so you can hear them", t_unignore, 1},
{"unprotect" ,0x0020 ,1, "Usage: unprotect <user>", "Remove protection from user", t_unprotect, 1},
+{"uptime" ,0x0000 ,0, "Usage: uptime", "Show server uptime", t_uptime, 1},
{"uri" ,0x0000 ,0, "Usage: uri [list [*|username] [n] | delete <id> | nsfw <id> | membersonly <id> | anonymous <id> | displaymode <full|short>]", "View/Edit the uris in the mwuri database", t_uri, 1},
{"variables" ,0x0200 ,0, "Usage: variables [mask]", "List script variables + contents [or starting with mask]", t_showvars, 1},
{"what" ,0x0000 ,0, "Usage: what", "See peoples status", t_what, 1},
Modified: trunk/src/incoming.c
===================================================================
--- trunk/src/incoming.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/incoming.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -45,7 +45,7 @@
static int events_cancelled = 0;
static struct mstack *MesgStack=NULL;
-static void accept_pipe_cmd(enum ipc_types state, char *newbuf, int mesg_pid, struct person *mesg_user);
+static void accept_pipe_cmd(ipc_message_t *msg, struct person *mesg_user);
static void force_text(char *text, char *from);
static void force_ipc(char *text, char *from);
static void force_rpc(char *text, char *from);
@@ -430,22 +430,18 @@
{
long mesg_posn;
static struct person mesg_user;
- static char newbuff[MAXPIPELENGTH];
mesg_waiting = 0;
ipc_message_t * msg = read_socket(ipcsock, 1);
while (msg != NULL) {
- if ((mesg_posn=get_who_userposn(msg->head.src))<0)
+ if (msg->head.src == 0 || (mesg_posn=get_who_userposn(msg->head.src))<0)
strcpy(mesg_user.name,"System");
else
fetch_user(&mesg_user, mesg_posn);
- bzero(newbuff, sizeof(newbuff));
- memcpy(newbuff, msg->body, _MIN(sizeof(newbuff)-1, msg->bodylen));
- accept_pipe_cmd(msg->head.type, newbuff, msg->head.src, &mesg_user);
+ accept_pipe_cmd(msg, &mesg_user);
- ipcmsg_destroy(msg);
msg = read_socket(ipcsock, 0);
}
if (msg == NULL && !ipc_connected()) {
@@ -465,9 +461,22 @@
sscanf(newbuff,"%d:%d",&fol,&pos);
user->lastread[fol]=pos;
}
+
+static void display_uptime(ipc_message_t *msg)
+{
+ json_t * j = json_init(msg);
+ const char *version = json_getstring(j, "version");
+ const int uptime = json_getint(j, "uptime");
-static void accept_pipe_cmd(enum ipc_types state, char *newbuff, int mesg_pid, struct person *mesg_user)
+ printf("Server version: %s\n", version);
+ printf("Server uptime: %s\n", itime(uptime));
+}
+
+static void accept_pipe_cmd(ipc_message_t *msg, struct person *mesg_user)
{
+ enum ipc_types state = msg->head.type;
+ char *newbuff = msg->body;
+
/*printf("\n<message type is %d>\n", state);*/
switch (state) {
case IPC_NEWMAIL:
@@ -545,10 +554,14 @@
case IPC_CLEARIGN:
force_clearign();
break;
+ case IPC_UPTIME:
+ display_uptime(msg);
+ break;
default:
devel_msg("incoming_mesg", "unknown message type %d.\007", state);
}
+ ipcmsg_destroy(msg);
update_user(user,userposn);
}
Modified: trunk/src/ipc.c
===================================================================
--- trunk/src/ipc.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/ipc.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -193,3 +193,14 @@
return ipc_send_to_processes(NULL, msgtype, data, filter, filterinfo);
}
+
+/*
+ * most of the mw code should use this to send a message object
+ * fourcc and destination will control what happens when it
+ * gets to the server
+ */
+void ipcmsg_transmit(ipc_message_t * msg)
+{
+ ipc_check();
+ ipcmsg_send(msg, ipcsock);
+}
Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/ipc.h 2012-11-29 15:20:32 UTC (rev 1302)
@@ -4,7 +4,13 @@
#include <unistd.h>
#include "who.h"
#include "user.h"
+#include "socket.h"
+#define MKFOURCC(a,b,c,d) \
+ ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
+ | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
+
+
enum ipc_types {
IPC_NOOP = 0, // Just go through the motions, don't actually write anything to the FIFO
IPC_NEWMAIL = 1,
@@ -30,7 +36,8 @@
IPC_CHECKONOFF = 23,
IPC_PROTLEVEL = 24,
IPC_PROTPOWER = 25,
- IPC_DOING = 26
+ IPC_DOING = 26,
+ IPC_UPTIME = MKFOURCC('U','P','T','M')
};
typedef int (send_filter)(const struct person * usr, const struct who * who, const void * info);
@@ -43,5 +50,7 @@
int ipc_send_to_pid(pid_t dest, enum ipc_types msgtype, const char * data);
unsigned int ipc_send_to_username(const char * dest, enum ipc_types msgtype, const char * data);
unsigned int ipc_send_to_all(enum ipc_types msgtype, const char * data, send_filter * filter, const void * filterinfo);
+void ipcmsg_transmit(ipc_message_t * msg);
+
#endif
Modified: trunk/src/server/mwserv.c
===================================================================
--- trunk/src/server/mwserv.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/server/mwserv.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -17,6 +17,8 @@
int userposn = 0;
#define MWUSER "mw"
+
+time_t uptime = 0;
struct servopts {
uint16_t port;
@@ -120,6 +122,7 @@
int fd = openwhofile(O_TRUNC|O_WRONLY);
close(fd);
+ uptime = time(0);
watch_mainsock(mainsock);
printf("Done.\n");
}
Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/server/servsock.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -9,6 +9,7 @@
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
+#include <time.h>
#include "../socket.h"
#include "servsock.h"
@@ -228,6 +229,7 @@
free(mtx);
}
+extern time_t uptime;
void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
{
@@ -259,6 +261,25 @@
return;
}
+ if (msg->head.type == FOURCC("UPTM")) {
+ time_t now = time(0);
+
+ ipcmsg_destroy(msg);
+
+ msg = ipcmsg_create(FOURCC("UPTM"), 0);
+ ipcmsg_destination(msg, conn->addr);
+
+ json_t * j = json_init(NULL);
+ json_addint(j, "uptime", now - uptime);
+ char *version = NULL;
+ asprintf(&version, "%s.%s.%s", VER_MAJ, VER_MIN, VER_TWK);
+ json_addstring(j, "version", version);
+ ipcmsg_json_encode(msg, j);
+ msg_attach(msg, conn);
+ if (msg->refcount <= 0) ipcmsg_destroy(msg);
+ return;
+ }
+
/* otherwise redistribute this message to intended target */
struct list_head *pos;
list_for_each(pos, &connection_list) {
Modified: trunk/src/socket.c
===================================================================
--- trunk/src/socket.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/socket.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -307,3 +307,12 @@
if (!json_is_string(tmp)) return NULL;
return json_string_value(tmp);
}
+
+int json_getint(json_t * js, const char * key)
+{
+ json_t * tmp;
+ tmp = json_object_get(js, key);
+ if (tmp == NULL) return 0;
+ if (!json_is_integer(tmp)) return 0;
+ return json_integer_value(tmp);
+}
Modified: trunk/src/socket.h
===================================================================
--- trunk/src/socket.h 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/socket.h 2012-11-29 15:20:32 UTC (rev 1302)
@@ -64,5 +64,6 @@
int json_addstring(json_t *js, const char *key, const char *value);
int json_addint(json_t *js, const char *key, int value);
const char *json_getstring(json_t *js, const char *key);
+int json_getint(json_t *js, const char *key);
#endif
Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/talker.c 2012-11-29 15:20:32 UTC (rev 1302)
@@ -267,6 +267,12 @@
who_list(1);
}
+void t_uptime(CommandList *cm, int argc, const const char **argv, char *args)
+{
+ ipc_message_t * msg = ipcmsg_create(FOURCC("UPTM"), getpid());
+ ipcmsg_transmit(msg);
+ ipcmsg_destroy(msg);
+}
void t_quit(CommandList *cm, int argc, const char **argv, char *args)
{
Modified: trunk/src/talker.h
===================================================================
--- trunk/src/talker.h 2012-11-28 19:38:23 UTC (rev 1301)
+++ trunk/src/talker.h 2012-11-29 15:20:32 UTC (rev 1302)
@@ -60,6 +60,7 @@
void t_mwrc(CommandList *cm, int argc, const char **argv, char *args);
void t_uri(CommandList *cm, int argc, const char **argv, char *args);
void t_topic(CommandList *cm, int argc, const char **argv, char *args);
+void t_uptime(CommandList *cm, int argc, const char **argv, char *args);
void t_chaton(void);
void chat_say(char *text);
More information about the mw-devel
mailing list