ROOT logo
// $Id$

/** @file   AliHLTMessage.h
    @author Matthias Richter (customization of Root TMessage )
    @date   
    @brief  Serialization of Root objects in the ALICE HLT. */

// This is the original Root TMessage implementation with a few minor
// modifications, original revision:
// root/net: v5-14-00 $: TMessage.h,v 1.9 2005/12/09 15:12:19 rdm
// Author: Fons Rademakers   19/12/96
//
// 2009-09-01 updating to revision
// @(#)root/net:$Id$
// Streaming problems have been encountered, especially when streaming
// TObjArrays. As long as there was just one member, the streaming was
// fine. With several members, internal variables of the objects have
// been interchanged/mixed. This bug only effected the serialization
// part, not the restoration of the object.

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ALIHLTMESSAGE_H
#define ALIHLTMESSAGE_H


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TMessage                                                             //
//                                                                      //
// Message buffer class used for serializing objects and sending them   //
// over the network.                                                    //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

// TBuffer has been made pure virtual in root version v5-15-02, this
// requires to inherit from TBufferFile instead of TBuffer.
// TMessage is not really used by this class but by including it we also get
// TBufferFile if this exists. The define ROOT_TBufferFile can than be used
// to differentiate between the usage of TBuffer or TBufferFile.
#include "TMessage.h"

#ifndef ROOT_MessageTypes
#include "MessageTypes.h"
#endif
#ifndef ROOT_TBits
#include "TBits.h"
#endif

#include "AliHLTLogging.h"
/**
 * @class AliHLTMessage
 * Serialization of Root objects for transport in the Alice HLT analysis
 * chain.
 * This is the original Root TMessage implementation with a few minor
 * modifications.
 * - the <tt> AliHLTMessage(void *buf, Int_t bufsize)</tt> constructor has been
 *   made public in order to be used externally.
 *
 * The class can be used to extract an object from an input data block, or a
 * data block received via the HOMER interface or from the file writer.
 * <pre>
 *  AliHLTMessage msg(buffer, size);
 *  TObject* pObj=msg.ReadObject(msg.GetClass());
 * </pre>
 *
 * A simple test macro for a file can look like
 * <pre>
 *  const char* filename="myobject.dat";
 *  TString param=filename;
 *  param+="?filetype=raw";
 *  TFile file(param);
 *  if (file.IsZombie()) {
 *    cout << "can not open file " << filename << endl;
 *    return;
 *  }
 *  
 *  TArrayC buffer(file.GetSize());
 *  TArrayC tgtbuffer(file.GetSize());
 *  if (file.ReadBuffer(buffer.GetArray(), buffer.GetSize())) {
 *    cout << "error reading file " << filename << endl;
 *    return;
 *  }
 *
 *  AliHLTMessage msg(buffer.GetArray(), buffer.GetSize());
 *  TObject* pObj=msg.ReadObject(msg.GetClass());
 * </pre>
 *
 * @see AliHLTRootFileWriterComponent for an easy way to save objects
 * exported via AliHLTMessage in a ROOT file.
 *
 * To serialize an object into a buffer, the normal ROOT TMessage mechanism
 * can be used.
 * <pre>
 *    AliHLTMessage msg(kMESS_OBJECT);
 *    msg.WriteObject(pObject);
 *    Int_t iMsgLength=msg.Length();
 *    if (iMsgLength>0) {
 *      msg.SetLength(); // sets the length to the first (reserved) word
 *      char* pMsgBuffer msg.Buffer();
 *      // do something with pMsgBuffer and iMsgLenghth
 *    }
 * </pre>
 */
class AliHLTMessage 
:
# if defined(ROOT_TBufferFile)
public TBufferFile,
#else
public TBuffer,
#endif
public AliHLTLogging {

public:
   AliHLTMessage(UInt_t what = kMESS_ANY);
   AliHLTMessage(void *buf, Int_t bufsize);
   virtual ~AliHLTMessage();

   void SetLength() const;

   void     ForceWriteInfo(TVirtualStreamerInfo *info, Bool_t force);
   void     Forward();
   TClass  *GetClass() const { return fClass;}
   void     IncrementLevel(TVirtualStreamerInfo* info);
   void     Reset();
   void     Reset(UInt_t what) { SetWhat(what); Reset(); }
   UInt_t   What() const { return fWhat; }
   void     SetWhat(UInt_t what);

   void     EnableSchemaEvolution(Bool_t enable = kTRUE) { fEvolution = enable; }
   Bool_t   UsesSchemaEvolution() const { return fEvolution; }
   void     SetCompressionLevel(Int_t level = 1);
   Int_t    GetCompressionLevel() const { return fCompress; }
   Int_t    Compress();
   Int_t    Uncompress();
   char    *CompBuffer() const { return fBufComp; }
   Int_t    CompLength() const { return (Int_t)(fBufCompCur - fBufComp); }
   void     WriteObject(const TObject *obj);
   UShort_t WriteProcessID(TProcessID *pid);

   static void   EnableSchemaEvolutionForAll(Bool_t enable = kTRUE);
   static Bool_t UsesSchemaEvolutionForAll();

   const TList* GetStreamerInfos() const {return fInfos;}

   /**
    * Helper function to stream an object into an AliHLTMessage
    * The returned instance must be cleaned by the caller
    */
   static AliHLTMessage* Stream(TObject* pSrc, Int_t compression=1, unsigned verbosity=0);

   /**
    * Helper function to extract an object from a buffer.
    * The returned object must be cleaned by the caller
    */
   static TObject* Extract(const void* pBuffer, unsigned bufferSize, unsigned verbosity=0);

   /**
    * Helper function to extract an object from  a file containing the streamed object.
    * The returned object must be cleaned by the caller
    */
   static TObject* Extract(const char* filename, unsigned verbosity=0);

private:
   UInt_t   fWhat;        //!Message type
   TClass  *fClass;       //!If message is kMESS_OBJECT pointer to object's class
   Int_t    fCompress;    //!Compression level from 0 (not compressed) to 9 (max compression)
   char    *fBufComp;     //!Compressed buffer
   char    *fBufCompCur;  //!Current position in compressed buffer
   char    *fCompPos;     //!Position of fBufCur when message was compressed
   char    *fBufUncompressed; //!Uncompressed buffer
   TBits    fBitsPIDs;    //Array of bits to mark the TProcessIDs uids written to the message
   TList   *fInfos;       //Array of TStreamerInfo used in WriteObject
   Bool_t   fEvolution;   //True if support for schema evolution required

   static Bool_t fgEvolution;  //True if global support for schema evolution required

   // AliHLTMessage objects cannot be copied or assigned
   AliHLTMessage(const AliHLTMessage &);           // not implemented
   void operator=(const AliHLTMessage &);     // not implemented

   /** the minimum size of a serialized TObject */
   static const Int_t fgkMinimumSize; //!transient

   /** a default buffer describing an empty message */
   static UInt_t fgkDefaultBuffer[2]; //!transient

   ClassDef(AliHLTMessage,2)  // Message buffer class
};

#endif // ALIHLTMESSAGE_H
 AliHLTMessage.h:1
 AliHLTMessage.h:2
 AliHLTMessage.h:3
 AliHLTMessage.h:4
 AliHLTMessage.h:5
 AliHLTMessage.h:6
 AliHLTMessage.h:7
 AliHLTMessage.h:8
 AliHLTMessage.h:9
 AliHLTMessage.h:10
 AliHLTMessage.h:11
 AliHLTMessage.h:12
 AliHLTMessage.h:13
 AliHLTMessage.h:14
 AliHLTMessage.h:15
 AliHLTMessage.h:16
 AliHLTMessage.h:17
 AliHLTMessage.h:18
 AliHLTMessage.h:19
 AliHLTMessage.h:20
 AliHLTMessage.h:21
 AliHLTMessage.h:22
 AliHLTMessage.h:23
 AliHLTMessage.h:24
 AliHLTMessage.h:25
 AliHLTMessage.h:26
 AliHLTMessage.h:27
 AliHLTMessage.h:28
 AliHLTMessage.h:29
 AliHLTMessage.h:30
 AliHLTMessage.h:31
 AliHLTMessage.h:32
 AliHLTMessage.h:33
 AliHLTMessage.h:34
 AliHLTMessage.h:35
 AliHLTMessage.h:36
 AliHLTMessage.h:37
 AliHLTMessage.h:38
 AliHLTMessage.h:39
 AliHLTMessage.h:40
 AliHLTMessage.h:41
 AliHLTMessage.h:42
 AliHLTMessage.h:43
 AliHLTMessage.h:44
 AliHLTMessage.h:45
 AliHLTMessage.h:46
 AliHLTMessage.h:47
 AliHLTMessage.h:48
 AliHLTMessage.h:49
 AliHLTMessage.h:50
 AliHLTMessage.h:51
 AliHLTMessage.h:52
 AliHLTMessage.h:53
 AliHLTMessage.h:54
 AliHLTMessage.h:55
 AliHLTMessage.h:56
 AliHLTMessage.h:57
 AliHLTMessage.h:58
 AliHLTMessage.h:59
 AliHLTMessage.h:60
 AliHLTMessage.h:61
 AliHLTMessage.h:62
 AliHLTMessage.h:63
 AliHLTMessage.h:64
 AliHLTMessage.h:65
 AliHLTMessage.h:66
 AliHLTMessage.h:67
 AliHLTMessage.h:68
 AliHLTMessage.h:69
 AliHLTMessage.h:70
 AliHLTMessage.h:71
 AliHLTMessage.h:72
 AliHLTMessage.h:73
 AliHLTMessage.h:74
 AliHLTMessage.h:75
 AliHLTMessage.h:76
 AliHLTMessage.h:77
 AliHLTMessage.h:78
 AliHLTMessage.h:79
 AliHLTMessage.h:80
 AliHLTMessage.h:81
 AliHLTMessage.h:82
 AliHLTMessage.h:83
 AliHLTMessage.h:84
 AliHLTMessage.h:85
 AliHLTMessage.h:86
 AliHLTMessage.h:87
 AliHLTMessage.h:88
 AliHLTMessage.h:89
 AliHLTMessage.h:90
 AliHLTMessage.h:91
 AliHLTMessage.h:92
 AliHLTMessage.h:93
 AliHLTMessage.h:94
 AliHLTMessage.h:95
 AliHLTMessage.h:96
 AliHLTMessage.h:97
 AliHLTMessage.h:98
 AliHLTMessage.h:99
 AliHLTMessage.h:100
 AliHLTMessage.h:101
 AliHLTMessage.h:102
 AliHLTMessage.h:103
 AliHLTMessage.h:104
 AliHLTMessage.h:105
 AliHLTMessage.h:106
 AliHLTMessage.h:107
 AliHLTMessage.h:108
 AliHLTMessage.h:109
 AliHLTMessage.h:110
 AliHLTMessage.h:111
 AliHLTMessage.h:112
 AliHLTMessage.h:113
 AliHLTMessage.h:114
 AliHLTMessage.h:115
 AliHLTMessage.h:116
 AliHLTMessage.h:117
 AliHLTMessage.h:118
 AliHLTMessage.h:119
 AliHLTMessage.h:120
 AliHLTMessage.h:121
 AliHLTMessage.h:122
 AliHLTMessage.h:123
 AliHLTMessage.h:124
 AliHLTMessage.h:125
 AliHLTMessage.h:126
 AliHLTMessage.h:127
 AliHLTMessage.h:128
 AliHLTMessage.h:129
 AliHLTMessage.h:130
 AliHLTMessage.h:131
 AliHLTMessage.h:132
 AliHLTMessage.h:133
 AliHLTMessage.h:134
 AliHLTMessage.h:135
 AliHLTMessage.h:136
 AliHLTMessage.h:137
 AliHLTMessage.h:138
 AliHLTMessage.h:139
 AliHLTMessage.h:140
 AliHLTMessage.h:141
 AliHLTMessage.h:142
 AliHLTMessage.h:143
 AliHLTMessage.h:144
 AliHLTMessage.h:145
 AliHLTMessage.h:146
 AliHLTMessage.h:147
 AliHLTMessage.h:148
 AliHLTMessage.h:149
 AliHLTMessage.h:150
 AliHLTMessage.h:151
 AliHLTMessage.h:152
 AliHLTMessage.h:153
 AliHLTMessage.h:154
 AliHLTMessage.h:155
 AliHLTMessage.h:156
 AliHLTMessage.h:157
 AliHLTMessage.h:158
 AliHLTMessage.h:159
 AliHLTMessage.h:160
 AliHLTMessage.h:161
 AliHLTMessage.h:162
 AliHLTMessage.h:163
 AliHLTMessage.h:164
 AliHLTMessage.h:165
 AliHLTMessage.h:166
 AliHLTMessage.h:167
 AliHLTMessage.h:168
 AliHLTMessage.h:169
 AliHLTMessage.h:170
 AliHLTMessage.h:171
 AliHLTMessage.h:172
 AliHLTMessage.h:173
 AliHLTMessage.h:174
 AliHLTMessage.h:175
 AliHLTMessage.h:176
 AliHLTMessage.h:177
 AliHLTMessage.h:178
 AliHLTMessage.h:179
 AliHLTMessage.h:180
 AliHLTMessage.h:181
 AliHLTMessage.h:182
 AliHLTMessage.h:183
 AliHLTMessage.h:184
 AliHLTMessage.h:185
 AliHLTMessage.h:186
 AliHLTMessage.h:187
 AliHLTMessage.h:188
 AliHLTMessage.h:189
 AliHLTMessage.h:190
 AliHLTMessage.h:191
 AliHLTMessage.h:192
 AliHLTMessage.h:193
 AliHLTMessage.h:194
 AliHLTMessage.h:195
 AliHLTMessage.h:196
 AliHLTMessage.h:197