ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
AbstractDescriptorReader Class Reference

Abstract class to be notified when a descriptor becomes ready for read operations. More...

#include <AbstractDescriptorReader.hpp>

Public Member Functions

 AbstractDescriptorReader (DescriptorsMonitor &dm)
 Constructor.
virtual ~AbstractDescriptorReader ()
virtual void dataAvailable (PosixDescriptor &descriptor)=0
 Method called when the descriptor becomes ready.
bool monitorDescriptor (PosixDescriptor &descriptor)
 Method to start monitoring a descriptor.
bool stopMonitorDescriptor (PosixDescriptor &descriptor)
 Method to stop monitoring a descriptor.

Private Attributes

DescriptorsMonitordm_
 Monitor that will notify the readiness of descriptors.

Detailed Description

Abstract class to be notified when a descriptor becomes ready for read operations.

"Observer" class to monitor readiness of a PosixDescriptor. This class allows to create a subclass which is notified when a descriptor becomes ready for read operations. Inherit from this class if your class needs to monitor one or more (file or socket) descriptors for readiness to read operations. The constructor receives as argument a DescriptorsMonitor class, which is the class in charge of monitoring the status of all the given descriptors, and notifying (through a call to the dataAvailable() method) the readiness of a specific descriptor. This class, together with DescriptorsMonitor, implements the "Observer" design pattern.

Example of class notified when data are available reading from a file:

class FileReader: public AbstractDescriptorReader {
FileDescriptor fd_;
std::string name_;
int calledTimes_;
public:
FileReader(DescriptorsMonitor& dm,
const std::string& name,
const std::string& filename):
fd_(filename, O_RDONLY),
name_(name),
calledTimes_(0){
}
~FileReader(){}
void dataAvailable(PosixDescriptor& descriptor) {
calledTimes_++;
Buffer buff(100);
std::cerr << "Name: " << name_
<< "\tDescriptor: " << descriptor
<< "\tTimes: " << calledTimes_ << std::endl;
int ret = fd_.read(&buff, buff.getSize());
std::cerr << ret << "bytes read" << std::endl;
if (descriptor.getDescriptorNumber() !=
fd_.getDescriptorNumber())
ERROR("Called with wrong descriptor!");
}
};

Definition at line 77 of file AbstractDescriptorReader.hpp.

Constructor & Destructor Documentation

AbstractDescriptorReader ( DescriptorsMonitor dm)
inlineexplicit

Constructor.

It just saves the pointer of the DescriptorsMonitor, that will be used to register at the monitor when monitorDescriptor() will be called.

Parameters
referenceto the DescriptorsMonitor

Definition at line 97 of file AbstractDescriptorReader.hpp.

:dm_(&dm){}
virtual ~AbstractDescriptorReader ( )
inlinevirtual

Definition at line 99 of file AbstractDescriptorReader.hpp.

{}

Member Function Documentation

virtual void dataAvailable ( PosixDescriptor descriptor)
pure virtual

Method called when the descriptor becomes ready.

Once monitorDescriptor(int descriptor) has been called, this method will be automatically called by the DescriptorsMonitor whenever the descriptor becomes available.

Parameters
Referenceto the descriptor that became ready for read operations (the inherited class may want to be called when more than one descriptor becomes ready).
bool monitorDescriptor ( PosixDescriptor descriptor)
inline

Method to start monitoring a descriptor.

This method is usually called inside the constructor of the inherited class and allows to start monitoring a specific descriptor.

Parameters
Descriptorthat must be monitored
Returns
true in case of success, false otherwise

Definition at line 124 of file AbstractDescriptorReader.hpp.

{
return dm_->startMonitoringDescriptor(*this, descriptor);
}

Here is the call graph for this function:

bool stopMonitorDescriptor ( PosixDescriptor descriptor)
inline

Method to stop monitoring a descriptor.

Parameters
Descriptorthat must be monitored
Returns
true in case of success, false otherwise

Definition at line 134 of file AbstractDescriptorReader.hpp.

{
return dm_->stopMonitoringDescriptor(descriptor);
}

Here is the call graph for this function:

Member Data Documentation

DescriptorsMonitor* dm_
private

Monitor that will notify the readiness of descriptors.

This is the monitor which, once monitorDescriptor(int descriptor) has been called, will automatically call dataAvailable(int descriptor) whenever such a descriptor becomes available for read operations.

Definition at line 86 of file AbstractDescriptorReader.hpp.


The documentation for this class was generated from the following file: