dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_sock_poller.h
1 #ifndef SCD_SOCK_POLLER_H
2 #define SCD_SOCK_POLLER_H
3
4 #include <vector>
5 #include "poll.h"
6
7 #include "scd_socket.h"
8
9
10 typedef short sock_ev;
11 const sock_ev SOCK_EV_READ = POLLIN;
12 const sock_ev SOCK_EV_WRITE = POLLOUT;
13 const sock_ev SOCK_EV_CLOSE = POLLERR | POLLHUP | POLLNVAL;
14
15
16 /**
17  * Interface for classes able to handle socket events.
18  */
19 class scd_sock_ev_handler_if
20 {
21 public:
22     /**
23      * Executes the handler for socket events that have been registered
24      * and have ocured. Callback carried out by scd_sock_poller.
25      *\param events the events that occured
26      */
27     virtual void handle_sock_ev(sock_ev events) = 0;
28
29     /**
30      * Returns the socket that should be watched. The socket must
31      * be the same as long the handler is registered.
32      */
33     virtual const scd_socket &get_sock() = 0;
34
35     virtual ~scd_sock_ev_handler_if() {};
36 };
37
38
39 /**
40  * Dispatcher that watches sockets and executes call backs if watched
41  * event occure. Socket events (sock_ev) are an OR combination of flags
42  * supported by ::poll. See "man 2 poll". The watched events for a specific
43  * event handler stay watched until the events are overwritten or the handler
44  * is removed.
45  */
46 class scd_sock_poller
47 {
48 public:
49     /**
50      * Register a socket event handler (a class handling socket events).
51      * Optionally the events to watch can be specified.
52      * \param handler the object that wants to watch a socket
53      * \param events (optional) the events to watch
54      * \return true if the handler could be registered succesfully
55      */
56     bool register_handler(scd_sock_ev_handler_if &handler, sock_ev events = 0);
57
58     /**
59      * Remove a socket event handler. The socket is not watched anymore.
60      * \param the object that has previously been registered
61      * \return false if no such handler was registered
62      */
63     bool remove_handler(const scd_sock_ev_handler_if &handler);
64
65     /**
66      * Set events to watch. The events expire only when overwritten
67      * by calling this function again or by removing the handler.
68      * \param handler the handler that has been registered before
69      * \param events the events the handler is interested in
70      * \return false if the handler has not been registered before
71      */
72     bool set_ev(const scd_sock_ev_handler_if &handler, sock_ev events);
73
74     /**
75      * Gets the events that are currently watched for the handler.
76      * \param the handler to get the watched events for
77      * \param events the variable where the watched events are written to
78      * \return false if the handler has not been registered before
79      */
80     bool get_ev(const scd_sock_ev_handler_if &handler, sock_ev &events) const;
81
82     /**
83      * Wait (blocking) for registered events or a timeout to occure.
84      * No callbacks are executed.
85      * \param milis timeout in milliseconds to wait for events, if omitted
86      * or -1 it is waited until events occure
87      * \return true if events occured, false otherwise
88      * \exception scd_exception if unexpected errors occured
89      */
90     bool wait(int ms = -1);
91
92     /**
93      * Check if events occured and callback the affected handlers.
94      * \return true if some events occured
95      * \exception scd_exception if unexcpected errors occured
96      */
97     bool process();
98
99     /**
100      * Wait (blocking) for registered events to occure and callback the
101      * affected handlers.
102      * \exception scd_exception if unexcpected errors occured
103      */
104     void wait_process();
105
106 private:
107     /* member variables */
108     std::vector<scd_sock_ev_handler_if *> _handlers;
109     std::vector<struct pollfd> _events;
110
111     /* member functions */
112
113     /**
114      * Checks if a handler is already registered.
115      * \param handler the handler to check for existence
116      * \return true if the handler has been registered before
117      */
118     bool _handler_exists(const scd_sock_ev_handler_if& handler) const;
119
120     /**
121      * Returns the index of an event handler in the vector. This is the same
122      * index as the corresponding pollfd.
123      * \param handler the handler to get the index of
124      * \return the index of the handler or -1 if it could not be found
125      */
126     int _get_index(const scd_sock_ev_handler_if& handler) const;
127
128     /**
129      * Polls during a specified amount of time.
130      * \param ms miliseconds to block (-1 for infinite)
131      * \exception scd_exception if an unexpected error occured
132      * \return number of sockets for witch events occured
133      */
134     int _poll(int ms);
135
136     /**
137      * Calls handlers back for which events occured.
138      */
139     void _callback();
140 }; 
141
142 //scd_sock_poller* scd_get_sock_poller();
143
144 #endif