00001 #include "otsdaq-core/DataProcessorPlugins/UDPDataListenerProducer.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "otsdaq-core/NetworkUtilities/NetworkConverters.h"
00005 #include "otsdaq-core/Macros/ProcessorPluginMacros.h"
00006
00007 #include <iostream>
00008 #include <cassert>
00009 #include <string.h>
00010 #include <unistd.h>
00011
00012 using namespace ots;
00013
00014
00015
00016 UDPDataListenerProducer::UDPDataListenerProducer(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
00017 : WorkLoop (processorUID)
00018 , Socket
00019 (
00020 theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>(),
00021 theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>()
00022 )
00023
00024 , DataProducer
00025 (
00026 supervisorApplicationUID,
00027 bufferUID,
00028 processorUID,
00029 theXDAQContextConfigTree.getNode(configurationPath).getNode("BufferSize").getValue<unsigned int>()
00030 )
00031
00032 , Configurable (theXDAQContextConfigTree, configurationPath)
00033 , dataP_ (nullptr)
00034 , headerP_ (nullptr)
00035 {
00036 unsigned int socketReceiveBufferSize;
00037 try
00038 {
00039 socketReceiveBufferSize = theXDAQContextConfigTree.getNode(configurationPath).getNode("SocketReceiveBufferSize").getValue<unsigned int>();
00040 }
00041 catch(...)
00042 {
00043
00044 socketReceiveBufferSize = 0x10000000;
00045 }
00046
00047 Socket::initialize(socketReceiveBufferSize);
00048 }
00049
00050
00051 UDPDataListenerProducer::~UDPDataListenerProducer(void)
00052 {}
00053
00054
00055 bool UDPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
00056
00057 {
00058
00059 fastWrite();
00060 return WorkLoop::continueWorkLoop_;
00061 }
00062
00063
00064 void UDPDataListenerProducer::slowWrite(void)
00065 {
00066
00067
00068 if(ReceiverSocket::receive(data_, ipAddress_, port_) != -1)
00069 {
00070
00071
00072 header_["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
00073 header_["Port"] = NetworkConverters::networkToStringPort(port_);
00074
00075
00076
00077
00078 while(DataProducer::write(data_, header_) < 0)
00079 {
00080 __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00081 usleep(10000);
00082 return;
00083 }
00084 }
00085 }
00086
00087
00088 void UDPDataListenerProducer::fastWrite(void)
00089 {
00090
00091
00092 if(DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
00093 {
00094 __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00095 usleep(10000);
00096 return;
00097 }
00098
00099 if(ReceiverSocket::receive(*dataP_, ipAddress_, port_) != -1)
00100 {
00101 (*headerP_)["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
00102 (*headerP_)["Port"] = NetworkConverters::networkToStringPort(port_);
00103
00104
00105 if(
00106 (requestedPort_ == 47000 || requestedPort_ == 47001 || requestedPort_ == 47002) &&
00107 dataP_->length() > 2)
00108 {
00109 unsigned char seqId = (*dataP_)[1];
00110 if(!(lastSeqId_ + 1 == seqId ||
00111 (lastSeqId_ == 255 && seqId == 0)))
00112 {
00113 __COUT__ << requestedPort_ <<
00114 "?????? NOOOO Missing Packets: " <<
00115 (unsigned int)lastSeqId_ << " v " << (unsigned int)seqId << __E__;
00116 }
00117
00118
00119
00120
00121 lastSeqId_ = seqId;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 DataProducer::setWrittenSubBuffer<std::string,std::map<std::string,std::string>>();
00161 }
00162 }
00163
00164 DEFINE_OTS_PROCESSOR(UDPDataListenerProducer)