Remove warnings
[anna.git] / include / anna / comm / internal / Poll.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_comm_internal_Poll_hpp
10 #define anna_comm_internal_Poll_hpp
11
12 #include <unistd.h>
13 #include <sys/time.h>
14 #include <limits.h>
15
16 #include <anna/config/defines.hpp>
17 #include <anna/core/util/defines.hpp>
18 #include <anna/core/RuntimeException.hpp>
19 #include <anna/core/functions.hpp>
20
21 namespace anna {
22
23 namespace comm {
24
25 /**
26    Clase que asegura que el comm::Reader no se quedara esperando indefinidamente a
27    que un determinado mensaje haya sido tratado y mantiene la integridad de forma
28    ya que asegura que el tratamiento de un mensaje solo tendra un unico signal enviado
29     hacia el comm::Reader.
30 */
31 class Poll {
32 public:
33   Poll() : a_ptrTimeout(NULL), a_minfd(INT_MAX), a_maxfd(0) {
34     FD_ZERO(&a_fdmask);
35     FD_ZERO(&a_fdset);
36   }
37
38   void setTimeout(const Millisecond &timeout) throw();
39
40   void waitMessage() throw(RuntimeException);
41   int fetch() throw();
42   bool isEmpty() const throw() { return a_pollr <= 0; }
43   void clear() throw() { a_maxfd = 0; a_minfd = INT_MAX; FD_ZERO(&a_fdmask); FD_ZERO(&a_fdset);}
44   void insert(const int fd) throw();
45   void erase(const int fd) throw();
46
47 private:
48   fd_set a_fdmask;
49   fd_set a_fdset;
50   timeval* a_ptrTimeout;
51   int a_minfd;
52   int a_maxfd;
53   int a_pollr;
54   int a_ifd;
55   timeval a_timeout;
56
57   static int select(const int maxfd, fd_set* fdset, timeval* timeout)
58   throw(RuntimeException) {
59     int result;
60
61     do {
62       result = ::select(maxfd + 1, fdset, NULL, NULL, timeout);
63     } while(result == -1 && errno == EINTR);
64
65     if(result == -1) {
66       int xerrno = errno;
67       throw RuntimeException("Error en anna::comm::Poll::select", xerrno, ANNA_FILE_LOCATION);
68     }
69
70     return result;
71   }
72
73   static int min(const Millisecond &n1, const Millisecond &n2) throw() { return (n1 < n2) ? n1 : n2; }
74 };
75
76 }
77 }
78
79 #endif