Abstract from shutdown initiation mechanism:

(shutdown_initiate): New.
(shut): Use it.  Shutdown in zero minutes no longer cancels the
shutdown, it just works.  Use negative argument to cancel.  Logging is
less detailed.
(shutdown_sequence): Internal linkage.

(pr_wall): All callers prefix text it with the same header.  Move it
into the function.
This commit is contained in:
Markus Armbruster 2007-01-20 20:40:52 +00:00
parent 516f42e063
commit 84cfd670ce
5 changed files with 70 additions and 59 deletions

View file

@ -43,15 +43,15 @@ extern time_t update_time;
extern int updating_mob; extern int updating_mob;
void mobility_init(void); void mobility_init(void);
/* thread entry points */
void delete_lostitems(void *);
void market_init(void); void market_init(void);
void mobility_check(void *);
void player_kill_idle(void *);
void update_main(void); void update_main(void);
void update_init(void); void update_init(void);
int update_trigger(time_t); int update_trigger(time_t);
void shutdown_sequence(void *); int shutdown_initiate(int);
/* thread entry points */
void delete_lostitems(void *);
void mobility_check(void *);
void player_kill_idle(void *);
#endif #endif

View file

@ -2,7 +2,7 @@
.NA shutdown "Shut down the server" .NA shutdown "Shut down the server"
.LV Expert .LV Expert
.SY "shutdown <minutes> <disable update?>" .SY "shutdown <minutes> <disable update?>"
Shutdown the server in <minutes> minutes. If <minutes> is 0, Shutdown the server in <minutes> minutes. If <minutes> is negative,
then any previous shutdown request will be canceled. If then any previous shutdown request will be canceled. If
the second argument is a "n" then updates will not be disabled the second argument is a "n" then updates will not be disabled
(otherwise they will be). (otherwise they will be).

View file

@ -49,43 +49,25 @@ shut(void)
shutdown_minutes = shutdown_minutes =
onearg(player->argp[1], onearg(player->argp[1],
"Time until shutdown in minutes (0 to abort shutdown sequence)? "); "Time until shutdown in minutes (-1 to abort shutdown sequence)? ");
if (shutdown_minutes < 0)
return RET_SYN;
if (!updates_disabled()) if (!updates_disabled())
if (!(p = getstarg(player->argp[2], "Disable update [y]? ", buf)) if (!(p = getstarg(player->argp[2], "Disable update [y]? ", buf))
|| *p != 'n') || *p != 'n')
disa(); disa();
shutdown_was_pending = shutdown_pending; shutdown_was_pending = shutdown_initiate(shutdown_minutes);
shutdown_pending = shutdown_minutes + !!shutdown_minutes; if (shutdown_was_pending < 0)
msgbuf[0] = '\0'; return RET_FAIL;
if (shutdown_was_pending) { if (shutdown_was_pending) {
if (shutdown_minutes) { if (shutdown_minutes >= 0) {
sprintf(msgbuf, pr("The shutdown time has been changed to %d minutes!",
": The shutdown time has been changed to %d minutes!", shutdown_minutes);
shutdown_minutes);
} else { } else {
sprintf(msgbuf, ": The server shutdown has been cancelled!"); pr("The server shutdown has been cancelled!");
} }
} else if (shutdown_minutes) { } else if (shutdown_minutes) {
pr("Shutdown sequence begun.\n"); pr("Shutdown sequence begun.\n");
logerror("Shutdown sequence begun");
empth_create(PP_SHUTDOWN, shutdown_sequence, (50 * 1024),
0, "shutdownSeq", "Counts down server shutdown", 0);
}
us = getnatp(player->cnum);
if (msgbuf[0]) {
sendmessage(us, 0, msgbuf, 1);
pr("%s\n", msgbuf + 2);
logerror("%s", msgbuf + 2);
}
if (shutdown_minutes) {
sprintf(msgbuf, ": The server will shut down in %d minutes!",
shutdown_minutes);
sendmessage(us, 0, msgbuf, 1);
pr("%s\n", msgbuf + 2);
logerror("%s", msgbuf + 2);
} }
return RET_OK; return RET_OK;
} }

View file

@ -182,17 +182,26 @@ pr_inform(struct player *pl, char *format, ...)
* Send C_FLASH text to everyone. * Send C_FLASH text to everyone.
* Format text to send using printf-style FORMAT and optional * Format text to send using printf-style FORMAT and optional
* arguments. It is assumed to be plain ASCII. * arguments. It is assumed to be plain ASCII.
* Prefix text it with a header suitable for broadcast from deity.
* Initiate an output queue flush, but do not wait for it to complete. * Initiate an output queue flush, but do not wait for it to complete.
*/ */
void void
pr_wall(char *format, ...) pr_wall(char *format, ...)
{ {
time_t now;
struct tm *tm;
char buf[4096]; /* UTF-8 */ char buf[4096]; /* UTF-8 */
int n;
struct player *p; struct player *p;
va_list ap; va_list ap;
time(&now);
tm = localtime(&now);
n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);
va_start(ap, format); va_start(ap, format);
(void)vsprintf(buf, format, ap); (void)vsprintf(buf + n, format, ap);
va_end(ap); va_end(ap);
for (p = player_next(0); p; p = player_next(p)) { for (p = player_next(0); p; p = player_next(p)) {
if (p->state != PS_PLAYING) if (p->state != PS_PLAYING)

View file

@ -42,46 +42,66 @@
int shutdown_pending; int shutdown_pending;
void static void shutdown_sequence(void *unused);
int
shutdown_initiate(int mins_from_now)
{
int old_pending = shutdown_pending;
if (mins_from_now < 0) {
if (shutdown_pending) {
shutdown_pending = 0;
pr_wall("The server shutdown has been cancelled!\n");
}
return old_pending;
}
shutdown_pending = mins_from_now + 1;
if (old_pending) {
pr_wall("The shutdown time has been changed to %d minutes!\n",
mins_from_now);
/* FIXME wake up shutdown_sequence() */
} else {
if (!empth_create(PP_SHUTDOWN, shutdown_sequence, 50 * 1024,
0, "shutdownSeq", "Counts down server shutdown",
NULL))
return -1;
}
return old_pending;
}
static void
shutdown_sequence(void *unused) shutdown_sequence(void *unused)
{ {
struct natstr *god;
struct tm *tm;
time_t now; time_t now;
char header[100];
if (shutdown_pending <= 0) { pr_wall("The server will shut down in %d minutes!\n",
shutdown_pending = 0; shutdown_pending - 1);
logerror("shutdown called with 0 shutdown_pending");
empth_exit();
return;
}
god = getnatp(0);
while (shutdown_pending > 0) { while (shutdown_pending > 0) {
--shutdown_pending; --shutdown_pending;
time(&now); time(&now);
if (shutdown_pending <= 1440) { /* one day */ if (shutdown_pending <= 1440) { /* one day */
tm = localtime(&now); if (shutdown_pending == 0) {
sprintf(header, "BROADCAST from %s @ %02d:%02d: ",
god->nat_cnam, tm->tm_hour, tm->tm_min);
if (!shutdown_pending) {
pr_wall("%sServer shutting down NOW!\n", header);
shutdwn(0); shutdwn(0);
} else if (shutdown_pending == 1) { } else if (shutdown_pending == 1) {
pr_wall("%sServer shutting down in 1 minute!\n", header); pr_wall("Server shutting down in 1 minute!\n");
} else if (shutdown_pending <= 5) { } else if (shutdown_pending <= 5) {
pr_wall("%sServer shutting down in %d minutes!\n", pr_wall("Server shutting down in %d minutes!\n",
header, shutdown_pending); shutdown_pending);
} else if (shutdown_pending <= 60 } else if (shutdown_pending <= 60
&& shutdown_pending % 10 == 0) { && shutdown_pending % 10 == 0) {
pr_wall("%sThe server will be shutting down in %d minutes!\n", pr_wall("The server will be shutting down in %d minutes!\n",
header, shutdown_pending); shutdown_pending);
} else if (shutdown_pending % 60 == 0) { } else if (shutdown_pending % 60 == 0) {
pr_wall("%sThe server will be shutting down %d hours from now.\n", pr_wall("The server will be shutting down %d hours from now.\n",
header, shutdown_pending / 60); shutdown_pending / 60);
} }
} }
/* FIXME error due to late wakeup accumulates */
empth_sleep(now + 60); empth_sleep(now + 60);
} }
empth_exit();
} }