Jpp
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 prefix prefix
42  * \param size number of bytes
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
JException.hh
JNET::JSocketChannel::reset
void reset()
Reset channel.
Definition: JSocketChannel.hh:60
JIO::JByteArrayWriter::write
virtual int write(const char *buffer, const int length)
Write byte array.
Definition: JByteArrayIO.hh:226
JIO::JByteArrayWriter::seekp
void seekp(const int pos)
Set write position.
Definition: JByteArrayIO.hh:213
JNET::JSocketChannel::IO_DATA
Definition: JSocketChannel.hh:54
JNET::JSocketOutputChannel
Socket output channel.
Definition: JSocketChannel.hh:193
JNET::JSocketBuffer::set
void set(const JSocketBuffer< JElement_t > &buffer)
Initialise buffer.
Definition: JSocketNonblocking.hh:66
JNET::JSocketStatus::isReset
bool isReset() const
Check reset status.
Definition: JSocketStatus.hh:54
JNET::JPrefix
ControlHost prefix.
Definition: JPrefix.hh:31
JNET::JSocketChannel::IO_PREFIX
Definition: JSocketChannel.hh:54
JNET::setSizeOfPacket
void setSizeOfPacket(const int size, JPrefix_t &prefix)
Set total size of internet packet.
Definition: JLigier.cc:51
JNET::JSocketInputChannel::prefix
JPrefix_t prefix
Definition: JSocketChannel.hh:171
std::vector< char >
JNET::JSocketStatus::isReady
bool isReady() const
Check ready status.
Definition: JSocketStatus.hh:76
JNET::JSocketInputChannel::reset
void reset()
Reset channel.
Definition: JSocketChannel.hh:162
JSocketNonblocking.hh
JNET::JSocket
Socket class.
Definition: JSocket.hh:42
JNET
Interprocess communication.
Definition: JDataFilter.cc:67
JNET::JSocketNonblockingWriter::write
JStatus_t write()
Continuation of non-blocking write method.
Definition: JSocketNonblocking.hh:190
JIO::JByteArrayReader
Byte array binary input.
Definition: JByteArrayIO.hh:25
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JNET::JSocketChannel::type
JType type
Definition: JSocketChannel.hh:67
JNET::JSocketOutputChannel::reset
void reset()
Reset channel.
Definition: JSocketChannel.hh:255
JNET::JSocketStatus::isBusy
bool isBusy() const
Check busy status.
Definition: JSocketStatus.hh:65
JNET::JSocketInputChannel::JSocketInputChannel
JSocketInputChannel(JSocket &socket)
Constructor.
Definition: JSocketChannel.hh:102
JIO::JByteArrayWriter::tellp
int tellp() const
Get write position.
Definition: JByteArrayIO.hh:202
JNET::JSocketNonblockingReader::read
JStatus_t read()
Continuation of non-blocking read method.
Definition: JSocketNonblocking.hh:139
JNET::JSocketOutputChannel::prefix
JPrefix_t prefix
Definition: JSocketChannel.hh:267
JNET::JSocketStatus::JStatus_t
JStatus_t
Definition: JSocketStatus.hh:26
JNET::JSocketBuffer::reset
void reset()
Reset.
Definition: JSocketNonblocking.hh:94
JNET::JSocketInputChannel::read
JStatus_t read()
Interruptable read method.
Definition: JSocketChannel.hh:116
JNET::JSocketOutputChannel::JSocketOutputChannel
JSocketOutputChannel(JSocket &socket)
Constructor.
Definition: JSocketChannel.hh:208
JNET::JSocketChannel::JType
JType
Definition: JSocketChannel.hh:54
JNET::JSocketInputChannel::buffer
std::vector< char > buffer
Definition: JSocketChannel.hh:174
JByteArrayIO.hh
JNET::JSocketNonblockingReader
Non-blocking socket reader.
Definition: JSocketNonblocking.hh:118
JNET::JSocketOutputChannel::write
JStatus_t write()
Interruptable write method.
Definition: JSocketChannel.hh:222
JIO::JByteArrayReader::JByteArrayReader
JByteArrayReader()
Default constructor.
Definition: JByteArrayIO.hh:32
JNET::JSocketChannel
Auxiliary class for socket channel.
Definition: JSocketChannel.hh:51
JLANG::JSocketChannelException
Exception for socket channel.
Definition: JException.hh:468
JNET::JSocketStatus::getStatus
JStatus_t getStatus() const
Get status of I/O.
Definition: JSocketStatus.hh:43
JIO::JByteArrayReader::read
virtual int read(char *buffer, const int length)
Read byte array.
Definition: JByteArrayIO.hh:136
JNET::JPrefix_t
JPrefix JPrefix_t
Definition: JLigier.cc:29
JNET::JSocketNonblockingWriter
Non-blocking socket writer.
Definition: JSocketNonblocking.hh:169
JNET::getSizeOfPacket
int getSizeOfPacket(const KM3NETDAQ::JDAQAbstractPreamble &preamble)
Get size of packeet.
Definition: JDataFilter.cc:76
JNET::JSocketInputChannel
Socket input channel.
Definition: JSocketChannel.hh:86
JIO::JByteArrayWriter
Byte array binary output.
Definition: JByteArrayIO.hh:157