00001 #include "otsdaq-core/DataProcessorPlugins/TCPDataListenerProducer.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutMacros.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 TCPDataListenerProducer::TCPDataListenerProducer(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID,
00017 const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
00018 : WorkLoop(processorUID)
00019
00020 , DataProducer
00021 (
00022 supervisorApplicationUID,
00023 bufferUID,
00024 processorUID,
00025 theXDAQContextConfigTree.getNode(configurationPath).getNode("BufferSize").getValue<unsigned int>()
00026 )
00027
00028 , Configurable(theXDAQContextConfigTree, configurationPath)
00029 , TCPSocket
00030 (
00031 theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>(),
00032 theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>(),
00033 theXDAQContextConfigTree.getNode(configurationPath).getNode("SocketReceiveBufferSize").getValue<unsigned int>()
00034 )
00035 , dataP_(nullptr)
00036 , headerP_(nullptr)
00037 , ipAddress_(theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>())
00038 , port_(theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>())
00039 {
00040 }
00041
00042
00043 TCPDataListenerProducer::~TCPDataListenerProducer(void)
00044 {}
00045
00046
00047 bool TCPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
00048
00049 {
00050
00051 fastWrite();
00052 return WorkLoop::continueWorkLoop_;
00053 }
00054
00055
00056 void TCPDataListenerProducer::slowWrite(void)
00057 {
00058
00059
00060 if (TCPSocket::receive(data_) != -1)
00061 {
00062 header_["IPAddress"] = ipAddress_;
00063 header_["Port"] = std::to_string(port_);
00064
00065 while (DataProducer::write(data_, header_) < 0)
00066 {
00067 __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00068 usleep(10000);
00069 return;
00070 }
00071 }
00072 }
00073
00074
00075 void TCPDataListenerProducer::fastWrite(void)
00076 {
00077
00078
00079 if (DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
00080 {
00081 __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
00082 usleep(10000);
00083 return;
00084 }
00085
00086 if (TCPSocket::receive(*dataP_) != -1)
00087 {
00088 (*headerP_)["IPAddress"] = ipAddress_;
00089 (*headerP_)["Port"] = std::to_string(port_);
00090
00091
00092
00093
00094
00095
00096 DataProducer::setWrittenSubBuffer<std::string, std::map<std::string, std::string>>();
00097 }
00098 }
00099
00100 DEFINE_OTS_PROCESSOR(TCPDataListenerProducer)