otsdaq  v2_00_00
TCPDataListenerProducer_processor.cc
1 #include "otsdaq-core/DataProcessorPlugins/TCPDataListenerProducer.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 #include "otsdaq-core/NetworkUtilities/NetworkConverters.h"
5 #include "otsdaq-core/Macros/ProcessorPluginMacros.h"
6 
7 #include <iostream>
8 #include <cassert>
9 #include <string.h>
10 #include <unistd.h>
11 
12 using namespace ots;
13 
14 
15 //========================================================================================================================
16 TCPDataListenerProducer::TCPDataListenerProducer(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID,
17  const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
18  : WorkLoop(processorUID)
19  //, Socket ("192.168.133.100", 40000)
20  , DataProducer
21  (
22  supervisorApplicationUID,
23  bufferUID,
24  processorUID,
25  theXDAQContextConfigTree.getNode(configurationPath).getNode("BufferSize").getValue<unsigned int>()
26  )
27  //, DataProducer (supervisorApplicationUID, bufferUID, processorUID, 100)
28  , Configurable(theXDAQContextConfigTree, configurationPath)
29  , TCPSocket
30  (
31  theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>(),
32  theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>(),
33  theXDAQContextConfigTree.getNode(configurationPath).getNode("SocketReceiveBufferSize").getValue<unsigned int>()
34  )
35  , dataP_(nullptr)
36  , headerP_(nullptr)
37  , ipAddress_(theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>())
38  , port_(theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>())
39 {
40 }
41 
42 //========================================================================================================================
43 TCPDataListenerProducer::~TCPDataListenerProducer(void)
44 {}
45 
46 //========================================================================================================================
47 bool TCPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
48 //bool TCPDataListenerProducer::getNextFragment(void)
49 {
50  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_ << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
51  fastWrite();
52  return WorkLoop::continueWorkLoop_;
53 }
54 
55 //========================================================================================================================
56 void TCPDataListenerProducer::slowWrite(void)
57 {
58  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
59 
60  if (TCPSocket::receive(data_) != -1)
61  {
62  header_["IPAddress"] = ipAddress_;
63  header_["Port"] = std::to_string(port_);
64 
65  while (DataProducer::write(data_, header_) < 0)
66  {
67  __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
68  usleep(10000);
69  return;
70  }
71  }
72 }
73 
74 //========================================================================================================================
75 void TCPDataListenerProducer::fastWrite(void)
76 {
77  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
78 
79  if (DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
80  {
81  __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
82  usleep(10000);
83  return;
84  }
85 
86  if (TCPSocket::receive(*dataP_) != -1)
87  {
88  (*headerP_)["IPAddress"] = ipAddress_;
89  (*headerP_)["Port"] = std::to_string(port_);
90 
91  //if (port_ == 40005)
92  //{
93  // __COUT__ << "Got data: " << dataP_->length() << std::endl;
94  //}
95 
96  DataProducer::setWrittenSubBuffer<std::string, std::map<std::string, std::string>>();
97  }
98 }
99 
100 DEFINE_OTS_PROCESSOR(TCPDataListenerProducer)