00001 #include "otsdaq-components/DAQHardware/OtsUDPHardware.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.h"
00004 #include <iostream>
00005 #include <iomanip>
00006
00007 using namespace ots;
00008
00009
00010
00011
00012 OtsUDPHardware::OtsUDPHardware (std::string boardIPAddress, unsigned int boardPort,
00013 unsigned int version, bool verbose)
00014 :
00015 FrontEndHardwareBase (version)
00016 , OtsUDPBoard_ (boardIPAddress, boardPort)
00017 , verbose_ (verbose)
00018 {
00019 Socket::initialize();
00020
00021
00022
00023
00024
00025
00026
00027
00028 }
00029
00030
00031 OtsUDPHardware::OtsUDPHardware (std::string hostIPAddress, unsigned int hostPort,
00032 std::string OtsUDPHardwareIPAddress, unsigned int OtsUDPHardwarePort,
00033 unsigned int version, bool verbose)
00034 : Socket (hostIPAddress, hostPort)
00035 , FrontEndHardwareBase (version)
00036 , OtsUDPBoard_ (OtsUDPHardwareIPAddress, OtsUDPHardwarePort)
00037 , verbose_ (verbose)
00038 {
00039 Socket::initialize();
00040
00041
00042
00043
00044
00045
00046
00047
00048 }
00049
00050
00051 OtsUDPHardware::~OtsUDPHardware(void)
00052 {}
00053
00054
00055 void OtsUDPHardware::write(const std::string& sendBuffer)
00056 throw(std::runtime_error)
00057 try
00058 {
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 if(TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_) < 0)
00084 {
00085 __SS__ << "Write failed." << std::endl;
00086 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00087 throw std::runtime_error(ss.str());
00088 }
00089 }
00090 catch(std::runtime_error &e)
00091 {
00092 throw;
00093 }
00094 catch(...)
00095 {
00096 __SS__ << "Unrecognized exception caught!" << std::endl;
00097 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00098 throw std::runtime_error(ss.str());
00099 }
00100
00101
00102 void OtsUDPHardware::write(const std::vector<std::string>& sendBuffer)
00103 throw(std::runtime_error)
00104 {
00105 for(const auto& it : sendBuffer)
00106 {
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 write(it);
00132 }
00133 }
00134
00135
00136 void OtsUDPHardware::writeAndAcknowledge(const std::string& buffer,
00137 int timeoutSeconds)
00138 throw(std::runtime_error)
00139 try
00140 {
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 TransceiverSocket::send(OtsUDPBoard_, buffer, verbose_);
00171
00172
00173 if(timeoutSeconds < 0)
00174 {
00175 if(TransceiverSocket::receive(acknowledgment_) < 0)
00176 {
00177 __SS__ << "writeAndAcknowledge failed. Default timeout period reached without response." << std::endl;
00178 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00179 throw std::runtime_error(ss.str());
00180 }
00181 }
00182 else
00183 {
00184 if(TransceiverSocket::receive(acknowledgment_,timeoutSeconds) < 0)
00185 {
00186 __SS__ << "writeAndAcknowledge failed. " << timeoutSeconds <<
00187 " second timeout period reached without response." << std::endl;
00188 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00189 throw std::runtime_error(ss.str());
00190 }
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 if((acknowledgment_[0]>>4))
00212 {
00213 __SS__ << "Error in OTS protocol encountered! " << std::setfill('0') << std::setw(2) << std::hex <<
00214 (((int16_t) acknowledgment_[0]) &0xff) << "-" << std::dec << __E__;
00215 __SS_THROW__;
00216 }
00217
00218 }
00219 catch(std::runtime_error &e)
00220 {
00221 throw;
00222 }
00223 catch(...)
00224 {
00225 __SS__ << "Unrecognized exception caught!" << std::endl;
00226 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00227 throw std::runtime_error(ss.str());
00228 }
00229
00230
00231 void OtsUDPHardware::writeAndAcknowledge(const std::vector<std::string>& buffer,
00232 int timeoutSeconds)
00233 throw(std::runtime_error)
00234 {
00235 for(const auto& it : buffer)
00236 {
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 writeAndAcknowledge(it);
00261 }
00262 }
00263
00264
00265
00266 void OtsUDPHardware::read(const std::string& sendBuffer, std::string& receiveBuffer,
00267 int timeoutSeconds)
00268 throw(std::runtime_error)
00269 try
00270 {
00271 {
00272 int clearedPackets = OtsUDPHardware::clearReadSocket();
00273 if(clearedPackets)
00274 __COUT__ << "Cleared receive socket buffer: " << clearedPackets << " packets cleared." << std::endl;
00275 }
00276
00277 __COUT__ << "sending" << std::endl;
00278 TransceiverSocket::send(OtsUDPBoard_, sendBuffer, verbose_);
00279 __COUT__ << "receiving" << std::endl;
00280
00281 if(timeoutSeconds < 0)
00282 {
00283 if(TransceiverSocket::receive(receiveBuffer,5 ,0 ,verbose_) < 0)
00284 {
00285 __SS__ << "Read failed. Default timeout period reached without response." << std::endl;
00286 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00287 throw std::runtime_error(ss.str());
00288 }
00289 }
00290 else
00291 {
00292 if(TransceiverSocket::receive(receiveBuffer,timeoutSeconds,0 ,verbose_) < 0)
00293 {
00294 __SS__ << "Read failed. " << timeoutSeconds <<
00295 " second timeout period reached without response." << std::endl;
00296 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00297 throw std::runtime_error(ss.str());
00298 }
00299 }
00300
00301
00302 __COUT__ << "RECEIVED MESSAGE: ";
00303 for(uint32_t i=0; i<receiveBuffer.size(); i++)
00304 std::cout << std::setfill('0') << std::setw(2) << std::hex << (((int16_t) receiveBuffer[i]) &0xff) << "-" << std::dec;
00305 std::cout << std::endl;
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 if((receiveBuffer[0]>>5))
00316 {
00317 __SS__ << "Error in OTS protocol encountered! " << std::setfill('0') << std::setw(2) << std::hex <<
00318 (((int16_t) receiveBuffer[0]) &0xff) << "-" << std::dec << __E__;
00319
00320 }
00321
00322 }
00323 catch(std::runtime_error &e)
00324 {
00325 throw;
00326 }
00327 catch(...)
00328 {
00329 __SS__ << "Unrecognized exception caught!" << std::endl;
00330 __SS_THROW__;
00331 }
00332
00333
00334 void OtsUDPHardware::read(const std::vector<std::string>& sendBuffers,
00335 std::vector<std::string>& receiveBuffers,
00336 int timeoutSeconds)
00337 throw(std::runtime_error)
00338 {
00339 receiveBuffers.resize(sendBuffers.size());
00340 auto receiveBufferIterator = receiveBuffers.begin();
00341 for(const auto& sendBuffer : sendBuffers)
00342 read(sendBuffer, *(receiveBufferIterator++));
00343 }
00344
00345
00346 void OtsUDPHardware::read(const std::string& sendBuffer,
00347 uint64_t& receiveQuadWord,
00348 int timeoutSeconds)
00349 throw(std::runtime_error)
00350 {
00351 std::string receiveBuffer;
00352 read(sendBuffer,receiveBuffer);
00353
00354
00355 if(receiveBuffer.length() != 10)
00356 {
00357 __SS__ << "Read uint64_t quad-word failed. Invalid size of received buffer: " <<
00358 receiveBuffer.length() << " != " << 10 << std::endl;
00359 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00360 throw std::runtime_error(ss.str());
00361 }
00362
00363
00364
00365
00366
00367
00368 receiveQuadWord = *((uint64_t *)&receiveBuffer[2]);
00369
00370
00371
00372 }
00373
00374
00375 void OtsUDPHardware::read(const std::string& sendBuffer,
00376 std::vector<uint64_t>& receiveQuadWords,
00377 int timeoutSeconds)
00378 throw(std::runtime_error)
00379 {
00380 receiveQuadWords.resize(0);
00381
00382 std::string receiveBuffer;
00383 read(sendBuffer,receiveBuffer);
00384
00385
00386 if((receiveBuffer.length()-2) % 8 != 0)
00387 {
00388 __SS__ << "Read vector of uint64_t quad-word failed. Invalid size of received buffer: (" <<
00389 receiveBuffer.length() << " - 2) % 8 != 0" << std::endl;
00390 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00391 throw std::runtime_error(ss.str());
00392 }
00393
00394 for(unsigned int i = 2; i < receiveBuffer.length(); i += 8)
00395 {
00396 receiveQuadWords.push_back(uint64_t());
00397
00398
00399
00400 receiveQuadWords[receiveQuadWords.size()-1] = *((uint64_t *)&receiveBuffer[i]);
00401 }
00402 }
00403
00404
00405 void OtsUDPHardware::read(const std::vector<std::string>& sendBuffers,
00406 std::vector<std::vector<uint64_t> >& receiveQuadWordsVector,
00407 int timeoutSeconds)
00408 throw(std::runtime_error)
00409 {
00410 receiveQuadWordsVector.resize(sendBuffers.size());
00411
00412 std::string receiveBuffer;
00413
00414
00415 for(unsigned int b = 0; b < sendBuffers.size(); ++b)
00416 {
00417 receiveQuadWordsVector[b].resize(0);
00418
00419 read(sendBuffers[b], receiveBuffer);
00420
00421
00422 if((receiveBuffer.length()-2) % 8 != 0)
00423 {
00424 __SS__ << "Read vector of uint64_t quad-word failed. Invalid size of received buffer: (" <<
00425 receiveBuffer.length() << " - 2) % 8 != 0" << std::endl;
00426 __COUT_ERR__ << "\n" << ss.str() << std::endl;
00427 throw std::runtime_error(ss.str());
00428 }
00429
00430
00431 for(unsigned int i = 2; i < receiveBuffer.length(); i += 8)
00432 {
00433 receiveQuadWordsVector[b].push_back(uint64_t());
00434 std::copy_n((char *)&receiveBuffer[i],sizeof(uint64_t),
00435 &receiveQuadWordsVector[b][receiveQuadWordsVector[b].size()-1]);
00436 }
00437 }
00438 }
00439
00440
00441
00442
00443
00444
00445
00446
00447 int OtsUDPHardware::clearReadSocket()
00448 {
00449 std::string dummerReceiveBuffer;
00450 int cnt = 0;
00451
00452
00453 try
00454 {
00455 while(TransceiverSocket::receive(dummerReceiveBuffer,
00456 0,
00457 0,
00458 false )>= 0)
00459 ++cnt;
00460 }
00461 catch(...)
00462 {
00463
00464 }
00465 return cnt;
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475