00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 00024 // -- END LICENSE BLOCK ------------------------------------------------ 00025 00026 //---------------------------------------------------------------------- 00033 //---------------------------------------------------------------------- 00034 00035 #include <sick_safetyscanners/data_processing/TCPPacketMerger.h> 00036 00037 namespace sick { 00038 namespace data_processing { 00039 00040 TCPPacketMerger::TCPPacketMerger() 00041 : m_is_complete(false) 00042 , m_deployed_packet_buffer() 00043 { 00044 } 00045 00046 00047 bool TCPPacketMerger::isComplete() const 00048 { 00049 return m_is_complete; 00050 } 00051 00052 bool TCPPacketMerger::isEmpty() const 00053 { 00054 return m_buffer_vector.empty(); 00055 } 00056 00057 sick::datastructure::PacketBuffer TCPPacketMerger::getDeployedPacketBuffer() 00058 { 00059 m_is_complete = false; 00060 return m_deployed_packet_buffer; 00061 } 00062 00063 bool TCPPacketMerger::addTCPPacket(const datastructure::PacketBuffer& buffer) 00064 { 00065 if (isComplete()) 00066 { 00067 m_is_complete = false; 00068 } 00069 00070 addToMap(buffer); 00071 deployPacketIfComplete(); 00072 return isComplete(); 00073 } 00074 00075 bool TCPPacketMerger::addToMap(const datastructure::PacketBuffer& newPacket) 00076 { 00077 uint32_t currentSize = getCurrentSize(); 00078 uint32_t remainingSize = m_targetSize - currentSize; 00079 m_buffer_vector.push_back(newPacket); 00080 if (remainingSize == newPacket.getLength()) 00081 { 00082 m_is_complete = true; 00083 } 00084 00085 return isComplete(); 00086 } 00087 00088 bool TCPPacketMerger::deployPacketIfComplete() 00089 { 00090 if (isComplete()) 00091 { 00092 deployPacket(); 00093 return true; 00094 } 00095 return false; 00096 } 00097 00098 bool TCPPacketMerger::deployPacket() 00099 { 00100 sick::datastructure::PacketBuffer::VectorBuffer headerless_packet_buffer; 00101 for (auto& parsed_packet_buffer : m_buffer_vector) 00102 { 00103 sick::datastructure::PacketBuffer packet_buffer = parsed_packet_buffer.getBuffer(); 00104 00105 headerless_packet_buffer.insert(headerless_packet_buffer.end(), 00106 packet_buffer.getBuffer().begin(), 00107 packet_buffer.getBuffer().end()); 00108 } 00109 m_deployed_packet_buffer.setBuffer(headerless_packet_buffer); 00110 m_buffer_vector.clear(); 00111 return true; 00112 } 00113 00114 uint32_t TCPPacketMerger::getTargetSize() const 00115 { 00116 return m_targetSize; 00117 } 00118 00119 void TCPPacketMerger::setTargetSize(const uint32_t& targetSize) 00120 { 00121 m_targetSize = targetSize; 00122 } 00123 00124 uint32_t TCPPacketMerger::getCurrentSize() const 00125 { 00126 size_t sum = 0; 00127 for (auto it_packet = m_buffer_vector.begin(); it_packet != m_buffer_vector.end(); ++it_packet) 00128 { 00129 const auto& packet = *it_packet; 00130 sum += packet.getLength(); 00131 } 00132 return static_cast<uint32_t>(sum); 00133 } 00134 00135 } // namespace data_processing 00136 } // namespace sick