Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSocketChannel.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSOCKETCHANNEL__
2 #define __JNET__JSOCKETCHANNEL__
3 
4 #include <vector>
5 #include <string.h>
6 
7 #include "JLang/JException.hh"
8 #include "JIO/JByteArrayIO.hh"
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JNET {}
17 namespace JPP { using namespace JNET; }
18 
19 namespace JNET {
20 
24 
25 
26  /**
27  * Get size of data, including the header.
28  * This method should be implemented for each template class for read operations.
29  *
30  * \param prefix prefix
31  * \return number of bytes
32  */
33  template<class JPrefix_t>
34  int getSizeOfPacket(const JPrefix_t& prefix);
35 
36 
37  /**
38  * Set size of data, including the header.
39  * This method should be implemented for each template class for write operations.
40  *
41  * \param size number of bytes
42  * \param prefix prefix
43  */
44  template<class JPrefix_t>
45  void setSizeOfPacket(const int size, JPrefix_t& prefix);
46 
47 
48  /**
49  * Auxiliary class for socket channel.
50  */
52  public:
53 
54  enum JType { IO_PREFIX = 1, IO_DATA = 2 };
55 
56 
57  /**
58  * Reset channel.
59  */
60  void reset()
61  {
62  type = IO_PREFIX;
63  }
64 
65 
66  protected:
68  };
69 
70 
71  /**
72  * Socket input channel.
73  *
74  * This class can be used to read a data packet from a socket without blocking and
75  * to buffer these data for subsequent user read operations.
76  *
77  * The template argument corresponds to a header that precedes any data.
78  * This header acts as a protocol specifier that is used to determine the size of the data packet.
79  * To this end, the method getSizeOfPacket() should be implemented for each header type.
80  *
81  * The reading from the socket may be non-blocking (depending on the configuration of the socket).
82  * The status of this object should be checked for completion of the data packet.
83  * The method reset() should be called before reading the next data packet.
84  */
85  template<class JPrefix_t>
88  public JSocketChannel,
89  public JByteArrayReader
90  {
91  public:
92 
93 
95 
96 
97  /**
98  * Constructor.
99  *
100  * \param socket socket
101  */
103  JSocketNonblockingReader(socket),
104  JSocketChannel(),
106  {
107  reset();
108  }
109 
110 
111  /**
112  * Interruptable read method.
113  *
114  * \return status
115  */
117  {
118  if (type == IO_PREFIX) {
119 
120  if (isReset()) {
121  set((char*) &prefix, sizeof(JPrefix_t));
122  }
123 
124  if (isBusy()) {
126  }
127 
128  if (isReady()) {
129 
130  buffer.clear();
131 
132  buffer.resize(getSizeOfPacket(prefix));
133 
134  memcpy(buffer.data(), &prefix, sizeof(JPrefix_t));
135 
136  set(buffer.data() + sizeof(JPrefix_t),
137  buffer.size() - sizeof(JPrefix_t));
138 
139  type = IO_DATA;
140  }
141  }
142 
143  if (type == IO_DATA) {
144 
145  if (isBusy()) {
147  }
148 
149  if (isReady()) {
150  static_cast<JByteArrayReader&>(*this) = JByteArrayReader(buffer.data(), buffer.size());
151  }
152  }
153 
154  return JSocketStatus::getStatus();
155  }
156 
157 
158  /**
159  * Reset channel.
160  * After a reset, the socket channel is ready to receive a new data packet.
161  */
162  void reset()
163  {
166 
167  buffer.clear();
168  }
169 
170 
172 
173  private:
175  };
176 
177 
178  /**
179  * Socket output channel.
180  *
181  * This class can be used to user write to an internal buffer, to format these data as
182  * a packet and to write this packet to the socket without blocking.
183  *
184  * The template argument corresponds to a header that precedes any data.
185  * This header acts as a protocol specifier that is used to determine the size of the data packet.
186  * To this end, the method setSizeOfPacket() should be implemented for each header type.
187  *
188  * The wroting to the socket may be non-blocking (depending on the configuration of the socket).
189  * The status of this object should be checked for completion of the data packet.
190  * The method reset() should be called before writing the next data packet.
191  */
192  template<class JPrefix_t>
195  public JSocketChannel,
196  public JByteArrayWriter
197  {
198  public:
199 
201 
202 
203  /**
204  * Constructor.
205  *
206  * \param socket socket
207  */
209  JSocketNonblockingWriter(socket),
210  JSocketChannel(),
212  {
213  reset();
214  }
215 
216 
217  /**
218  * Interruptable write method.
219  *
220  * \return status
221  */
223  {
224  if (type == IO_PREFIX) {
225 
226  const int length = tellp();
227 
228  setSizeOfPacket(length, prefix);
229 
230  // overwrite prefix
231 
232  seekp(0);
233 
234  JByteArrayWriter::write((char*) &prefix, sizeof(JPrefix_t));
235 
236  seekp(length);
237 
238  type = IO_DATA;
239 
240  set(data(), tellp());
241  }
242 
243  if (isBusy()) {
245  }
246 
247  return JSocketStatus::getStatus();
248  }
249 
250 
251  /**
252  * Reset channel.
253  * After a reset, the socket channel is ready to receive a new data packet.
254  */
255  void reset()
256  {
259 
260  // reserve space for prefix
261 
262  resize(sizeof(JPrefix_t));
263  seekp (sizeof(JPrefix_t));
264  }
265 
266 
268  };
269 }
270 
271 #endif
ControlHost prefix.
Definition: JPrefix.hh:31
void seekp(const int pos)
Set write position.
void set(const JSocketBuffer< JElement_t > &buffer)
Initialise buffer.
int getSizeOfPacket(const KM3NETDAQ::JDAQAbstractPreamble &preamble)
Get size of packeet.
Definition: JDataFilter.cc:79
Exceptions.
Exception for socket channel.
Definition: JException.hh:486
JStatus_t write()
Interruptable write method.
Non-blocking socket reader.
Auxiliary class for socket channel.
JPrefix JPrefix_t
Definition: JLigier.cc:29
void reset()
Reset channel.
JStatus_t getStatus() const
Get status of I/O.
Non-blocking socket writer.
Byte array binary input.
Definition: JByteArrayIO.hh:25
bool isReady() const
Check ready status.
virtual int write(const char *buffer, const int length) override
Write byte array.
JStatus_t write()
Continuation of non-blocking write method.
void reset()
Reset channel.
Socket input channel.
JStatus_t read()
Continuation of non-blocking read method.
bool isReset() const
Check reset status.
JSocketInputChannel(JTCPSocket &socket)
Constructor.
TCP socket.
Definition: JTCPSocket.hh:25
std::vector< char > buffer
Socket output channel.
void setSizeOfPacket(const int size, JPrefix_t &prefix)
Set total size of internet packet.
Definition: JLigier.cc:51
Byte array binary output.
JSocketOutputChannel(JTCPSocket &socket)
Constructor.
JByteArrayReader()
Default constructor.
Definition: JByteArrayIO.hh:32
JStatus_t read()
Interruptable read method.
bool isBusy() const
Check busy status.
int tellp() const
Get write position.
virtual int read(char *buffer, const int length) override
Read byte array.
void reset()
Reset channel.