otsdaq  v2_01_00
UDPDataListenerProducer_processor.cc
1 #include "otsdaq-core/DataProcessorPlugins/UDPDataListenerProducer.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.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 = 0x10000000; //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__ << << " 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  //__COUT__ << "Data for ip: " << IPAddress_ << " listening on port: " << requestedPort_ << std::endl;
104 
105  if(
106  (requestedPort_ == 47000 || requestedPort_ == 47001 || requestedPort_ == 47002) &&
107  dataP_->length() > 2)
108  {
109  unsigned char seqId = (*dataP_)[1];
110  if(!(lastSeqId_ + 1 == seqId ||
111  (lastSeqId_ == 255 && seqId == 0)))
112  {
113  __COUT__ << requestedPort_ <<
114  "?????? NOOOO Missing Packets: " <<
115  (unsigned int)lastSeqId_ << " v " << (unsigned int)seqId << __E__;
116  }
117 
118  //if(seqId == 0)
119  // __COUT__ << requestedPort_ << " test" << __E__;
120 
121  lastSeqId_ = seqId;
122  }
123 
124 // char str[5];
125 // for(unsigned int j=0;j<dataP_->length();++j)
126 // {
127 // sprintf(str,"%2.2x",((unsigned int)(*dataP_)[j]) & ((unsigned int)(0x0FF)));
128 //
129 // if(j%64 == 0) std::cout << "RECV 0x\t";
130 // std::cout << str;
131 // if(j%8 == 7) std::cout << " ";
132 // if(j%64 == 63) std::cout << std::endl;
133 // }
134 
135  //unsigned long long value;
136  //memcpy((void *)&value, (void *) dataP_->substr(2).data(),8); //make data counter
137  //__COUT__ << "Got data: " << dataP_->length()
138  // << std::hex << value << std::dec
139  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_)
140  // << std::endl;
141  //if( NetworkConverters::networkToUnsignedPort(port_) == 2001)
142  //{
143  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
144  // *dataP_ = dataP_->substr(2);
145  // std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
146  // unsigned int oldValue32;
147  // memcpy((void *)&oldValue32, (void *) dataP_->data(),4);
148  // unsigned int value32;
149  // for(unsigned int i=8; i<dataP_->size(); i+=8)
150  // {
151  // memcpy((void *)&value32, (void *) dataP_->substr(i).data(),4); //make data counter
152  // if(value32 == oldValue32)
153  // std::cout << __COUT_HDR_FL__ << "Trimming! i=" << i
154  // << std::hex << " v: " << value32 << std::dec
155  // << " from port: " << NetworkConverters::networkToUnsignedPort(port_) << std::endl;
156  //
157  // oldValue32 = value32;
158  // }
159  //}
160  DataProducer::setWrittenSubBuffer<std::string,std::map<std::string,std::string>>();
161  }
162 }
163 
164 DEFINE_OTS_PROCESSOR(UDPDataListenerProducer)