Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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"
11#include "JNet/JSelectReader.hh"
12
13
14/**
15 * \author mdejong
16 */
17
18namespace JNET {}
19namespace JPP { using namespace JNET; }
20
21namespace 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,
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(this->getFileDescriptor());
136
137 //socket.setSendBufferSize (buffer_size);
139
140 socket.setKeepAlive (true);
141 socket.setNonBlocking(true);
142
143 this->push_back(JSocketInputChannel_t(socket));
144 }
145 }
146 }
147
148 return false;
149 }
150
151 private:
155 };
156}
157
158#endif
int getFileDescriptor() const
Get file descriptor.
Object iteration through ControlHost server.
JLigierObjectIterator(const int port, const int backlog, const int buffer_size=262144, const int timeout_us=10)
Constructor.
virtual bool setObject(T &object) override
Set object.
ControlHost prefix.
Definition JPrefix.hh:33
int getSize() const
Get size.
Definition JPrefix.hh:62
Wrapper class for select call.
void setReaderMask(const JAbstractFile &file)
Set reader mask.
bool hasReaderMask(const JAbstractFile &file) const
Has reader file.
TCP server socket.
Socket input channel.
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition JSocket.hh:148
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition JSocket.hh:104
TCP socket.
Definition JTCPSocket.hh:30
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition JTCPSocket.hh:56
int getSizeOfPacket(const KM3NETDAQ::JDAQAbstractPreamble &preamble)
Get size of packeet.
JSocketInputChannel< JPrefix_t > JSocketInputChannel_t
Definition JLigier.cc:30
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).