server/src/common/triggers/timeout.c

110 lines
2.3 KiB
C
Raw Normal View History

2001-01-25 10:37:55 +01:00
/* vi: set ts=2:
*
*
* Eressea PB(E)M host Copyright (C) 1998-2003
2001-01-25 10:37:55 +01:00
* Christian Schlittchen (corwin@amber.kn-bremen.de)
* Katja Zedel (katze@felidae.kn-bremen.de)
* Henning Peters (faroul@beyond.kn-bremen.de)
* Enno Rehling (enno@eressea.de)
2001-01-25 10:37:55 +01:00
* Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
*
* This program may not be used, modified or distributed without
* prior permission by the authors of Eressea.
*/
#include <config.h>
#include <kernel/eressea.h>
2001-01-25 10:37:55 +01:00
#include "timeout.h"
2001-02-20 23:54:05 +01:00
/* util includes */
2007-06-20 02:34:02 +02:00
#include <util/attrib.h>
#include <util/event.h>
#include <util/log.h>
#include <util/storage.h>
2001-01-25 10:37:55 +01:00
#include <stdio.h>
#include <stdlib.h>
/***
** timeout
**/
typedef struct timeout_data {
trigger * triggers;
int timer;
variant trigger_data;
2001-01-25 10:37:55 +01:00
} timeout_data;
static void
timeout_init(trigger * t)
{
t->data.v = calloc(sizeof(timeout_data), 1);
2001-01-25 10:37:55 +01:00
}
static void
timeout_free(trigger * t)
{
timeout_data * td = (timeout_data*)t->data.v;
free_triggers(td->triggers);
free(t->data.v);
2001-01-25 10:37:55 +01:00
}
static int
timeout_handle(trigger * t, void * data)
{
/* call an event handler on timeout.
* data.v -> ( variant event, int timer )
*/
timeout_data * td = (timeout_data*)t->data.v;
if (--td->timer==0) {
handle_triggers(&td->triggers, NULL);
return -1;
}
unused(data);
return 0;
2001-01-25 10:37:55 +01:00
}
static void
timeout_write(const trigger * t, struct storage * store)
2001-01-25 10:37:55 +01:00
{
timeout_data * td = (timeout_data*)t->data.v;
store->w_int(store, td->timer);
write_triggers(store, td->triggers);
2001-01-25 10:37:55 +01:00
}
static int
timeout_read(trigger * t, struct storage * store)
2001-01-25 10:37:55 +01:00
{
timeout_data * td = (timeout_data*)t->data.v;
td->timer = store->r_int(store);
read_triggers(store, &td->triggers);
if (td->timer>20) {
trigger * tr = td->triggers;
log_warning(("there is a timeout lasting for another %d turns\n", td->timer));
while (tr) {
log_warning((" timeout triggers: %s\n", tr->type->name));
tr = tr->next;
}
}
if (td->triggers!=NULL && td->timer>0) return AT_READ_OK;
return AT_READ_FAIL;
2001-01-25 10:37:55 +01:00
}
trigger_type tt_timeout = {
"timeout",
timeout_init,
timeout_free,
timeout_handle,
timeout_write,
timeout_read
2001-01-25 10:37:55 +01:00
};
trigger *
trigger_timeout(int time, trigger * callbacks)
{
trigger * t = t_new(&tt_timeout);
timeout_data * td = (timeout_data*)t->data.v;
td->triggers = callbacks;
td->timer = time;
return t;
2001-01-25 10:37:55 +01:00
}