otsdaq  v1_01_02
 All Classes Namespaces Functions
UDPDataListenerProducer_processor.cc
1 #include "otsdaq-core/DataProcessorPlugins/UDPDataListenerProducer.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 UDPDataListenerProducer::UDPDataListenerProducer(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
17 : WorkLoop (processorUID)
18 , Socket
19 (
20  theXDAQContextConfigTree.getNode(configurationPath).getNode("HostIPAddress").getValue<std::string>(),
21  theXDAQContextConfigTree.getNode(configurationPath).getNode("HostPort").getValue<unsigned int>()
22 )
23 //, Socket ("192.168.133.100", 40000)
25 (
26  supervisorApplicationUID,
27  bufferUID,
28  processorUID,
29  theXDAQContextConfigTree.getNode(configurationPath).getNode("BufferSize").getValue<unsigned int>()
30 )
31 //, DataProducer (supervisorApplicationUID, bufferUID, processorUID, 100)
32 , Configurable (theXDAQContextConfigTree, configurationPath)
33 , dataP_ (nullptr)
34 , headerP_ (nullptr)
35 {
36  Socket::initialize();
37 }
38 
39 //========================================================================================================================
40 UDPDataListenerProducer::~UDPDataListenerProducer(void)
41 {}
42 
43 //========================================================================================================================
44 bool UDPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
45 //bool UDPDataListenerProducer::getNextFragment(void)
46 {
47  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_ << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
48  fastWrite();
49  return WorkLoop::continueWorkLoop_;
50 }
51 
52 //========================================================================================================================
53 void UDPDataListenerProducer::slowWrite(void)
54 {
55  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
56 
57  if(ReceiverSocket::receive(data_, ipAddress_, port_) != -1)
58  {
59  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " Buffer: " << message << std::endl;
60  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " -> Got some data. From: " << std::hex << fromIPAddress << " port: " << fromPort << std::dec << std::endl;
61  header_["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
62  header_["Port"] = NetworkConverters::networkToStringPort(port_);
63  // unsigned long long value;
64  // memcpy((void *)&value, (void *) data_.substr(2).data(),8); //make data counter
65  // std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << std::hex << value << std::dec << std::endl;
66 
67  while(DataProducer::write(data_, header_) < 0)
68  {
69  __MOUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
70  usleep(10000);
71  return;
72  }
73  }
74 }
75 
76 //========================================================================================================================
77 void UDPDataListenerProducer::fastWrite(void)
78 {
79  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
80 
81  if(DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
82  {
83  __MOUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
84  usleep(10000);
85  return;
86  }
87 
88  if(ReceiverSocket::receive(*dataP_, ipAddress_, port_) != -1)
89  {
90  (*headerP_)["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
91  (*headerP_)["Port"] = NetworkConverters::networkToStringPort(port_);
92  //unsigned long long value;
93  //memcpy((void *)&value, (void *) dataP_->substr(2).data(),8); //make data counter
94  //__MOUT__ << "Got data: " << dataP_->length()
95  // << std::hex << value << std::dec
96  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_)
97  // << std::endl;
98  //if( NetworkConverters::networkToUnsignedPort(port_) == 2001)
99  //{
100  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
101  // *dataP_ = dataP_->substr(2);
102  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
103  // unsigned int oldValue32;
104  // memcpy((void *)&oldValue32, (void *) dataP_->data(),4);
105  // unsigned int value32;
106  // for(unsigned int i=8; i<dataP_->size(); i+=8)
107  // {
108  // memcpy((void *)&value32, (void *) dataP_->substr(i).data(),4); //make data counter
109  // if(value32 == oldValue32)
110  // std::cout << __COUT_HDR_FL__ << "Trimming! i=" << i
111  // << std::hex << " v: " << value32 << std::dec
112  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_) << std::endl;
113  //
114  // oldValue32 = value32;
115  // }
116  //}
117  DataProducer::setWrittenSubBuffer<std::string,std::map<std::string,std::string>>();
118  }
119 }
120 
121 DEFINE_OTS_PROCESSOR(UDPDataListenerProducer)