ONPOSIX
2.0
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Enumerator
Friends
Macros
Pages
AbstractDescriptorReader.hpp
Go to the documentation of this file.
1
/*
2
* AbstractDescriptorReader.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 ABSTRACTDESCRIPTORREADER_HPP_
22
#define ABSTRACTDESCRIPTORREADER_HPP_
23
24
#include "
DescriptorsMonitor.hpp
"
25
#include "
PosixDescriptor.hpp
"
26
27
namespace
onposix {
28
29
/**
30
* \brief Abstract class to be notified when a descriptor becomes ready for
31
* read operations.
32
*
33
* "Observer" class to monitor readiness of a PosixDescriptor.
34
* This class allows to create a subclass which is notified when a descriptor
35
* becomes ready for read operations.
36
* Inherit from this class if your class needs to monitor one or more
37
* (file or socket) descriptors for readiness to read operations.
38
* The constructor receives as argument a DescriptorsMonitor class, which is
39
* the class in charge of monitoring the status of all the given descriptors,
40
* and notifying (through a call to the dataAvailable() method) the readiness
41
* of a specific descriptor.
42
* This class, together with DescriptorsMonitor, implements the "Observer"
43
* design pattern.
44
*
45
* Example of class notified when data are available reading from a file:
46
* \code
47
* class FileReader: public AbstractDescriptorReader {
48
* FileDescriptor fd_;
49
* std::string name_;
50
* int calledTimes_;
51
* public:
52
* FileReader(DescriptorsMonitor& dm,
53
* const std::string& name,
54
* const std::string& filename):
55
* AbstractDescriptorReader(dm),
56
* fd_(filename, O_RDONLY),
57
* name_(name),
58
* calledTimes_(0){
59
* monitorDescriptor(fd_);
60
* }
61
* ~FileReader(){}
62
* void dataAvailable(PosixDescriptor& descriptor) {
63
* calledTimes_++;
64
* Buffer buff(100);
65
* std::cerr << "Name: " << name_
66
* << "\tDescriptor: " << descriptor
67
* << "\tTimes: " << calledTimes_ << std::endl;
68
* int ret = fd_.read(&buff, buff.getSize());
69
* std::cerr << ret << "bytes read" << std::endl;
70
* if (descriptor.getDescriptorNumber() !=
71
* fd_.getDescriptorNumber())
72
* ERROR("Called with wrong descriptor!");
73
* }
74
* };
75
* \endcode
76
*/
77
class
AbstractDescriptorReader
{
78
/**
79
* \brief Monitor that will notify the readiness of descriptors.
80
*
81
* This is the monitor which, once monitorDescriptor(int descriptor)
82
* has been called, will automatically call
83
* dataAvailable(int descriptor)
84
* whenever such a descriptor becomes available for read operations.
85
*/
86
DescriptorsMonitor
*
dm_
;
87
88
public
:
89
/**
90
* \brief Constructor.
91
*
92
* It just saves the pointer of the DescriptorsMonitor, that will be
93
* used to register at the monitor when monitorDescriptor() will be
94
* called.
95
* @param reference to the DescriptorsMonitor
96
*/
97
explicit
AbstractDescriptorReader
(
DescriptorsMonitor
& dm):
dm_
(&dm){}
98
99
virtual
~AbstractDescriptorReader
(){}
100
101
102
/**
103
* \brief Method called when the descriptor becomes ready
104
*
105
* Once monitorDescriptor(int descriptor) has been called, this
106
* method
107
* will be automatically called by the DescriptorsMonitor
108
* whenever the descriptor becomes available.
109
* @param Reference to the descriptor that became ready for read
110
* operations (the inherited class may want to be called when more than
111
* one descriptor becomes ready).
112
*/
113
virtual
void
dataAvailable
(
PosixDescriptor
& descriptor)=0;
114
115
/**
116
* \brief Method to start monitoring a descriptor.
117
*
118
* This method is usually called inside the constructor of the
119
* inherited
120
* class and allows to start monitoring a specific descriptor.
121
* @param Descriptor that must be monitored
122
* @return true in case of success, false otherwise
123
*/
124
inline
bool
monitorDescriptor
(
PosixDescriptor
& descriptor){
125
return
dm_
->
startMonitoringDescriptor
(*
this
, descriptor);
126
}
127
128
/**
129
* \brief Method to stop monitoring a descriptor.
130
*
131
* @param Descriptor that must be monitored
132
* @return true in case of success, false otherwise
133
*/
134
inline
bool
stopMonitorDescriptor
(
PosixDescriptor
& descriptor){
135
return
dm_
->
stopMonitoringDescriptor
(descriptor);
136
}
137
};
138
139
140
}
/* onposix */
141
142
#endif
/* ABSTRACTDESCRIPTORREADER_HPP_ */
include
AbstractDescriptorReader.hpp
Generated on Wed Apr 30 2014 15:27:24 for ONPOSIX by
1.8.1.2