1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>AliHLTPHOS: /home/perthi/cern/aliroot/AliRoot_head020507/HLT/BASE/AliHLTMessage.cxx Source File</title>
4 <link href="doxygen.css" rel="stylesheet" type="text/css">
6 <!-- Generated by Doxygen 1.3.9.1 -->
7 <div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a></div>
9 <a class="el" href="dir_000000.html">BASE</a></div>
10 <h1>AliHLTMessage.cxx</h1><a href="AliHLTMessage_8cxx.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">// $Id$</span>
12 00008 <span class="comment">// This is the original Root TMessage implementation with a few minor</span>
13 00009 <span class="comment">// modifications, original revision:</span>
14 00010 <span class="comment">// root/net: v5-14-00 $: TMessage.cxx,v 1.6 2004/05/07 09:51:58 brun</span>
15 00011 <span class="comment">// Author: Fons Rademakers 19/12/96</span>
17 00013 <span class="comment">/*************************************************************************</span>
18 00014 <span class="comment"> * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *</span>
19 00015 <span class="comment"> * All rights reserved. *</span>
20 00016 <span class="comment"> * *</span>
21 00017 <span class="comment"> * For the licensing terms see $ROOTSYS/LICENSE. *</span>
22 00018 <span class="comment"> * For the list of contributors see $ROOTSYS/README/CREDITS. *</span>
23 00019 <span class="comment"> *************************************************************************/</span>
25 00022 <span class="comment">// //</span>
26 00023 <span class="comment">// TMessage //</span>
27 00024 <span class="comment">// //</span>
28 00025 <span class="comment">// Message buffer class used for serializing objects and sending them //</span>
29 00026 <span class="comment">// over a network. This class inherits from TBuffer the basic I/O //</span>
30 00027 <span class="comment">// serializer. //</span>
31 00028 <span class="comment">// //</span>
32 00030 <span class="comment"></span>
33 00031 <span class="preprocessor">#include "<a class="code" href="AliHLTMessage_8h.html">AliHLTMessage.h</a>"</span>
34 00032 <span class="preprocessor">#include "Bytes.h"</span>
35 00033 <span class="preprocessor">#include "TFile.h"</span>
37 00035 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">void</span> <a class="code" href="AliHLTMessage_8cxx.html#a1">R__zip</a> (Int_t cxlevel, Int_t *nin, <span class="keywordtype">char</span> *bufin, Int_t *lout, <span class="keywordtype">char</span> *bufout, Int_t *nout);
38 00036 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">void</span> <a class="code" href="AliHLTMessage_8cxx.html#a2">R__unzip</a>(Int_t *nin, UChar_t *bufin, Int_t *lout, <span class="keywordtype">char</span> *bufout, Int_t *nout);
39 <a name="l00037"></a><a class="code" href="AliHLTMessage_8cxx.html#a0">00037</a> <span class="keyword">const</span> Int_t <a class="code" href="AliHLTMessage_8cxx.html#a0">kMAXBUF</a> = 0xffffff;
42 <a name="l00040"></a><a class="code" href="AliHLTMessage_8cxx.html#a3">00040</a> <a class="code" href="AliHLTPHOSRawAnalyzerPeakFinder_8cxx.html#a0">ClassImp</a>(<a class="code" href="classAliHLTMessage.html">AliHLTMessage</a>)
44 00042 <span class="comment">//______________________________________________________________________________</span>
45 00043 <a class="code" href="classAliHLTMessage.html">AliHLTMessage</a>::<a class="code" href="classAliHLTMessage.html">AliHLTMessage</a>(UInt_t what)
47 00045 # ifdef ROOT_TBufferFile
48 00046 TBufferFile(kWrite),
50 00048 TBuffer(kWrite),
52 00050 <a class="code" href="classAliHLTLogging.html">AliHLTLogging</a>()
54 00052 <span class="comment">// Create a AliHLTMessage object for storing objects. The "what" integer</span>
55 00053 <span class="comment">// describes the type of message. Predifined ROOT system message types</span>
56 00054 <span class="comment">// can be found in MessageTypes.h. Make sure your own message types are</span>
57 00055 <span class="comment">// unique from the ROOT defined message types (i.e. 0 - 10000 are</span>
58 00056 <span class="comment">// reserved by ROOT). In case you OR "what" with kMESS_ACK, the message</span>
59 00057 <span class="comment">// will wait for an acknowledgement from the remote side. This makes</span>
60 00058 <span class="comment">// the sending process synchronous. In case you OR "what" with kMESS_ZIP,</span>
61 00059 <span class="comment">// the message will be compressed in TSocket using the zip algorithm</span>
62 00060 <span class="comment">// (only if message is > 256 bytes).</span>
64 00062 <span class="comment">// space at the beginning of the message reserved for the message length</span>
65 00063 UInt_t reserved = 0;
66 00064 *<span class="keyword">this</span> << reserved;
69 00067 *<span class="keyword">this</span> << what;
74 00072 fBufCompCur = 0;
78 00076 <span class="comment">//______________________________________________________________________________</span>
79 <a name="l00077"></a><a class="code" href="classAliHLTMessage.html#a1">00077</a> <a class="code" href="classAliHLTMessage.html#a0">AliHLTMessage::AliHLTMessage</a>(<span class="keywordtype">void</span> *buf, Int_t bufsize)
81 00079 # if defined(ROOT_TBufferFile) || defined(HAVE_TBUFFERFILE_H)
82 00080 TBufferFile(kRead, bufsize, buf, 0),
84 00082 TBuffer(kRead, bufsize, buf, 0),
86 00084 <a class="code" href="classAliHLTLogging.html">AliHLTLogging</a>()
88 00086 <span class="comment">// Create a AliHLTMessage object for reading objects. The objects will be</span>
89 00087 <span class="comment">// read from buf. Use the What() method to get the message type.</span>
91 00089 <span class="comment">// skip space at the beginning of the message reserved for the message length</span>
92 00090 fBufCur += <span class="keyword">sizeof</span>(UInt_t);
94 00092 *<span class="keyword">this</span> >> fWhat;
96 00094 <a class="code" href="classAliHLTMessage.html#r2">fCompress</a> = 0;
97 00095 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
98 00096 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
99 00097 <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> = 0;
101 00099 <span class="keywordflow">if</span> (<a class="code" href="classAliHLTMessage.html#r0">fWhat</a> & kMESS_ZIP) {
102 00100 <span class="comment">// if buffer has kMESS_ZIP set, move it to fBufComp and uncompress</span>
103 00101 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = fBuffer;
104 00102 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = fBuffer + bufsize;
106 00104 <a class="code" href="classAliHLTMessage.html#a13">Uncompress</a>();
109 00107 <span class="keywordflow">if</span> (<a class="code" href="classAliHLTMessage.html#r0">fWhat</a> == kMESS_OBJECT) {
111 00109 <a class="code" href="classAliHLTMessage.html#r1">fClass</a> = ReadClass(); <span class="comment">// get first the class stored in message</span>
112 00110 SetBufferOffset(<span class="keyword">sizeof</span>(UInt_t) + <span class="keyword">sizeof</span>(<a class="code" href="classAliHLTMessage.html#r0">fWhat</a>));
114 00112 } <span class="keywordflow">else</span> {
115 00113 <a class="code" href="classAliHLTMessage.html#r1">fClass</a> = 0;
119 00117 <span class="comment">//______________________________________________________________________________</span>
120 <a name="l00118"></a><a class="code" href="classAliHLTMessage.html#a2">00118</a> <a class="code" href="classAliHLTMessage.html#a2">AliHLTMessage::~AliHLTMessage</a>()
122 00120 <span class="comment">// Clean up compression buffer.</span>
123 00121 <span class="keyword">delete</span> [] fBufComp;
126 00124 <span class="comment">//______________________________________________________________________________</span>
127 <a name="l00125"></a><a class="code" href="classAliHLTMessage.html#a4">00125</a> <span class="keywordtype">void</span> <a class="code" href="classAliHLTMessage.html#a4">AliHLTMessage::Forward</a>()
129 00127 <span class="comment">// Change a buffer that was received into one that can be send, i.e.</span>
130 00128 <span class="comment">// forward a just received message.</span>
132 00130 <span class="keywordflow">if</span> (IsReading()) {
133 00131 SetWriteMode();
134 00132 SetBufferOffset(fBufSize);
136 00134 <span class="keywordflow">if</span> (fBufComp) {
137 00135 <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> = fBufCur;
142 00140 <span class="comment">//______________________________________________________________________________</span>
143 <a name="l00141"></a><a class="code" href="classAliHLTMessage.html#a6">00141</a> <span class="keywordtype">void</span> <a class="code" href="classAliHLTMessage.html#a6">AliHLTMessage::Reset</a>()
145 00143 <span class="comment">// Reset the message buffer so we can use (i.e. fill) it again.</span>
147 00145 SetBufferOffset(<span class="keyword">sizeof</span>(UInt_t) + <span class="keyword">sizeof</span>(<a class="code" href="classAliHLTMessage.html#r0">fWhat</a>));
150 00148 <span class="keywordflow">if</span> (fBufComp) {
151 00149 <span class="keyword">delete</span> [] fBufComp;
152 00150 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
153 00151 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
154 00152 <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> = 0;
158 00156 <span class="comment">//______________________________________________________________________________</span>
159 <a name="l00157"></a><a class="code" href="classAliHLTMessage.html#a3">00157</a> <span class="keywordtype">void</span> <a class="code" href="classAliHLTMessage.html#a3">AliHLTMessage::SetLength</a>()<span class="keyword"> const</span>
160 00158 <span class="keyword"></span>{
161 00159 <span class="comment">// Set the message length at the beginning of the message buffer.</span>
163 00161 <span class="keywordflow">if</span> (IsWriting()) {
164 00162 <span class="keywordtype">char</span> *buf = Buffer();
165 00163 *((UInt_t*)buf) = (UInt_t)(Length() - <span class="keyword">sizeof</span>(UInt_t));
167 00165 <span class="keywordflow">if</span> (fBufComp) {
168 00166 buf = fBufComp;
169 00167 *((UInt_t*)buf) = (UInt_t)(Length() - <span class="keyword">sizeof</span>(UInt_t));
174 00172 <span class="comment">//______________________________________________________________________________</span>
175 <a name="l00173"></a><a class="code" href="classAliHLTMessage.html#a9">00173</a> <span class="keywordtype">void</span> <a class="code" href="classAliHLTMessage.html#a9">AliHLTMessage::SetWhat</a>(UInt_t what)
177 00175 <span class="comment">// Using this method one can change the message type a-posteriory.</span>
178 00176 <span class="comment">// In case you OR "what" with kMESS_ACK, the message will wait for</span>
179 00177 <span class="comment">// an acknowledgement from the remote side. This makes the sending</span>
180 00178 <span class="comment">// process synchronous.</span>
182 00180 <a class="code" href="classAliHLTMessage.html#r0">fWhat</a> = what;
184 00182 <span class="keywordtype">char</span> *buf = Buffer();
185 00183 buf += <span class="keyword">sizeof</span>(UInt_t); <span class="comment">// skip reserved length space</span>
186 00184 tobuf(buf, what);
188 00186 <span class="keywordflow">if</span> (fBufComp) {
189 00187 buf = fBufComp;
190 00188 buf += <span class="keyword">sizeof</span>(UInt_t); <span class="comment">// skip reserved length space</span>
191 00189 tobuf(buf, what | kMESS_ZIP);
195 00193 <span class="comment">//______________________________________________________________________________</span>
196 <a name="l00194"></a><a class="code" href="classAliHLTMessage.html#a10">00194</a> <span class="keywordtype">void</span> <a class="code" href="classAliHLTMessage.html#a10">AliHLTMessage::SetCompressionLevel</a>(Int_t level)
198 00196 <span class="comment">// Set the message compression level. Can be between 0 and 9 with 0</span>
199 00197 <span class="comment">// being no compression and 9 maximum compression. In general the default</span>
200 00198 <span class="comment">// level of 1 is the best compromise between achieved compression and</span>
201 00199 <span class="comment">// cpu time. Compression will only happen when the message is > 256 bytes.</span>
203 00201 <span class="keywordflow">if</span> (level < 0) level = 0;
204 00202 <span class="keywordflow">if</span> (level > 9) level = 9;
206 00204 <span class="keywordflow">if</span> (level != <a class="code" href="classAliHLTMessage.html#r2">fCompress</a> && fBufComp) {
207 00205 <span class="keyword">delete</span> [] fBufComp;
208 00206 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
209 00207 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
210 00208 <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> = 0;
212 00210 <a class="code" href="classAliHLTMessage.html#r2">fCompress</a> = level;
215 00213 <span class="comment">//______________________________________________________________________________</span>
216 <a name="l00214"></a><a class="code" href="classAliHLTMessage.html#a12">00214</a> Int_t <a class="code" href="classAliHLTMessage.html#a12">AliHLTMessage::Compress</a>()
218 00216 <span class="comment">// Compress the message. The message will only be compressed if the</span>
219 00217 <span class="comment">// compression level > 0 and the if the message is > 256 bytes.</span>
220 00218 <span class="comment">// Returns -1 in case of error (when compression fails or</span>
221 00219 <span class="comment">// when the message increases in size in some pathological cases),</span>
222 00220 <span class="comment">// otherwise returns 0.</span>
224 00222 <span class="keywordflow">if</span> (<a class="code" href="classAliHLTMessage.html#r2">fCompress</a> == 0) {
225 00223 <span class="comment">// no compression specified</span>
226 00224 <span class="keywordflow">if</span> (fBufComp) {
227 00225 <span class="keyword">delete</span> [] fBufComp;
228 00226 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
229 00227 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
230 00228 <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> = 0;
232 00230 <span class="keywordflow">return</span> 0;
235 00233 <span class="keywordflow">if</span> (<a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> && <a class="code" href="classAliHLTMessage.html#r5">fCompPos</a> == fBufCur) {
236 00234 <span class="comment">// the message was already compressed</span>
237 00235 <span class="keywordflow">return</span> 0;
240 00238 <span class="comment">// remove any existing compressed buffer before compressing modified message</span>
241 00239 <span class="keywordflow">if</span> (fBufComp) {
242 00240 <span class="keyword">delete</span> [] fBufComp;
243 00241 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
244 00242 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
248 00246 <span class="keywordflow">if</span> (Length() <= (Int_t)(256 + 2*<span class="keyword">sizeof</span>(UInt_t))) {
249 00247 <span class="comment">// this message is too small to be compressed</span>
250 00248 <span class="keywordflow">return</span> 0;
253 00251 Int_t hdrlen = 2*<span class="keyword">sizeof</span>(UInt_t);
254 00252 Int_t messlen = Length() - hdrlen;
255 00253 Int_t nbuffers = messlen / kMAXBUF;
256 00254 Int_t chdrlen = 3*<span class="keyword">sizeof</span>(UInt_t); <span class="comment">// compressed buffer header length</span>
257 00255 Int_t buflen = TMath::Max(512, chdrlen + messlen + 9*nbuffers);
258 00256 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = <span class="keyword">new</span> <span class="keywordtype">char</span>[buflen];
259 00257 <span class="keywordtype">char</span> *messbuf = Buffer() + hdrlen;
260 00258 <span class="keywordtype">char</span> *bufcur = <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> + chdrlen;
261 00259 Int_t noutot = 0;
262 00260 Int_t nzip = 0;
263 00261 Int_t nout, bufmax;
264 00262 <span class="keywordflow">for</span> (Int_t i = 0; i <= nbuffers; i++) {
265 00263 <span class="keywordflow">if</span> (i == nbuffers)
266 00264 bufmax = messlen - nzip;
267 00265 <span class="keywordflow">else</span>
268 00266 bufmax = kMAXBUF;
269 00267 <a class="code" href="AliHLTMessage_8cxx.html#a1">R__zip</a>(<a class="code" href="classAliHLTMessage.html#r2">fCompress</a>, &bufmax, messbuf, &bufmax, bufcur, &nout);
270 00268 <span class="keywordflow">if</span> (nout == 0 || nout >= messlen) {
271 00269 <span class="comment">//this happens when the buffer cannot be compressed</span>
272 00270 <span class="keyword">delete</span> [] fBufComp;
273 00271 <a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> = 0;
274 00272 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = 0;
276 00274 <span class="keywordflow">return</span> -1;
278 00276 bufcur += nout;
279 00277 noutot += nout;
280 00278 messbuf += kMAXBUF;
281 00279 nzip += kMAXBUF;
283 00281 <a class="code" href="classAliHLTMessage.html#r4">fBufCompCur</a> = bufcur;
284 00282 fCompPos = fBufCur;
286 00284 bufcur = fBufComp;
287 00285 tobuf(bufcur, (UInt_t)(<a class="code" href="classAliHLTMessage.html#a15">CompLength</a>() - <span class="keyword">sizeof</span>(UInt_t)));
288 00286 Int_t what = <a class="code" href="classAliHLTMessage.html#r0">fWhat</a> | kMESS_ZIP;
289 00287 tobuf(bufcur, what);
290 00288 tobuf(bufcur, Length()); <span class="comment">// original uncompressed buffer length</span>
292 00290 <span class="keywordflow">return</span> 0;
295 00293 <span class="comment">//______________________________________________________________________________</span>
296 <a name="l00294"></a><a class="code" href="classAliHLTMessage.html#a13">00294</a> Int_t <a class="code" href="classAliHLTMessage.html#a13">AliHLTMessage::Uncompress</a>()
298 00296 <span class="comment">// Uncompress the message. The message will only be uncompressed when</span>
299 00297 <span class="comment">// kMESS_ZIP is set. Returns -1 in case of error, 0 otherwise.</span>
301 00299 <span class="keywordflow">if</span> (!<a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> || !(<a class="code" href="classAliHLTMessage.html#r0">fWhat</a> & kMESS_ZIP))
302 00300 <span class="keywordflow">return</span> -1;
305 00303 Int_t hdrlen = 2*<span class="keyword">sizeof</span>(UInt_t);
306 00304 UChar_t *bufcur = (UChar_t*)<a class="code" href="classAliHLTMessage.html#r3">fBufComp</a> + hdrlen;
307 00305 frombuf((<span class="keywordtype">char</span> *&)bufcur, &buflen);
308 00306 fBuffer = <span class="keyword">new</span> <span class="keywordtype">char</span>[buflen];
309 00307 fBufSize = buflen;
310 00308 fBufCur = fBuffer + <span class="keyword">sizeof</span>(UInt_t) + <span class="keyword">sizeof</span>(fWhat);
311 00309 fBufMax = fBuffer + fBufSize;
312 00310 <span class="keywordtype">char</span> *messbuf = fBuffer + hdrlen;
314 00312 Int_t nin, nout, nbuf;
315 00313 Int_t noutot = 0;
316 00314 <span class="keywordflow">while</span> (1) {
317 00315 nin = 9 + ((Int_t)bufcur[3] | ((Int_t)bufcur[4] << 8) | ((Int_t)bufcur[5] << 16));
318 00316 nbuf = (Int_t)bufcur[6] | ((Int_t)bufcur[7] << 8) | ((Int_t)bufcur[8] << 16);
319 00317 <a class="code" href="AliHLTMessage_8cxx.html#a2">R__unzip</a>(&nin, bufcur, &nbuf, messbuf, &nout);
320 00318 <span class="keywordflow">if</span> (!nout) <span class="keywordflow">break</span>;
321 00319 noutot += nout;
322 00320 <span class="keywordflow">if</span> (noutot >= buflen - hdrlen) <span class="keywordflow">break</span>;
324 00322 messbuf += nout;
327 00325 <a class="code" href="classAliHLTMessage.html#r0">fWhat</a> &= ~kMESS_ZIP;
328 00326 <a class="code" href="classAliHLTMessage.html#r2">fCompress</a> = 1;
330 00328 <span class="keywordflow">return</span> 0;
333 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri May 4 04:49:52 2007 for AliHLTPHOS by
334 <a href="http://www.doxygen.org/index.html">
335 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address>