Jpp  15.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSelectReader.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSELECTREADER__
2 #define __JNET__JSELECTREADER__
3 
4 #include <sys/select.h>
5 #include <limits>
6 #include <errno.h>
7 
8 #include "JLang/JException.hh"
10 #include "JLang/JAbstractFile.hh"
11 #include "JLang/JTimeval.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JNET {}
19 namespace JPP { using namespace JNET; }
20 
21 namespace JNET {
22 
26  using JLANG::JTimeval;
27 
28 
29  /**
30  * Wrapper class for select call.
31  */
32  class JSelectReader {
33  public:
34  /**
35  * Default constructor.
36  */
38  {}
39 
40 
41  /**
42  * Get reader mask.
43  *
44  * \return mask
45  */
47  {
48  return readerMask;
49  }
50 
51 
52  /**
53  * Set reader mask.
54  *
55  * \param file file
56  */
58  {
59  readerMask.set(file);
60  }
61 
62 
63  /**
64  * Has reader file.
65  *
66  * \param file file
67  * \return true if file set; else false
68  */
69  bool hasReaderMask(const JAbstractFile& file) const
70  {
71  return readerMask.has(file);
72  }
73 
74 
75  /**
76  * Reset.
77  */
78  void reset()
79  {
80  readerMask.reset(true); // hard reset
81  }
82 
83 
84  /**
85  * Select call.
86  *
87  * \param timeout timeout
88  * \return number of valid file descriptors
89  */
90  bool operator()(JTimeval timeout)
91  {
92  return this->operator()(&timeout);
93  }
94 
95 
96  /**
97  * Select call.
98  *
99  * \param timeout timeout
100  * \return number of valid file descriptors
101  */
102  int operator()(timeval* timeout = NULL)
103  {
105 
106  nfds += 1;
107  nfds = ::select(nfds, &readerMask, NULL, NULL, timeout);
108 
109  if (nfds != 0) {
110 
111  if (nfds < 0) {
112  if (errno != EINTR) {
113  THROW(JSelectException, "JSelectReader(..) failed.");
114  }
115  }
116 
117  } else {
118 
119  readerMask.reset(false); // soft reset
120  }
121 
122  return nfds;
123  }
124 
125 
126  protected:
128  };
129 }
130 
131 #endif
Exceptions.
int operator()(timeval *timeout=NULL)
Select call.
bool operator()(JTimeval timeout)
Select call.
bool has(const int file_descriptor) const
Has file descriptor.
void set(const int file_descriptor)
Set file descriptor.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JFileDescriptorMask & getReaderMask()
Get reader mask.
Exception for select call.
Definition: JException.hh:414
Auxiliary class for time values.
Definition: JTimeval.hh:26
void setReaderMask(const JAbstractFile &file)
Set reader mask.
void reset(const bool option=true)
Reset mask.
Auxiliary class for method select.
Wrapper class for select call.
The JAbstractFile class encapsulates the c-style file descriptor.
int getNumberOfFileDescriptors() const
Get number of file descriptors.
void reset()
Reset.
JFileDescriptorMask readerMask
JSelectReader()
Default constructor.
bool hasReaderMask(const JAbstractFile &file) const
Has reader file.
then usage $script< string identifier >< detectorfile > event file(toashort file)+" "\nNote that the event files and toashort files should be one-to-one related." fi if (( $