test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Messages.h
Go to the documentation of this file.
1 #ifndef IOPool_Streamer_Messages_h
2 #define IOPool_Streamer_Messages_h
3 
4 /*
5  The header for each of the data buffer that will be transferred.
6  The packing and unpacking of the data in the header is directly
7  handled here.
8 
9  Every incoming message has a length and message code. Beyond that,
10  each type has a specific header, followed by a specific
11  payload - the packed ROOT data buffer.
12 */
13 
14 #include <vector>
15 #include <cassert>
16 #include <iostream>
17 
19 
20 /*
21  There is still a problem here - the message lengths are not
22  well specified. There is dataSize, msgSize, getDataSize,
23  and payloadSize calls. This sounds like too many.
24 
25  There is still an inconsistency between the way that dataSize is treated
26  in the EventMsg and in the InitMsg. The EventMsg method is better. The
27  problem is that the base class (MsgCode) may have a zero size if it is
28  referring to an incoming buffer. This makes a call to payloadSize useless.
29  */
30 
31 namespace edm {
32 
33  inline unsigned int decodeInt(unsigned char* v)
34  {
35  // first four bytes are code, LSB first
36  unsigned int a=v[0], b=v[1], c=v[2], d=v[3];
37  a|=(b<<8)|(c<<16)|(d<<24);
38  return a;
39  }
40 
41  inline void encodeInt(unsigned int i, unsigned char* v)
42  {
43  v[0]=i&0xff;
44  v[1]=(i>>8)&0xff;
45  v[2]=(i>>16)&0xff;
46  v[3]=(i>>24)&0xff;
47  }
48 
49  // -----------------------------------------------
50 
51  // we currently only support two message types:
52  // 1 = SendJobHead (INIT)
53  // 2 = SendEvent (EVENT)
54 
55  class MsgCode
56  {
57  public:
58  enum Codes { INVALID = 0, INIT = 1, EVENT = 2, DONE = 3 };
59 
60  // header is the message code
61  // the size kept in this object is the available space (i.e. -header)
62 
63  // the entire size of the buffer
64  MsgCode(void* buffer, int size):
65  buffer_((unsigned char*)buffer),size_(size<4?0:size-4)
66  { assert(size_>=0); }
67 
68  // unknown size
69  explicit MsgCode(void* buffer):
70  buffer_((unsigned char*)buffer),size_(0)
71  { }
72 
73  // with header message code set
74  MsgCode(void* buffer, Codes c):
75  buffer_((unsigned char*)buffer),size_(0)
76  { setCode(c); }
77 
78  // with header message code set and a total buffer length
79  MsgCode(void* buffer, int size, Codes c):
80  buffer_((unsigned char*)buffer),size_(size-4)
81  { setCode(c); }
82 
83  void setCode(Codes c)
84  {
85  encodeInt(c,buffer_);
86  }
87 
88  Codes getCode() const
89  {
90  return (Codes)decodeInt(buffer_);
91  }
92 
93  // adjust for header (header not included in payload address
94  void* payload() const { return &buffer_[4]; }
95  int payloadSize() const { return size_; }
96  int codeSize() const { return 4; }
97  int totalSize() const { return size_+4; }
98 
99  private:
100  unsigned char* buffer_;
101  int size_;
102 
103  };
104 
105  // -----------------------------------------------
106 
107  /*
108  added two more fields - "the m out of n data".
109  Each message will contain these fragment counts and the header
110  data.
111 
112  Event Message Format:
113  |MSG_ID|WHICH_SEG|TOTAL_SEGS|EVENT_ID|RUN_ID|DATA_SIZE| ...DATA... |
114  |-code-|-------------------header --------------------|
115 
116  In order to know the original data, you must concatenate all the
117  segments in the proper order. The field WHICH_SEG which one of the
118  TOTAL_SEGS this part of the data is.
119  */
120 
121  class EventMsg : public MsgCode
122  {
123  public:
125  {
126  unsigned char which_seg_[4];
127  unsigned char total_segs_[4];
128  unsigned char event_num_[4];
129  unsigned char run_num_[4];
130  unsigned char data_size_[4];
131  };
132 
133  // incoming data buffer to be looked at
135  MsgCode(mc),
142  { }
143 
144  // incoming data buffer to be looked at
145  explicit EventMsg(void* buffer, int size=0):
146  MsgCode(buffer,size),
153  { }
154 
155  // outgoing data buffer to be filled
156  EventMsg(void* buffer, int size,
159  int which_seg,
160  int total_segs):
161  MsgCode(buffer,size),
163  which_seg_(which_seg),
164  total_segs_(total_segs),
165  event_num_(e),
166  run_num_(r),
168  {
170  setWhichSeg(which_seg);
171  setTotalSegs(total_segs);
172  setEventNumber(e);
173  setRunNumber(r);
175  }
176 
177  // the data is really a SendEvent
178  void* data() const { return (char*)payload() + sizeof(EventMsgHeader); }
179  // the size is max size here
180  int dataSize() const { return data_size_ + sizeof(EventMsgHeader); }
181 
182  int getDataSize() const
183  {
184  return decodeInt(head_->data_size_);
185  }
186 
187  void setDataSize(int s)
188  {
190  }
191 
192  int getWhichSeg() const
193  {
194  return decodeInt(head_->which_seg_);
195  }
196 
197  void setWhichSeg(int s)
198  {
200  }
201 
202  int getTotalSegs() const
203  {
204  return decodeInt(head_->total_segs_);
205  }
206 
207  void setTotalSegs(int s)
208  {
210  }
211 
213  {
214  assert(sizeof(edm::EventNumber_t) == sizeof(int) && "event ID streaming only knows how to work with 4 byte event ID numbers right now");
215  return decodeInt(head_->event_num_);
216  }
217 
219  {
220  assert(sizeof(edm::RunNumber_t) == sizeof(int) && "run number streaming only knows how to work with 4 byte event ID numbers right now");
222  }
223 
225  {
226  assert(sizeof(edm::EventNumber_t) == sizeof(int) && "event ID streaming only knows how to work with 4 byte event ID numbers right now");
227  return decodeInt(head_->run_num_);
228  }
229 
231  {
232  assert(sizeof(edm::RunNumber_t) == sizeof(int) && "run number streaming only knows how to work with 4 byte event ID numbers right now");
233  return encodeInt(r,head_->run_num_);
234  }
235 
236  // the number of bytes used, including the headers
237  int msgSize() const
238  {
239  return codeSize()+sizeof(EventMsgHeader)+getDataSize();
240  }
241 
242  private:
249  };
250 
251  // -------------------------------------------------
252  /*
253  Format:
254  | MSG_ID | DATA_SIZE | ... DATA ... |
255  */
256 
257  class InitMsg : public MsgCode
258  {
259  public:
261  {
262  unsigned char data_size_[4];
263  };
264 
266  MsgCode(m),
269  { setDataSize(data_size_); }
270 
271  InitMsg(void* buffer, int size, bool setcode = false):
272  MsgCode(buffer,size),
274  data_size_(payloadSize() - sizeof(InitMsgHeader)) // default to full length
275  {
276  if(setcode)
277  {
278  // for new message
281  }
282  else
283  // for existing message
285  }
286 
287  // the data is really a SendJobHeader
288  // for this message, there is nothing manually encoded/decoded in the
289  // header, it is all contained in the ROOT buffer.
290  // currently we supply no extra data header
291  void* data() const { return (char*)payload()+sizeof(InitMsgHeader); }
292  int dataSize() const { return payloadSize() + sizeof(InitMsgHeader); }
293 
294  int getDataSize() const
295  {
296  return decodeInt(head_->data_size_);
297  }
298 
299  void setDataSize(int s)
300  {
302  }
303 
304  // the number of bytes used, including the headers
305  int msgSize() const
306  {
307  return codeSize()+sizeof(InitMsgHeader)+getDataSize();
308  }
309 
310  private:
313  };
314 
315 }
316 #endif
317 
unsigned char total_segs_[4]
Definition: Messages.h:127
int i
Definition: DBlmapReader.cc:9
unsigned char event_num_[4]
Definition: Messages.h:128
void encodeInt(unsigned int i, unsigned char *v)
Definition: Messages.h:41
unsigned int EventNumber_t
Definition: EventID.h:30
int codeSize() const
Definition: Messages.h:96
MsgCode(void *buffer, int size)
Definition: Messages.h:64
void setRunNumber(edm::RunNumber_t r)
Definition: Messages.h:230
void setDataSize(int s)
Definition: Messages.h:187
unsigned char run_num_[4]
Definition: Messages.h:129
int msgSize() const
Definition: Messages.h:237
void * payload() const
Definition: Messages.h:94
void setEventNumber(edm::EventNumber_t e)
Definition: Messages.h:218
MsgCode(void *buffer, Codes c)
Definition: Messages.h:74
void setTotalSegs(int s)
Definition: Messages.h:207
void * data() const
Definition: Messages.h:178
void setCode(Codes c)
Definition: Messages.h:83
int data_size_
Definition: Messages.h:248
EventMsg(void *buffer, int size=0)
Definition: Messages.h:145
InitMsg(void *buffer, int size, bool setcode=false)
Definition: Messages.h:271
MsgCode(void *buffer, int size, Codes c)
Definition: Messages.h:79
int data_size_
Definition: Messages.h:312
int which_seg_
Definition: Messages.h:244
int total_segs_
Definition: Messages.h:245
unsigned char which_seg_[4]
Definition: Messages.h:126
edm::RunNumber_t getRunNumber() const
Definition: Messages.h:224
int getDataSize() const
Definition: Messages.h:182
int dataSize() const
Definition: Messages.h:180
InitMsgHeader * head_
Definition: Messages.h:311
MsgCode(void *buffer)
Definition: Messages.h:69
Codes getCode() const
Definition: Messages.h:88
int getTotalSegs() const
Definition: Messages.h:202
void * data() const
Definition: Messages.h:291
edm::EventNumber_t event_num_
Definition: Messages.h:246
double b
Definition: hdecay.h:120
edm::EventNumber_t getEventNumber() const
Definition: Messages.h:212
int dataSize() const
Definition: Messages.h:292
unsigned char data_size_[4]
Definition: Messages.h:262
int payloadSize() const
Definition: Messages.h:95
unsigned char * buffer_
Definition: Messages.h:100
double a
Definition: hdecay.h:121
InitMsg(MsgCode &m)
Definition: Messages.h:265
int getWhichSeg() const
Definition: Messages.h:192
unsigned char data_size_[4]
Definition: Messages.h:130
unsigned int decodeInt(unsigned char *v)
Definition: Messages.h:33
void setWhichSeg(int s)
Definition: Messages.h:197
void setDataSize(int s)
Definition: Messages.h:299
edm::RunNumber_t run_num_
Definition: Messages.h:247
EventMsgHeader * head_
Definition: Messages.h:243
unsigned int RunNumber_t
Definition: EventRange.h:32
int msgSize() const
Definition: Messages.h:305
int totalSize() const
Definition: Messages.h:97
EventMsg(MsgCode &mc)
Definition: Messages.h:134
int getDataSize() const
Definition: Messages.h:294
tuple size
Write out results.
EventMsg(void *buffer, int size, edm::EventNumber_t e, edm::RunNumber_t r, int which_seg, int total_segs)
Definition: Messages.h:156