Jpp  18.2.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 
196  }
197  }
198 
199 
200  /**
201  * Reset file.
202  *
203  * \param file file
204  */
205  void reset(const JAbstractFile& file)
206  {
207  reset(file.getFileDescriptor());
208  }
209 
210 
211  /**
212  * Has file descriptor.
213  *
214  * \param file_descriptor file descriptor
215  * \return true if file descriptor set; else false
216  */
217  bool has(const int file_descriptor) const
218  {
219  return FD_ISSET(file_descriptor, get());
220  }
221 
222 
223  /**
224  * Has file.
225  *
226  * \param file file
227  * \return true if file set; else false
228  */
229  bool has(const JAbstractFile& file) const
230  {
231  return has(file.getFileDescriptor());
232  }
233 
234 
235  /**
236  * Get number of file descriptors.
237  *
238  * \return number of file descriptors
239  */
241  {
243  }
244 
245 
246  /**
247  * Check setting of file descriptors.
248  *
249  * \return true if no file descriptors are set; else false
250  */
251  bool empty() const
252  {
253  return number_of_file_descriptors == 0;
254  }
255 
256 
257  /**
258  * Check availability of input.
259  * This method returns true if at least one byte can be read without blocking.
260  * Following the <tt>select</tt> call, this method overwrites the internal mask!
261  *
262  * \param timeout timeout
263  * \return true if ready to read; else false
264  */
265  bool in_avail(JTimeval timeout = JTimeval::min())
266  {
267  const int nfds = ::select(getNumberOfFileDescriptors() + 1, get(), NULL, NULL, &timeout);
268 
269  if (nfds > 0)
270  return true;
271  else if (nfds == 0)
272  return false;
273  else
274  THROW(JSocketException, "select error " << errno);
275  }
276 
277 
278  /**
279  * Check availability of output.
280  * This method returns true if at least one byte can be written without blocking.
281  * Following the <tt>select</tt> call, this method overwrites the internal mask!
282  *
283  * \param timeout timeout
284  * \return true if ready to write; else false
285  */
286  bool out_avail(JTimeval timeout = JTimeval::min())
287  {
288  const int nfds = ::select(getNumberOfFileDescriptors() + 1, NULL, get(), NULL, &timeout) > 0;
289 
290  if (nfds > 0)
291  return true;
292  else if (nfds == 0)
293  return false;
294  else
295  THROW(JSocketException, "select error " << errno);
296  }
297 
298 
299  private:
302  };
303 }
304 
305 #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.