1 #include "otsdaq-components/DAQHardware/OtsUDPHardware.h"
4 #include "otsdaq/Macros/CoutMacros.h"
5 #include "otsdaq/MessageFacility/MessageFacility.h"
11 OtsUDPHardware::OtsUDPHardware(std::string boardIPAddress,
12 unsigned int boardPort,
17 , OtsUDPBoard_(boardIPAddress, boardPort)
31 OtsUDPHardware::OtsUDPHardware(std::string hostIPAddress,
32 unsigned int hostPort,
33 std::string OtsUDPHardwareIPAddress,
34 unsigned int OtsUDPHardwarePort,
37 : Socket(hostIPAddress, hostPort)
39 , OtsUDPBoard_(OtsUDPHardwareIPAddress, OtsUDPHardwarePort)
44 __COUT__ <<
"Socket for hardware is initialized at IP:Port "
45 << OtsUDPBoard_.getIPAddress() <<
":" << OtsUDPBoard_.getPort() << __E__;
56 OtsUDPHardware::~OtsUDPHardware(
void) {}
86 if(TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_) < 0)
88 __SS__ <<
"Write failed to hardware at IP:Port " << OtsUDPBoard_.getIPAddress()
89 <<
":" << OtsUDPBoard_.getPort() << __E__;
93 catch(std::runtime_error& e)
99 __SS__ <<
"Unrecognized exception caught!" << std::endl;
101 catch(
const std::exception &e)
103 ss <<
"Exception message: " << e.what();
112 for(
const auto& it : sendBuffer)
142 void OtsUDPHardware::writeAndAcknowledge(
const std::string& buffer,
int timeoutSeconds)
173 TransceiverSocket::send(OtsUDPBoard_, buffer, verbose_);
176 if(timeoutSeconds < 0)
179 if(TransceiverSocket::receive(acknowledgment_, timeoutSeconds) < 0)
181 __SS__ <<
"writeAndAcknowledge failed from hardware at IP:Port "
182 << OtsUDPBoard_.getIPAddress() <<
":" << OtsUDPBoard_.getPort()
183 <<
". Timeout period of " << timeoutSeconds
184 <<
" seconds reached without response." << std::endl;
185 __COUT_ERR__ <<
"\n" << ss.str() << std::endl;
206 if((acknowledgment_[0] >> 4))
208 __SS__ <<
"Error in OTS protocol encountered! " << std::setfill(
'0')
209 << std::setw(2) << std::hex << (((int16_t)acknowledgment_[0]) & 0xff)
210 <<
"-" << std::dec << __E__;
214 catch(std::runtime_error& e)
220 __SS__ <<
"Unrecognized exception caught!" << std::endl;
222 catch(
const std::exception &e)
224 ss <<
"Exception message: " << e.what();
227 __COUT_ERR__ <<
"\n" << ss.str() << std::endl;
232 void OtsUDPHardware::writeAndAcknowledge(
const std::vector<std::string>& buffer,
235 for(
const auto& it : buffer)
260 writeAndAcknowledge(it);
267 std::string& receiveBuffer,
274 __COUT__ <<
"Cleared receive socket buffer: " << clearedPackets
275 <<
" packets cleared." << std::endl;
278 __COUT__ <<
"Sending" << std::endl;
279 TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_);
281 if(timeoutSeconds < 0)
284 __COUT__ <<
"Receiving" << std::endl;
285 if(TransceiverSocket::receive(
286 receiveBuffer, timeoutSeconds, 0 , verbose_) < 0)
288 __SS__ <<
"Read failed from hardware at IP : Port " << OtsUDPBoard_.getIPAddress()
289 <<
" : " << OtsUDPBoard_.getPort() <<
". Timeout period of "
290 << timeoutSeconds <<
" seconds reached without response." << __E__;
308 if((receiveBuffer[0] >> 5))
310 __SS__ <<
"Error in OTS protocol encountered! " << std::setfill(
'0')
311 << std::setw(2) << std::hex << (((int16_t)receiveBuffer[0]) & 0xff) <<
"-"
312 << std::dec << __E__;
316 catch(std::runtime_error& e)
322 __SS__ <<
"Unrecognized exception caught!" << std::endl;
324 catch(
const std::exception &e)
326 ss <<
"Exception message: " << e.what();
334 std::vector<std::string>& receiveBuffers,
337 receiveBuffers.resize(sendBuffers.size());
338 auto receiveBufferIterator = receiveBuffers.begin();
339 for(
const auto& sendBuffer : sendBuffers)
340 read(sendBuffer, *(receiveBufferIterator++));
345 uint64_t& receiveQuadWord,
348 std::string receiveBuffer;
349 read(sendBuffer, receiveBuffer);
352 if(receiveBuffer.length() != 10)
354 __SS__ <<
"Read uint64_t quad-word failed. Invalid size of received buffer: "
355 << receiveBuffer.length() <<
" != " << 10 << std::endl;
364 receiveQuadWord = *((uint64_t*)&receiveBuffer[2]);
372 std::vector<uint64_t>& receiveQuadWords,
375 receiveQuadWords.resize(0);
377 std::string receiveBuffer;
378 read(sendBuffer, receiveBuffer);
381 if((receiveBuffer.length() - 2) % 8 != 0)
383 __SS__ <<
"Read vector of uint64_t quad-word failed. Invalid size of received "
385 << receiveBuffer.length() <<
" - 2) % 8 != 0" << std::endl;
389 for(
unsigned int i = 2; i < receiveBuffer.length(); i += 8)
391 receiveQuadWords.push_back(uint64_t());
396 receiveQuadWords[receiveQuadWords.size() - 1] = *((uint64_t*)&receiveBuffer[i]);
402 std::vector<std::vector<uint64_t> >& receiveQuadWordsVector,
405 receiveQuadWordsVector.resize(
408 std::string receiveBuffer;
411 for(
unsigned int b = 0; b < sendBuffers.size(); ++b)
413 receiveQuadWordsVector[b].resize(0);
415 read(sendBuffers[b], receiveBuffer);
418 if((receiveBuffer.length() - 2) % 8 != 0)
420 __SS__ <<
"Read vector of uint64_t quad-word failed. Invalid size of "
422 << receiveBuffer.length() <<
" - 2) % 8 != 0" << std::endl;
427 for(
unsigned int i = 2; i < receiveBuffer.length(); i += 8)
429 receiveQuadWordsVector[b].push_back(uint64_t());
430 std::copy_n((
char*)&receiveBuffer[i],
432 &receiveQuadWordsVector[b][receiveQuadWordsVector[b].size() - 1]);
446 std::string dummerReceiveBuffer;
452 while(TransceiverSocket::receive(dummerReceiveBuffer,
461 __COUT__ <<
"I am crashing while trying to read the socket...strange!"
virtual void read(const std::string &sendBuffer, std::string &receiveBuffer, int timeoutSeconds=-1)
return -1 on failure
virtual void write(const std::string &sendBuffer)