Merge branch 'master' of feistymeow.org:feisty_meow
[feisty_meow.git] / octopi / library / sockets / sequence_tracker.h
1 #ifndef SEQUENCE_TRACKER_CLASS
2 #define SEQUENCE_TRACKER_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : sequence_tracker                                                  *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *  Purpose:                                                                   *
10 *                                                                             *
11 *    Tracks sequence numbers coming from a collection of hosts.  These are    *
12 *  presumably attached to network packets.  The intention is to record if     *
13 *  we've already seen a packet or not.  When hosts have not communicated for  *
14 *  a while, they are removed from tracking.  Also, when enough time has       *
15 *  elapsed for a sequence number, we consider that we've heard everything     *
16 *  we're going to before that sequence number; hence, any prior sequence      *
17 *  numbers are considered already received.                                   *
18 *                                                                             *
19 *******************************************************************************
20 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
21 * redistribute it and/or modify it under the terms of the GNU General Public  *
22 * License as published by the Free Software Foundation; either version 2 of   *
23 * the License or (at your option) any later version.  This is online at:      *
24 *     http://www.fsf.org/copyleft/gpl.html                                    *
25 * Please send any updates to: fred@gruntose.com                               *
26 \*****************************************************************************/
27
28 #include <basis/contracts.h>
29 #include <basis/mutex.h>
30 #include <timely/time_stamp.h>
31
32 namespace sockets {
33
34 class host_history;
35 class machine_uid;
36
37 //! this will keep track of sequencing for a communication process on a per host basis.
38
39 class sequence_tracker : public virtual basis::root_object
40 {
41 public:
42   sequence_tracker(int coalesce_time, int silence_time);
43     // tracks the sequence numbers from a set of hosts.  the "coalesce_time" is
44     // the interval that we wait before considering all prior sequence numbers
45     // to have been received.  the "silence_time" is the time interval a host
46     // is allowed to be silent before being eliminated.  all measurements are
47     // in milliseconds.
48
49   ~sequence_tracker();
50
51   DEFINE_CLASS_NAME("sequence_tracker");
52
53   void add_pair(const machine_uid &host, int sequence);
54     // adds a hostname/sequence# pair as being received "now".
55
56   bool have_seen(const machine_uid &host, int sequence);
57     // returns true if the "host" and "sequence" have already been seen in
58     // a previous transmission.
59
60   void clean_up();
61     // this must be invoked periodically to allow the clearing of outdated
62     // information.  once a second seems frequent enough.
63
64   basis::astring text_form(bool verbose = false) const;
65     // provides a dump of the information held in the tracker.
66
67 private:
68   int _coalesce_time;  // sequences older than this get coalesced.
69   int _silence_time;  // hosts silent for longer than this get canned.
70   host_history *_hosts;  // the overall record of sequence activity per host.
71   basis::mutex *_lock;  // protects from multi-threaded access.
72 };
73
74 } //namespace.
75
76 #endif
77