1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __TIMETRACKER_H__
#define __TIMETRACKER_H__
#include "config.h"
#include <stdio.h>
#include <time.h>
#include <list>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
class Timetracker {
public:
typedef struct timer_event {
int timer_id;
// Time it was scheduled
struct timeval schedule_tm;
// Explicit trigger time or number of 100000us timeslices
struct timeval trigger_tm;
int timeslices;
// Event is rescheduled again once it expires, if it's a timesliced event
int recurring;
int (*callback)(timer_event *, void *);
void *callback_parm;
};
// Sort alerts by alert trigger time
class SortTimerEventsTrigger {
public:
inline bool operator() (const Timetracker::timer_event *x, const Timetracker::timer_event *y) const {
if ((x->trigger_tm.tv_sec < y->trigger_tm.tv_sec) ||
((x->trigger_tm.tv_sec == y->trigger_tm.tv_sec) && (x->trigger_tm.tv_usec < y->trigger_tm.tv_usec)))
return 1;
return 0;
}
};
Timetracker();
~Timetracker();
// Tick and handle timers
int Tick();
// Register an optionally recurring timer. Slices are 1/100th of a second,
// the smallest linux can slice without getting into weird calls.
int RegisterTimer(int in_timeslices, struct timeval *in_trigger,
int in_recurring, int (*in_callback)(timer_event *, void *),
void *in_parm);
// Remove a timer that's going to execute
int RemoveTimer(int timer_id);
protected:
int next_timer_id;
map<int, timer_event *> timer_map;
vector<timer_event *> sorted_timers;
};
#endif
|