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 Socket::initialize();
00037 }
00038
00039
00040 UDPDataListenerProducer::~UDPDataListenerProducer(void)
00041 {}
00042
00043
00044 bool UDPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
00045
00046 {
00047
00048 fastWrite();
00049 return WorkLoop::continueWorkLoop_;
00050 }
00051
00052
00053 void UDPDataListenerProducer::slowWrite(void)
00054 {
00055
00056
00057 if(ReceiverSocket::receive(data_, ipAddress_, port_) != -1)
00058 {
00059
00060
00061 header_["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
00062 header_["Port"] = NetworkConverters::networkToStringPort(port_);
00063
00064
00065
00066
00067 while(DataProducer::write(data_, header_) < 0)
00068 {
00069 __MOUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00070 usleep(10000);
00071 return;
00072 }
00073 }
00074 }
00075
00076
00077 void UDPDataListenerProducer::fastWrite(void)
00078 {
00079
00080
00081 if(DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
00082 {
00083 __MOUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00084 usleep(10000);
00085 return;
00086 }
00087
00088 if(ReceiverSocket::receive(*dataP_, ipAddress_, port_) != -1)
00089 {
00090 (*headerP_)["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
00091 (*headerP_)["Port"] = NetworkConverters::networkToStringPort(port_);
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 DataProducer::setWrittenSubBuffer<std::string,std::map<std::string,std::string>>();
00118 }
00119 }
00120
00121 DEFINE_OTS_PROCESSOR(UDPDataListenerProducer)