otsdaq  v2_04_02
TCPDataListenerProducer_processor.cc
1 #include "otsdaq/DataProcessorPlugins/TCPDataListenerProducer.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/Macros/ProcessorPluginMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
5 #include "otsdaq/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  , TCPSubscribeClient(theXDAQContextConfigTree.getNode(configurationPath)
32  .getNode("HostIPAddress") // THIS IS ACTUALLY THE SERVER IP
33  .getValue<std::string>(),
34  theXDAQContextConfigTree.getNode(configurationPath)
35  .getNode("HostPort") // THIS IS ACTUALLY THE SERVER PORT
36  .getValue<unsigned int>())
37  , dataP_(nullptr)
38  , headerP_(nullptr)
39  , ipAddress_(theXDAQContextConfigTree.getNode(configurationPath)
40  .getNode("HostIPAddress")
41  .getValue<std::string>())
42  , port_(theXDAQContextConfigTree.getNode(configurationPath)
43  .getNode("HostPort")
44  .getValue<unsigned int>())
45 {
46 }
47 
48 //========================================================================================================================
49 TCPDataListenerProducer::~TCPDataListenerProducer(void) {}
50 
51 //========================================================================================================================
52 void TCPDataListenerProducer::startProcessingData(std::string runNumber)
53 {
54  TCPSubscribeClient::connect();
55  DataProducer::startProcessingData(runNumber);
56 }
57 
58 //========================================================================================================================
59 void TCPDataListenerProducer::stopProcessingData(void)
60 {
61  TCPSubscribeClient::close();
62  DataProducer::stopProcessingData();
63 }
64 
65 //========================================================================================================================
66 bool TCPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
67 // bool TCPDataListenerProducer::getNextFragment(void)
68 {
69  // std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_
70  // << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
71  fastWrite();
72  return WorkLoop::continueWorkLoop_;
73 }
74 
75 //========================================================================================================================
76 void TCPDataListenerProducer::slowWrite(void)
77 {
78  // std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" <<
79  // std::endl;
80 
81  data_ =
82  TCPSubscribeClient::receive<std::string>(); // Throws an exception if it fails
83  header_["IPAddress"] = ipAddress_;
84  header_["Port"] = std::to_string(port_);
85 
86  while(DataProducer::write(data_, header_) < 0)
87  {
88  __COUT__ << "There are no available buffers! Retrying...after waiting 10 "
89  "milliseconds!"
90  << std::endl;
91  usleep(10000);
92  return;
93  }
94 }
95 
96 //========================================================================================================================
97 void TCPDataListenerProducer::fastWrite(void)
98 {
99  // std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" <<
100  // std::endl;
101 
102  if(DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
103  {
104  __COUT__
105  << "There are no available buffers! Retrying...after waiting 10 milliseconds!"
106  << std::endl;
107  usleep(10000);
108  return;
109  }
110 
111  // IT IS A BLOCKING CALL! SO NEEDS TO BE CHANGED
112  *dataP_ =
113  TCPSubscribeClient::receive<std::string>(); // Throws an exception if it fails
114  (*headerP_)["IPAddress"] = ipAddress_;
115  (*headerP_)["Port"] = std::to_string(port_);
116 
117  // if (port_ == 40005)
118  //{
119  // __COUT__ << "Got data: " << dataP_->length() << std::endl;
120  //}
121 
122  DataProducer::setWrittenSubBuffer<std::string, std::map<std::string, std::string>>();
123 }
124 
125 DEFINE_OTS_PROCESSOR(TCPDataListenerProducer)