Branch data Line data Source code
1 : : // $Id: IOSource.h 6888 2009-08-20 18:23:11Z vern $
2 : : //
3 : : // Interface for classes providing/consuming data during Bro's main loop.
4 : :
5 : : #ifndef iosource_h
6 : : #define iosource_h
7 : :
8 : : #include <list>
9 : : #include "Timer.h"
10 : :
11 : : using namespace std;
12 : :
13 : : class IOSource {
14 : : public:
15 : 7 : IOSource() { idle = closed = false; }
16 [ # # ][ # # ]: 0 : virtual ~IOSource() {}
[ # # ]
17 : :
18 : : // Returns true if source has nothing ready to process.
19 : 46095 : bool IsIdle() const { return idle; }
20 : :
21 : : // Returns true if more data is to be expected in the future.
22 : : // Otherwise, source may be removed.
23 : 42701 : bool IsOpen() const { return ! closed; }
24 : :
25 : : // Returns select'able fds (leaves args untouched if we don't have
26 : : // selectable fds).
27 : : virtual void GetFds(int* read, int* write, int* except) = 0;
28 : :
29 : : // The following two methods are only called when either IsIdle()
30 : : // returns false or select() on one of the fds indicates that there's
31 : : // data to process.
32 : :
33 : : // Returns timestamp (in global network time) associated with next
34 : : // data item. If the source wants the data item to be processed
35 : : // with a local network time, it sets the argument accordingly.
36 : : virtual double NextTimestamp(double* network_time) = 0;
37 : :
38 : : // Processes and consumes next data item.
39 : : virtual void Process() = 0;
40 : :
41 : : // Returns tag of timer manager associated with last processed
42 : : // data item, nil for global timer manager.
43 : 64923 : virtual TimerMgr::Tag* GetCurrentTag() { return 0; }
44 : :
45 : : // Returns a descriptual tag for debugging.
46 : : virtual const char* Tag() = 0;
47 : :
48 : : protected:
49 : : // Derived classed are to set this to true if they have gone dry
50 : : // temporarily.
51 : : bool idle;
52 : :
53 : : // Derived classed are to set this to true if they have gone dry
54 : : // permanently.
55 : : bool closed;
56 : : };
57 : :
58 : : class IOSourceRegistry {
59 : : public:
60 : 3 : IOSourceRegistry() { call_count = 0; dont_counts = 0; }
61 : : ~IOSourceRegistry();
62 : :
63 : : // If dont_count is true, this source does not contribute to the
64 : : // number of IOSources returned by Size(). The effect is that
65 : : // if all sources but the non-counting ones have gone dry,
66 : : // processing will shut down.
67 : : void Register(IOSource* src, bool dont_count = false);
68 : :
69 : : // This may block for some time.
70 : : IOSource* FindSoonest(double* ts);
71 : :
72 : 21351 : int Size() const { return sources.size() - dont_counts; }
73 : :
74 : : // Terminate IOSource processing immediately by removing all
75 : : // sources (and therefore returning a Size() of zero).
76 : 0 : void Terminate() { RemoveAll(); }
77 : :
78 : : protected:
79 : : // When looking for a source with something to process,
80 : : // every SELECT_FREQUENCY calls we will go ahead and
81 : : // block on a select().
82 : : static const int SELECT_FREQUENCY = 25;
83 : :
84 : : // Microseconds to wait in an empty select if no source is ready.
85 : : static const int SELECT_TIMEOUT = 50;
86 : :
87 : : void RemoveAll();
88 : :
89 : : unsigned int call_count;
90 : : int dont_counts;
91 : :
92 : : struct Source {
93 : : IOSource* src;
94 : : int fd_read;
95 : : int fd_write;
96 : : int fd_except;
97 : : };
98 : :
99 : : typedef list<Source*> SourceList;
100 : : SourceList sources;
101 : : };
102 : :
103 : : extern IOSourceRegistry io_sources;
104 : :
105 : : #endif
|