otsdaq  v1_01_04
 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  unsigned int socketReceiveBufferSize;
37  try //if socketReceiveBufferSize is defined in configuration, use it
38  {
39  socketReceiveBufferSize = theXDAQContextConfigTree.getNode(configurationPath).getNode("SocketReceiveBufferSize").getValue<unsigned int>();
40  }
41  catch(...)
42  {
43  //for backwards compatibility, ignore
44  socketReceiveBufferSize = 0x1000000; //default to "large"
45  }
46 
47  Socket::initialize(socketReceiveBufferSize);
48 }
49 
50 //========================================================================================================================
51 UDPDataListenerProducer::~UDPDataListenerProducer(void)
52 {}
53 
54 //========================================================================================================================
55 bool UDPDataListenerProducer::workLoopThread(toolbox::task::WorkLoop* workLoop)
56 //bool UDPDataListenerProducer::getNextFragment(void)
57 {
58  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_ << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
59  fastWrite();
60  return WorkLoop::continueWorkLoop_;
61 }
62 
63 //========================================================================================================================
64 void UDPDataListenerProducer::slowWrite(void)
65 {
66  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
67 
68  if(ReceiverSocket::receive(data_, ipAddress_, port_) != -1)
69  {
70  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " Buffer: " << message << std::endl;
71  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " -> Got some data. From: " << std::hex << fromIPAddress << " port: " << fromPort << std::dec << std::endl;
72  header_["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
73  header_["Port"] = NetworkConverters::networkToStringPort(port_);
74  // unsigned long long value;
75  // memcpy((void *)&value, (void *) data_.substr(2).data(),8); //make data counter
76  // std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << std::hex << value << std::dec << std::endl;
77 
78  while(DataProducer::write(data_, header_) < 0)
79  {
80  __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
81  usleep(10000);
82  return;
83  }
84  }
85 }
86 
87 //========================================================================================================================
88 void UDPDataListenerProducer::fastWrite(void)
89 {
90  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << name_ << " running!" << std::endl;
91 
92  if(DataProducer::attachToEmptySubBuffer(dataP_, headerP_) < 0)
93  {
94  __COUT__ << "There are no available buffers! Retrying...after waiting 10 milliseconds!" << std::endl;
95  usleep(10000);
96  return;
97  }
98 
99  if(ReceiverSocket::receive(*dataP_, ipAddress_, port_) != -1)
100  {
101  (*headerP_)["IPAddress"] = NetworkConverters::networkToStringIP (ipAddress_);
102  (*headerP_)["Port"] = NetworkConverters::networkToStringPort(port_);
103 
104  if( NetworkConverters::networkToUnsignedPort(port_) == 40005 )
105  {
106  __COUT__ << "Got data: " << dataP_->length() << std::endl;
107  }
108 
109 // char str[5];
110 // for(unsigned int j=0;j<dataP_->length();++j)
111 // {
112 // sprintf(str,"%2.2x",((unsigned int)(*dataP_)[j]) & ((unsigned int)(0x0FF)));
113 //
114 // if(j%64 == 0) std::cout << "RECV 0x\t";
115 // std::cout << str;
116 // if(j%8 == 7) std::cout << " ";
117 // if(j%64 == 63) std::cout << std::endl;
118 // }
119 
120  //unsigned long long value;
121  //memcpy((void *)&value, (void *) dataP_->substr(2).data(),8); //make data counter
122  //__COUT__ << "Got data: " << dataP_->length()
123  // << std::hex << value << std::dec
124  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_)
125  // << std::endl;
126  //if( NetworkConverters::networkToUnsignedPort(port_) == 2001)
127  //{
128  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
129  // *dataP_ = dataP_->substr(2);
130  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
131  // unsigned int oldValue32;
132  // memcpy((void *)&oldValue32, (void *) dataP_->data(),4);
133  // unsigned int value32;
134  // for(unsigned int i=8; i<dataP_->size(); i+=8)
135  // {
136  // memcpy((void *)&value32, (void *) dataP_->substr(i).data(),4); //make data counter
137  // if(value32 == oldValue32)
138  // std::cout << __COUT_HDR_FL__ << "Trimming! i=" << i
139  // << std::hex << " v: " << value32 << std::dec
140  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_) << std::endl;
141  //
142  // oldValue32 = value32;
143  // }
144  //}
145  DataProducer::setWrittenSubBuffer<std::string,std::map<std::string,std::string>>();
146  }
147 }
148 
149 DEFINE_OTS_PROCESSOR(UDPDataListenerProducer)