Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFileDescriptorMask.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JFILEDESCRIPTORMASK__
2 #define __JLANG__JFILEDESCRIPTORMASK__
3 
4 #include <sys/select.h>
5 
6 #include "JLang/JAbstractFile.hh"
7 #include "JLang/JTimeval.hh"
8 #include "JLang/JException.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JLANG {}
16 namespace JPP { using namespace JLANG; }
17 
18 namespace JLANG {
19 
20 
21  /**
22  * Auxiliary class for method select.
23  * This class encapsulates the <tt>fd_set</tT> data structure.
24  */
26  protected fd_set
27  {
28  public:
29 
30 
31  static const int MAXIMUM_FILE_DESCRIPTOR = FD_SETSIZE;
32 
33 
34  /**
35  * Default constructor.
36  */
40  {
41  FD_ZERO(get());
42  }
43 
44 
45  /**
46  * Constructor.
47  *
48  * \param file file descriptor
49  */
53  {
54  FD_ZERO(get());
55 
56  set(file);
57  }
58 
59 
60  /**
61  * Constructor.
62  *
63  * \param file_descriptor file descriptor
64  */
65  JFileDescriptorMask(const int file_descriptor) :
68  {
69  FD_ZERO(get());
70 
71  set(file_descriptor);
72  }
73 
74 
75  /**
76  * Get pointer to mask.
77  *
78  * \return pointer to mask
79  */
80  const fd_set* get() const
81  {
82  return static_cast<const fd_set*>(this);
83  }
84 
85 
86  /**
87  * Get pointer to mask.
88  *
89  * \return pointer to mask
90  */
91  fd_set* get()
92  {
93  return static_cast<fd_set*>(this);
94  }
95 
96 
97  /**
98  * Address of operator.
99  *
100  * \return pointer to mask
101  */
102  const fd_set* operator &() const
103  {
104  return get();
105  }
106 
107 
108  /**
109  * Address of operator.
110  *
111  * \return pointer to mask
112  */
113  fd_set* operator &()
114  {
115  return get();
116  }
117 
118 
119  /**
120  * Reset mask.
121  * A hard reset causes the reset of the complete mask whereas
122  * a soft reset causes the reset of the internal parameters only.
123  *
124  * \param option true hard reset; else soft reset
125  */
126  void reset(const bool option = true)
127  {
128  if (option) {
129 
130  switch (number_of_file_descriptors) {
131 
132  case 0:
133  break;
134 
135  case 1:
136  FD_CLR(maximum_file_descriptor, get());
137  break;
138 
139  default:
140  FD_ZERO(get());
141  break;
142  }
143  }
144 
147  }
148 
149 
150  /**
151  * Set file descriptor.
152  *
153  * \param file_descriptor file descriptor
154  */
155  void set(const int file_descriptor)
156  {
157  if (!has(file_descriptor)) {
158 
159  FD_SET(file_descriptor, get());
160 
161  if (file_descriptor > maximum_file_descriptor) {
162  maximum_file_descriptor = file_descriptor;
163  }
164 
166  }
167  }
168 
169 
170  /**
171  * Set file.
172  *
173  * \param file file
174  */
175  void set(const JAbstractFile& file)
176  {
177  set(file.getFileDescriptor());
178  }
179 
180 
181  /**
182  * Reset file descriptor.
183  *
184  * \param file_descriptor file descriptor
185  */
186  void reset(const int file_descriptor)
187  {
188  if (has(file_descriptor)) {
189 
190  FD_CLR(file_descriptor, get());
191 
194  }
195 
197  }
198  }
199 
200 
201  /**
202  * Reset file.
203  *
204  * \param file file
205  */
206  void reset(const JAbstractFile& file)
207  {
208  reset(file.getFileDescriptor());
209  }
210 
211 
212  /**
213  * Has file descriptor.
214  *
215  * \param file_descriptor file descriptor
216  * \return true if file descriptor set; else false
217  */
218  bool has(const int file_descriptor) const
219  {
220  return FD_ISSET(file_descriptor, get());
221  }
222 
223 
224  /**
225  * Has file.
226  *
227  * \param file file
228  * \return true if file set; else false
229  */
230  bool has(const JAbstractFile& file) const
231  {
232  return has(file.getFileDescriptor());
233  }
234 
235 
236  /**
237  * Get number of file descriptors.
238  *
239  * \return number of file descriptors
240  */
242  {
244  }
245 
246 
247  /**
248  * Check setting of file descriptors.
249  *
250  * \return true if no file descriptors are set; else false
251  */
252  bool empty() const
253  {
254  return number_of_file_descriptors == 0;
255  }
256 
257 
258  /**
259  * Check availability of input.
260  * This method returns true if at least one byte can be read without blocking.
261  * Following the <tt>select</tt> call, this method overwrites the internal mask!
262  *
263  * \param timeout timeout
264  * \return true if ready to read; else false
265  */
266  bool in_avail(JTimeval timeout = JTimeval::min())
267  {
268  const int nfds = ::select(getNumberOfFileDescriptors() + 1, get(), NULL, NULL, &timeout);
269 
270  if (nfds > 0)
271  return true;
272  else if (nfds == 0)
273  return false;
274  else
275  THROW(JSocketException, "select error " << errno);
276  }
277 
278 
279  /**
280  * Check availability of output.
281  * This method returns true if at least one byte can be written without blocking.
282  * Following the <tt>select</tt> call, this method overwrites the internal mask!
283  *
284  * \param timeout timeout
285  * \return true if ready to write; else false
286  */
287  bool out_avail(JTimeval timeout = JTimeval::min())
288  {
289  const int nfds = ::select(getNumberOfFileDescriptors() + 1, NULL, get(), NULL, &timeout) > 0;
290 
291  if (nfds > 0)
292  return true;
293  else if (nfds == 0)
294  return false;
295  else
296  THROW(JSocketException, "select error " << errno);
297  }
298 
299 
300  private:
303  };
304 }
305 
306 #endif
Exceptions.
bool select(const Trk &trk, const Evt &evt)
Event selection.
bool has(const JAbstractFile &file) const
Has file.
bool empty() const
Check setting of file descriptors.
then usage $script[< detector identifier >< run range >]< QA/QCfile > nExample script to produce data quality plots nWhen a detector identifier and run range are data are downloaded from the database nand subsequently stored in the given QA QC file
Definition: JDataQuality.sh:19
bool has(const int file_descriptor) const
Has file descriptor.
void set(const int file_descriptor)
Set file descriptor.
static JTimeval min()
Get minimal time value.
Definition: JTimeval.hh:119
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
void reset(const int file_descriptor)
Reset file descriptor.
static const int MAXIMUM_FILE_DESCRIPTOR
Auxiliary class for time values.
Definition: JTimeval.hh:26
int getFileDescriptor() const
Get file descriptor.
void reset(const JAbstractFile &file)
Reset file.
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
const fd_set * operator&() const
Address of operator.
void reset(const bool option=true)
Reset mask.
Auxiliary class for method select.
JFileDescriptorMask(const int file_descriptor)
Constructor.
The JAbstractFile class encapsulates the c-style file descriptor.
int getNumberOfFileDescriptors() const
Get number of file descriptors.
JFileDescriptorMask()
Default constructor.
Exception for socket.
Definition: JException.hh:466
JFileDescriptorMask(const JAbstractFile &file)
Constructor.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
void set(const JAbstractFile &file)
Set file.