Belle II Software light-2607-kasei
MsgHandler Class Reference

A class to encode/decode an EvtMessage. More...

#include <MsgHandler.h>

Collaboration diagram for MsgHandler:

Public Member Functions

 MsgHandler (int complevel=0)
 Constructor.
 
virtual ~MsgHandler ()
 Destructor.
 
virtual void clear ()
 Clear object list.
 
virtual void add (const TObject *, const std::string &name)
 Add an object to be streamed.
 
virtual EvtMessageencode_msg (ERecordType rectype)
 Stream object list into an EvtMessage.
 
virtual void decode_msg (EvtMessage *msg, std::vector< TObject * > &objlist, std::vector< std::string > &namelist)
 Decode an EvtMessage into a vector list of objects with names.
 

Private Attributes

CharBuffer m_buf
 EvtMessage character buffer for encode_msg().
 
CharBuffer m_compBuf
 EvtMessage character buffer for compressing/decompressing.
 
std::unique_ptr< TMessage > m_msg
 Used for serialising objects into m_buf.
 
InMessage m_inMsg
 Used for deserializing in decode_msg()
 
int m_complevel
 compression algorithm * 100 + compression level.
 

Detailed Description

A class to encode/decode an EvtMessage.

Definition at line 103 of file MsgHandler.h.

Constructor & Destructor Documentation

◆ MsgHandler()

MsgHandler ( int complevel = 0)
explicit

Constructor.

Parameters
complevelCompression level and algorithm: algorithm*100 + level where algorithm can be one of 0: default root system algorithm (usually zlib) 1: zlib 2: lzma 3: old root compression code and level can be between 1 and 9 (0 disables compression). So for example 101 corresponds to zlib with minimal compression while 209 means lzma with maximal compression.

Definition at line 25 of file MsgHandler.cc.

25 :
26 m_buf(100000),
27 m_compBuf(0),
28 m_msg(new TMessage(kMESS_OBJECT))
29{
30 m_complevel = complevel;
31
32 //Schema evolution is needed to stream genfit tracks
33 //If disabled, streamers will crash when reading data.
34 TMessage::EnableSchemaEvolutionForAll();
35 m_msg->SetWriteMode();
36}
int m_complevel
compression algorithm * 100 + compression level.
Definition MsgHandler.h:134
CharBuffer m_buf
EvtMessage character buffer for encode_msg().
Definition MsgHandler.h:130
CharBuffer m_compBuf
EvtMessage character buffer for compressing/decompressing.
Definition MsgHandler.h:131
std::unique_ptr< TMessage > m_msg
Used for serialising objects into m_buf.
Definition MsgHandler.h:132

Member Function Documentation

◆ add()

void add ( const TObject * obj,
const std::string & name )
virtual

Add an object to be streamed.

Definition at line 46 of file MsgHandler.cc.

47{
48 m_msg->WriteObject(obj);
49
50 int len = m_msg->Length();
51 const char* buf = m_msg->Buffer();
52
53 if (len > c_maxObjectSizeBytes) {
54 B2WARNING("MsgHandler: Object " << name << " is very large (" << len << " bytes), parallel processing may be slow.");
55 }
56
57 // Put name of object in output buffer including a final 0-byte
58 UInt_t nameLength = name.size() + 1;
59 m_buf.add(&nameLength, sizeof(nameLength));
60 m_buf.add(name.c_str(), nameLength);
61 // Copy object into buffer
62 m_buf.add(&len, sizeof(len));
63 m_buf.add(buf, len);
64 m_msg->Reset();
65}

◆ clear()

void clear ( )
virtual

Clear object list.

Definition at line 40 of file MsgHandler.cc.

41{
42 m_buf.clear();
43 m_compBuf.clear();
44}

◆ decode_msg()

void decode_msg ( EvtMessage * msg,
std::vector< TObject * > & objlist,
std::vector< std::string > & namelist )
virtual

Decode an EvtMessage into a vector list of objects with names.

Definition at line 106 of file MsgHandler.cc.

108{
109 const char* msgptr = msg->msg();
110 const char* end = msgptr + msg->msg_size();
111
113 // apparently message is compressed, let's decompress
114 m_compBuf.clear();
115 int nzip{0}, nout{0};
116 // ROOT wants a non-const unsigned char pointer although it only reads the data
117 auto* zipptr = reinterpret_cast<unsigned char*>(const_cast<char*>(msgptr));
118 const auto* zipend = reinterpret_cast<const unsigned char*>(end);
119 // and uncompress everything
120 while (zipptr < zipend) {
121 // first get a header of the next block so we know how big the output will be
122 if (R__unzip_header(&nzip, zipptr, &nout) != 0) {
123 B2FATAL("Cannot uncompress message header");
124 }
125 // no more output? fine
126 if (!nout) break;
127 if (zipend - zipptr > nzip) {
128 B2FATAL("Not enough bytes left to uncompress");
129 }
130 // otherwise make sure output buffer is large enough
131 int old_size = m_compBuf.size();
132 m_compBuf.resize(old_size + nout);
133 // and uncompress, the amount of bytes will be returned as irep
134 int irep{0};
135 R__unzip(&nzip, zipptr, &nout, reinterpret_cast<unsigned char*>(m_compBuf.data() + old_size), &irep);
136 // if that is not positive an error happened, bail
137 if (irep <= 0) {
138 B2FATAL("Cannot uncompress message");
139 }
140 // otherwise advance pointer by the amount of bytes compressed bytes in the block
141 zipptr += nzip;
142 }
143 // ok, decompressed successfully, set msg pointer to the correct area
144 msgptr = m_compBuf.data();
145 end = msgptr + m_compBuf.size();
146 }
147
148 while (msgptr < end) {
149 // Restore object name
150 UInt_t nameLength;
151 memcpy(&nameLength, msgptr, sizeof(nameLength));
152 msgptr += sizeof(nameLength);
153 if (nameLength == 0 || std::distance(msgptr, end) < nameLength)
154 B2FATAL("Buffer overrun while decoding object name, check length fields!");
155
156 // read full string but omit final 0-byte. This safeguards against strings containing 0-bytes
157 namelist.emplace_back(msgptr, nameLength - 1);
158 msgptr += nameLength;
159
160 // Restore object
161 UInt_t objlen;
162 memcpy(&objlen, msgptr, sizeof(objlen));
163 msgptr += sizeof(objlen);
164 if (objlen == 0 || std::distance(msgptr, end) < objlen)
165 B2FATAL("Buffer overrun while decoding object, check length fields!");
166
167 m_inMsg.SetBuffer(msgptr, objlen);
168 objlist.push_back(m_inMsg.readTObject());
169 msgptr += objlen;
170 //no need to call InMessage::Reset() here (done in SetBuffer())
171 }
172}
int msg_size() const
Get size of message body.
bool hasMsgFlags(unsigned int flags) const
Check if the message has the given flags.
Definition EvtMessage.h:115
char * msg()
Get pointer to message body.
@ c_MsgCompressed
indicates that the message body is compressed and should be uncompressed using ROOT R__unzip_header a...
Definition EvtMessage.h:69
InMessage m_inMsg
Used for deserializing in decode_msg()
Definition MsgHandler.h:133

◆ encode_msg()

EvtMessage * encode_msg ( ERecordType rectype)
virtual

Stream object list into an EvtMessage.

Caller is responsible for deletion.

Definition at line 67 of file MsgHandler.cc.

68{
69 if (rectype == MSG_TERMINATE) {
70 auto* eod = new EvtMessage(nullptr, 0, rectype);
71 return eod;
72 }
73
74 // which buffer to send? defaults to uncompressed
75 auto buf = &m_buf;
76 unsigned int flags = 0;
77 // but if we have compression enabled then please compress.
78 if (m_complevel > 0) {
79 // make sure buffer for the compression is big enough.
80 m_compBuf.resize(m_buf.size());
81 // And call the root compression function
82 const int algorithm = m_complevel / 100;
83 const int level = m_complevel % 100;
84 int irep{0}, nin{(int)m_buf.size()}, nout{nin};
85 R__zipMultipleAlgorithm(level, &nin, m_buf.data(), &nout, m_compBuf.data(), &irep,
86 (ROOT::RCompressionSetting::EAlgorithm::EValues) algorithm);
87 // it returns the number of bytes of the output in irep. If that is zero or
88 // to big compression failed and we transmit uncompressed.
89 if (irep > 0 && irep <= nin) {
90 //set correct size of compressed message
91 m_compBuf.resize(irep);
92 // and set pointer to correct buffer for creating message
93 buf = &m_compBuf;
94 // also add a flag indicating it's compressed
96 }
97 }
98
99 auto* evtmsg = new EvtMessage(buf->data(), buf->size(), rectype);
100 evtmsg->setMsgFlags(flags);
101 clear();
102
103 return evtmsg;
104}
virtual void clear()
Clear object list.
Definition MsgHandler.cc:40

Member Data Documentation

◆ m_buf

CharBuffer m_buf
private

EvtMessage character buffer for encode_msg().

Definition at line 130 of file MsgHandler.h.

◆ m_compBuf

CharBuffer m_compBuf
private

EvtMessage character buffer for compressing/decompressing.

Definition at line 131 of file MsgHandler.h.

◆ m_complevel

int m_complevel
private

compression algorithm * 100 + compression level.

level can be 0 for no compression to 9 for highest compression, algorithm can be one of default (0), zlib (1) or lzma (2)

Definition at line 134 of file MsgHandler.h.

◆ m_inMsg

InMessage m_inMsg
private

Used for deserializing in decode_msg()

Definition at line 133 of file MsgHandler.h.

◆ m_msg

std::unique_ptr<TMessage> m_msg
private

Used for serialising objects into m_buf.

Definition at line 132 of file MsgHandler.h.


The documentation for this class was generated from the following files: