ONPOSIX
2.0
|
Class to watch a set of descriptors and notify related classes. More...
#include <DescriptorsMonitor.hpp>
Classes | |
struct | monitoredDescriptor |
Association between a reader and a monitored descriptor. More... |
Public Member Functions | |
DescriptorsMonitor () | |
Constructor. It just initializes the set of descriptors. | |
virtual | ~DescriptorsMonitor () |
Destructor. | |
bool | startMonitoringDescriptor (AbstractDescriptorReader &reader, PosixDescriptor &descriptor) |
Method to start monitoring a descriptor. | |
bool | stopMonitoringDescriptor (PosixDescriptor &descriptor) |
Method to stop monitoring a descriptor. | |
bool | wait () |
Method to wait until some descriptor becomes ready for read operations. |
Private Attributes | |
fd_set | descriptorSet_ |
Current set of monitored descriptors. | |
int | highestDescriptor_ |
Highest-value descriptor in descriptorSet_. | |
std::vector < monitoredDescriptor * > | descriptors_ |
Associations between readers and monitored descriptors. |
Class to watch a set of descriptors and notify related classes.
This class implements the "Observer" design pattern, and allows classes inherited from AbstractDescriptorReader to be notified when a descriptor they monitor becomes ready for read operations. The class is a wrapper for the select() POSIX system call, so the descriptor may refer to both a file or a socket. When the descriptor becomes ready, this class notifies the reader class by calling AbstractDescriptorReader::dataAvailable(int descriptor). Notes:
It is not implemented as a Singleton because it must be possible to have more than one monitor with different sets of descriptors. Classes that want to make use of this class must inherit from virtual class AbstractDescriptorReader, because the readiness of the descriptor is notified through a call to AbstractDescriptorReader::dataAvailable(int descriptor).
Definition at line 62 of file DescriptorsMonitor.hpp.
Constructor. It just initializes the set of descriptors.
Definition at line 32 of file DescriptorsMonitor.cpp.
|
virtual |
Destructor.
It just deletes the data structure containing the association between readers and monitored descriptors. Note: it does not deletes the descriptors and the readers, because they are just pointers to classes allocated somewhere else.
Definition at line 45 of file DescriptorsMonitor.cpp.
bool startMonitoringDescriptor | ( | AbstractDescriptorReader & | reader, |
PosixDescriptor & | descriptor | ||
) |
Method to start monitoring a descriptor.
It is called by each class AbstractDescriptorReader that wants to be notified about a specific descriptor.
reader | class that wants to be notified |
descriptor | descriptor |
Definition at line 61 of file DescriptorsMonitor.cpp.
bool stopMonitoringDescriptor | ( | PosixDescriptor & | descriptor | ) |
Method to stop monitoring a descriptor.
It is called by each class AbstractDescriptorReader that wants to stop notifications about a specific descriptor. The AbstractDescriptorReader class is not among arguments, because each descriptor can be monitored by at most one class.
descriptor | whose notifications must be stopped |
Definition at line 90 of file DescriptorsMonitor.cpp.
bool wait | ( | ) |
Method to wait until some descriptor becomes ready for read operations.
It suspends the execution of the program until a descriptor becomes ready.
Definition at line 117 of file DescriptorsMonitor.cpp.
|
private |
Associations between readers and monitored descriptors.
Definition at line 98 of file DescriptorsMonitor.hpp.
|
private |
Current set of monitored descriptors.
This set is given as argument to the select() syscall.
Definition at line 68 of file DescriptorsMonitor.hpp.
|
private |
Highest-value descriptor in descriptorSet_.
The select() syscall needs this value + 1.
Definition at line 75 of file DescriptorsMonitor.hpp.