3 /** @file AliHLTMessage.cxx
4 @author Matthias Richter (customization of Root TMessage )
6 @brief Serialization of Root objects in the ALICE HLT. */
8 // This is the original Root TMessage implementation with a few minor
9 // modifications, original revision:
10 // root/net: v5-14-00 $: TMessage.cxx,v 1.6 2004/05/07 09:51:58 brun
11 // Author: Fons Rademakers 19/12/96
13 /*************************************************************************
14 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
15 * All rights reserved. *
17 * For the licensing terms see $ROOTSYS/LICENSE. *
18 * For the list of contributors see $ROOTSYS/README/CREDITS. *
19 *************************************************************************/
21 //////////////////////////////////////////////////////////////////////////
25 // Message buffer class used for serializing objects and sending them //
26 // over a network. This class inherits from TBuffer the basic I/O //
29 //////////////////////////////////////////////////////////////////////////
31 #include "AliHLTMessage.h"
35 extern "C" void R__zip (Int_t cxlevel, Int_t *nin, char *bufin, Int_t *lout, char *bufout, Int_t *nout);
36 extern "C" void R__unzip(Int_t *nin, UChar_t *bufin, Int_t *lout, char *bufout, Int_t *nout);
37 const Int_t kMAXBUF = 0xffffff;
39 ClassImp(AliHLTMessage)
41 //______________________________________________________________________________
42 AliHLTMessage::AliHLTMessage(UInt_t what)
44 # ifdef ROOT_TBufferFile
58 // Create a AliHLTMessage object for storing objects. The "what" integer
59 // describes the type of message. Predifined ROOT system message types
60 // can be found in MessageTypes.h. Make sure your own message types are
61 // unique from the ROOT defined message types (i.e. 0 - 10000 are
62 // reserved by ROOT). In case you OR "what" with kMESS_ACK, the message
63 // will wait for an acknowledgement from the remote side. This makes
64 // the sending process synchronous. In case you OR "what" with kMESS_ZIP,
65 // the message will be compressed in TSocket using the zip algorithm
66 // (only if message is > 256 bytes).
68 // space at the beginning of the message reserved for the message length
76 const Int_t AliHLTMessage::fgkMinimumSize=30;
77 UInt_t AliHLTMessage::fgkDefaultBuffer[2]={0,0};
79 //______________________________________________________________________________
80 AliHLTMessage::AliHLTMessage(void *buf, Int_t bufsize)
82 # if defined(ROOT_TBufferFile)
83 TBufferFile(kRead, bufsize>fgkMinimumSize?bufsize:sizeof(fgkDefaultBuffer), bufsize>fgkMinimumSize?buf:&fgkDefaultBuffer, 0),
85 TBuffer(kRead, bufsize>fgkMinimumSize?bufsize:sizeof(fgkDefaultBuffer), bufsize>fgkMinimumSize?buf:&fgkDefaultBuffer, 0),
96 // Create a AliHLTMessage object for reading objects. The objects will be
97 // read from buf. Use the What() method to get the message type.
99 // skip space at the beginning of the message reserved for the message length
100 fBufCur += sizeof(UInt_t);
104 if (fWhat & kMESS_ZIP) {
105 // if buffer has kMESS_ZIP set, move it to fBufComp and uncompress
107 fBufCompCur = fBuffer + bufsize;
111 // NOTE: this is not done in TMessage and will lead to the deletion
112 // of the buffer. This is not allowed in case of HLT where the
113 // buffer is handled by the framework. In general, I think this
114 // is a very bad idea to do it like that in TMessage
119 if (fWhat == kMESS_OBJECT) {
121 fClass = ReadClass(); // get first the class stored in message
122 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
129 //______________________________________________________________________________
130 AliHLTMessage::~AliHLTMessage()
132 // Clean up compression buffer.
136 //______________________________________________________________________________
137 void AliHLTMessage::Forward()
139 // Change a buffer that was received into one that can be send, i.e.
140 // forward a just received message.
144 SetBufferOffset(fBufSize);
152 //______________________________________________________________________________
153 void AliHLTMessage::Reset()
155 // Reset the message buffer so we can use (i.e. fill) it again.
157 SetBufferOffset(sizeof(UInt_t) + sizeof(fWhat));
166 if (fBufUncompressed) {
167 delete [] fBufUncompressed;
168 fBufUncompressed=NULL;
172 //______________________________________________________________________________
173 void AliHLTMessage::SetLength() const
175 // Set the message length at the beginning of the message buffer.
178 char *buf = Buffer();
179 *((UInt_t*)buf) = (UInt_t)(Length() - sizeof(UInt_t));
183 *((UInt_t*)buf) = (UInt_t)(CompLength() - sizeof(UInt_t));
188 //______________________________________________________________________________
189 void AliHLTMessage::SetWhat(UInt_t what)
191 // Using this method one can change the message type a-posteriory.
192 // In case you OR "what" with kMESS_ACK, the message will wait for
193 // an acknowledgement from the remote side. This makes the sending
194 // process synchronous.
198 char *buf = Buffer();
199 buf += sizeof(UInt_t); // skip reserved length space
204 buf += sizeof(UInt_t); // skip reserved length space
205 tobuf(buf, what | kMESS_ZIP);
209 //______________________________________________________________________________
210 void AliHLTMessage::SetCompressionLevel(Int_t level)
212 // Set the message compression level. Can be between 0 and 9 with 0
213 // being no compression and 9 maximum compression. In general the default
214 // level of 1 is the best compromise between achieved compression and
215 // cpu time. Compression will only happen when the message is > 256 bytes.
217 if (level < 0) level = 0;
218 if (level > 9) level = 9;
220 if (level != fCompress && fBufComp) {
229 //______________________________________________________________________________
230 Int_t AliHLTMessage::Compress()
232 // Compress the message. The message will only be compressed if the
233 // compression level > 0 and the if the message is > 256 bytes.
234 // Returns -1 in case of error (when compression fails or
235 // when the message increases in size in some pathological cases),
236 // otherwise returns 0.
238 if (fCompress == 0) {
239 // no compression specified
249 if (fBufComp && fCompPos == fBufCur) {
250 // the message was already compressed
254 // remove any existing compressed buffer before compressing modified message
262 if (Length() <= (Int_t)(256 + 2*sizeof(UInt_t))) {
263 // this message is too small to be compressed
267 Int_t hdrlen = 2*sizeof(UInt_t);
268 Int_t messlen = Length() - hdrlen;
269 Int_t nbuffers = messlen / kMAXBUF;
270 Int_t chdrlen = 3*sizeof(UInt_t); // compressed buffer header length
271 Int_t buflen = TMath::Max(512, chdrlen + messlen + 9*nbuffers);
272 fBufComp = new char[buflen];
273 char *messbuf = Buffer() + hdrlen;
274 char *bufcur = fBufComp + chdrlen;
278 for (Int_t i = 0; i <= nbuffers; i++) {
280 bufmax = messlen - nzip;
283 R__zip(fCompress, &bufmax, messbuf, &bufmax, bufcur, &nout);
284 if (nout == 0 || nout >= messlen) {
285 //this happens when the buffer cannot be compressed
297 fBufCompCur = bufcur;
301 tobuf(bufcur, (UInt_t)(CompLength() - sizeof(UInt_t)));
302 Int_t what = fWhat | kMESS_ZIP;
304 tobuf(bufcur, Length()); // original uncompressed buffer length
309 //______________________________________________________________________________
310 Int_t AliHLTMessage::Uncompress()
312 // Uncompress the message. The message will only be uncompressed when
313 // kMESS_ZIP is set. Returns -1 in case of error, 0 otherwise.
315 if (!fBufComp || !(fWhat & kMESS_ZIP))
319 Int_t hdrlen = 2*sizeof(UInt_t);
320 UChar_t *bufcur = (UChar_t*)fBufComp + hdrlen;
321 frombuf((char *&)bufcur, &buflen);
322 fBuffer = new char[buflen];
323 fBufUncompressed = fBuffer;
325 fBufCur = fBuffer + sizeof(UInt_t) + sizeof(fWhat);
326 fBufMax = fBuffer + fBufSize;
327 char *messbuf = fBuffer + hdrlen;
329 Int_t nin, nout, nbuf;
332 nin = 9 + ((Int_t)bufcur[3] | ((Int_t)bufcur[4] << 8) | ((Int_t)bufcur[5] << 16));
333 nbuf = (Int_t)bufcur[6] | ((Int_t)bufcur[7] << 8) | ((Int_t)bufcur[8] << 16);
334 R__unzip(&nin, bufcur, &nbuf, messbuf, &nout);
337 if (noutot >= buflen - hdrlen) break;