ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
DescriptorsMonitor.hpp
Go to the documentation of this file.
1 /*
2  * DescriptorsMonitor.hpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef DESCRIPTORSMONITOR_HPP_
22 #define DESCRIPTORSMONITOR_HPP_
23 
24 #include <sys/select.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <vector>
29 
30 #include "PosixDescriptor.hpp"
31 
32 namespace onposix {
33 
34 class AbstractDescriptorReader;
35 
36 /**
37  * \brief Class to watch a set of descriptors and notify related classes.
38  *
39  * This class implements the "Observer" design pattern, and allows classes
40  * inherited from AbstractDescriptorReader to be notified when a descriptor
41  * they monitor becomes ready for read operations.
42  * The class is a wrapper for the select() POSIX system call, so the
43  * descriptor may refer to both a file or a socket.
44  * When the descriptor becomes ready, this class notifies the reader
45  * class by calling AbstractDescriptorReader::dataAvailable(int descriptor).
46  * Notes:
47  * <ul>
48  * <li> This monitor works only for read operations (i.e., it does
49  * not monitor write operations or system exceptions) even if it can
50  * be easily extended to support also write operations.
51  * <li> One descriptor can be monitored by at most one receiver.
52  * <li> A receiver can monitor more than one descriptor.
53  * </ul>
54  * It is not implemented as a Singleton because it must be possible to have
55  * more than one monitor with different sets of descriptors.
56  * Classes that want to make use of this class must inherit from virtual
57  * class AbstractDescriptorReader, because the readiness of the descriptor
58  * is notified through a call to
59  * AbstractDescriptorReader::dataAvailable(int descriptor).
60  */
61 
63  /**
64  * \brief Current set of monitored descriptors.
65  *
66  * This set is given as argument to the select() syscall.
67  */
69 
70  /**
71  * \brief Highest-value descriptor in descriptorSet_.
72  *
73  * The select() syscall needs this value + 1.
74  */
76 
77  /**
78  * \brief Association between a reader and a monitored descriptor.
79  */
81  /**
82  * \brief Pointer to the observer class.
83  *
84  * This points to the class that wants to be notified when
85  * the descriptor ise ready for read operations.
86  */
88 
89  /**
90  * \brief Monitored descriptor.
91  */
93  };
94 
95  /**
96  * \brief Associations between readers and monitored descriptors
97  */
98  std::vector<monitoredDescriptor*> descriptors_;
99 
100 public:
102  virtual ~DescriptorsMonitor();
104  PosixDescriptor& descriptor);
105  bool stopMonitoringDescriptor(PosixDescriptor& descriptor);
106  bool wait();
107 };
108 
109 } /* onposix */
110 
111 #endif /* DESCRIPTORSMONITOR_HPP_ */