Jpp  debug
the software that should make you happy
JSelect.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSELECT__
2 #define __JNET__JSELECT__
3 
4 #include <algorithm>
5 #include <sys/select.h>
6 #include <limits>
7 #include <errno.h>
8 
9 #include "JLang/JException.hh"
11 #include "JLang/JAbstractFile.hh"
12 #include "JLang/JTimeval.hh"
13 
14 #include "JNet/JSelectReader.hh"
15 #include "JNet/JSelectWriter.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JNET {}
23 namespace JPP { using namespace JNET; }
24 
25 namespace JNET {
26 
27  using JLANG::JTimeval;
29 
30 
31  /**
32  * Wrapper class for select call.
33  */
34  class JSelect :
35  public JSelectReader,
36  public JSelectWriter
37  {
38  public:
39  /**
40  * Default constructor.
41  */
42  JSelect() :
43  JSelectReader(),
45  {}
46 
47 
48  /**
49  * Reset.
50  */
51  void reset()
52  {
53  static_cast<JSelectReader&>(*this).reset();
54  static_cast<JSelectWriter&>(*this).reset();
55  }
56 
57 
58  /**
59  * Infinite wait select call.
60  *
61  * \return true if data can be read or written; else false
62  */
63 
64  bool operator ()()
65  {
66  return this->operator ()(0);
67  }
68 
69 
70  /**
71  * Select call.
72  *
73  * \param timeout timeout
74  * \return true if data can be read or written; else false
75  */
76  bool operator()(JTimeval timeout)
77  {
78  return this->operator ()(static_cast<JTimeval*>(static_cast<void*>(&timeout)));
79  }
80 
81 
82  /**
83  * Select call.
84  *
85  * \return number of valid file descriptors
86  */
87  int operator()(JTimeval* timeout)
88  {
89  int nfds = std::max(readerMask.getNumberOfFileDescriptors(),
91 
92  nfds += 1;
93  nfds = ::select(nfds, &readerMask, &writerMask, NULL, timeout->get());
94 
95  if (nfds != 0) {
96 
97  if (nfds < 0) {
98 
99  readerMask.reset(true); // hard reset
100  writerMask.reset(true); // hard reset
101 
102  if (errno != EINTR) {
103  THROW(JSelectException, "select(..) failed " << nfds << "; errno = " << errno);
104  }
105  }
106 
107  } else {
108 
109  readerMask.reset(false); // soft reset
110  writerMask.reset(false); // soft reset
111  }
112 
113  return nfds;
114  }
115  };
116 }
117 
118 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
void reset(const bool option=true)
Reset mask.
int getNumberOfFileDescriptors() const
Get number of file descriptors.
Exception for select call.
Definition: JException.hh:450
Auxiliary class for time values.
Definition: JTimeval.hh:29
const timeval * get() const
Get pointer to time value.
Definition: JTimeval.hh:143
Wrapper class for select call.
JFileDescriptorMask readerMask
void reset()
Reset.
Wrapper class for select call.
void reset()
Reset.
JFileDescriptorMask writerMask
Wrapper class for select call.
Definition: JSelect.hh:37
JSelect()
Default constructor.
Definition: JSelect.hh:42
bool operator()()
Infinite wait select call.
Definition: JSelect.hh:64
bool operator()(JTimeval timeout)
Select call.
Definition: JSelect.hh:76
void reset()
Reset.
Definition: JSelect.hh:51
int operator()(JTimeval *timeout)
Select call.
Definition: JSelect.hh:87
bool select(const Trk &trk, const Evt &evt)
Event selection.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).