Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLigierObjectIterator.hh
Go to the documentation of this file.
1 #ifndef __JNET__JLIGIEROBJECTITERATOR__
2 #define __JNET__JLIGIEROBJECTITERATOR__
3 
4 #include <vector>
5 #include <exception>
6 
8 #include "JNet/JPrefix.hh"
9 #include "JNet/JServerSocket.hh"
10 #include "JNet/JSocketChannel.hh"
11 #include "JNet/JSelectReader.hh"
12 
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JNET {}
19 namespace JPP { using namespace JNET; }
20 
21 namespace JNET {
22 
24 
26 
27 
28  /**
29  * Get total size of internet packet.
30  *
31  * \param prefix prefix
32  * \return number of bytes
33  */
34  inline int getSizeOfPacket(const JPrefix& prefix)
35  {
36  return prefix.getSize() + sizeof(JPrefix);
37  }
38 
39 
40 
41  /**
42  * Object iteration through ControlHost server.
43  * This class implements the JLANG::JObjectIterator interface.
44  */
45  template<class T>
47  public JServerSocket,
48  public JAbstractObjectIterator<T>,
49  public std::vector<JSocketInputChannel_t>
50  {
51  public:
52 
53  /**
54  * Constructor.
55  *
56  * \param port server port
57  * \param backlog server backlog
58  * \param buffer_size socket buffer size
59  * \param timeout_us timeout [us]
60  */
61  JLigierObjectIterator(const int port,
62  const int backlog,
63  const int buffer_size = 262144,
64  const int timeout_us = 10) :
65  JServerSocket(port,backlog),
68  {}
69 
70 
71  /**
72  * Set object.
73  *
74  * \param object reference to object to be set
75  * \return true if set; else false
76  */
77  virtual bool setObject(T& object) override
78  {
79  using namespace std;
80 
81  for ( ; ; ) {
82 
83  select.reset();
85 
86  for (const_iterator channel = this->begin(); channel != this->end(); ++channel) {
87  if (!channel->isReady()) {
88  select.setReaderMask(channel->getFileDescriptor());
89  }
90  }
91 
92  if (select(timeout_us) > 0) {
93 
94  for (iterator channel = this->begin(); channel != this->end(); ) {
95 
96  if (select.hasReaderMask(*channel)) {
97 
98  try {
99 
100  channel->read();
101 
102  if (channel->isReady()) {
103 
104  if (channel->prefix.getTag() == getTag<T>()) {
105 
106  channel->seekg(sizeof(JPrefix));
107 
108  *channel >> object; // read object
109 
110  channel->reset();
111 
112  return true;
113 
114  } else {
115 
116  channel->reset(); // skip data
117  }
118  }
119  }
120  catch(const exception& error) {
121 
122  channel->shutdown();
123 
124  channel = this->erase(channel);
125 
126  continue;
127  }
128  }
129 
130  ++channel;
131  }
132 
133  if (select.hasReaderMask(*this)) {
134 
135  JTCPSocket socket;
136 
137  socket.accept(this->getFileDescriptor());
138 
139  //socket.setSendBufferSize (buffer_size);
141 
142  socket.setKeepAlive (true);
143  socket.setNonBlocking(true);
144 
145  this->push_back(JSocketInputChannel_t(socket));
146  }
147  }
148  }
149 
150  return false;
151  }
152 
153  private:
157  };
158 }
159 
160 #endif
ControlHost prefix.
Definition: JPrefix.hh:31
int getSizeOfPacket(const KM3NETDAQ::JDAQAbstractPreamble &preamble)
Get size of packeet.
Definition: JDataFilter.cc:79
virtual bool setObject(T &object) override
Set object.
TCP server socket.
int getSize() const
Get size.
Definition: JPrefix.hh:62
void setReaderMask(const JAbstractFile &file)
Set reader mask.
int getFileDescriptor() const
Get file descriptor.
void accept(const int server)
Accept connection from a server.
Definition: JTCPSocket.hh:139
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Wrapper class for select call.
Socket input channel.
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition: JSocket.hh:131
void reset()
Reset.
TCP socket.
Definition: JTCPSocket.hh:25
JLigierObjectIterator(const int port, const int backlog, const int buffer_size=262144, const int timeout_us=10)
Constructor.
Object iteration through ControlHost server.
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JTCPSocket.hh:42
JSocketInputChannel< JPrefix_t > JSocketInputChannel_t
Definition: JLigier.cc:30
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition: JSocket.hh:87
bool hasReaderMask(const JAbstractFile &file) const
Has reader file.