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