00001 #ifndef IOPool_Streamer_Messages_h
00002 #define IOPool_Streamer_Messages_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <vector>
00015 #include <cassert>
00016 #include <iostream>
00017
00018 #include "DataFormats/Provenance/interface/EventID.h"
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 namespace edm {
00032
00033 inline unsigned int decodeInt(unsigned char* v)
00034 {
00035
00036 unsigned int a=v[0], b=v[1], c=v[2], d=v[3];
00037 a|=(b<<8)|(c<<16)|(d<<24);
00038 return a;
00039 }
00040
00041 inline void encodeInt(unsigned int i, unsigned char* v)
00042 {
00043 v[0]=i&0xff;
00044 v[1]=(i>>8)&0xff;
00045 v[2]=(i>>16)&0xff;
00046 v[3]=(i>>24)&0xff;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 class MsgCode
00056 {
00057 public:
00058 enum Codes { INVALID = 0, INIT = 1, EVENT = 2, DONE = 3 };
00059
00060
00061
00062
00063
00064 MsgCode(void* buffer, int size):
00065 buffer_((unsigned char*)buffer),size_(size<4?0:size-4)
00066 { assert(size_>=0); }
00067
00068
00069 explicit MsgCode(void* buffer):
00070 buffer_((unsigned char*)buffer),size_(0)
00071 { }
00072
00073
00074 MsgCode(void* buffer, Codes c):
00075 buffer_((unsigned char*)buffer),size_(0)
00076 { setCode(c); }
00077
00078
00079 MsgCode(void* buffer, int size, Codes c):
00080 buffer_((unsigned char*)buffer),size_(size-4)
00081 { setCode(c); }
00082
00083 void setCode(Codes c)
00084 {
00085 encodeInt(c,buffer_);
00086 }
00087
00088 Codes getCode() const
00089 {
00090 return (Codes)decodeInt(buffer_);
00091 }
00092
00093
00094 void* payload() const { return &buffer_[4]; }
00095 int payloadSize() const { return size_; }
00096 int codeSize() const { return 4; }
00097 int totalSize() const { return size_+4; }
00098
00099 private:
00100 unsigned char* buffer_;
00101 int size_;
00102
00103 };
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 class EventMsg : public MsgCode
00122 {
00123 public:
00124 struct EventMsgHeader
00125 {
00126 unsigned char which_seg_[4];
00127 unsigned char total_segs_[4];
00128 unsigned char event_num_[4];
00129 unsigned char run_num_[4];
00130 unsigned char data_size_[4];
00131 };
00132
00133
00134 EventMsg(MsgCode& mc):
00135 MsgCode(mc),
00136 head_((EventMsgHeader*)MsgCode::payload()),
00137 which_seg_(getWhichSeg()),
00138 total_segs_(getTotalSegs()),
00139 event_num_(getEventNumber()),
00140 run_num_(getRunNumber()),
00141 data_size_(getDataSize())
00142 { }
00143
00144
00145 explicit EventMsg(void* buffer, int size=0):
00146 MsgCode(buffer,size),
00147 head_((EventMsgHeader*)MsgCode::payload()),
00148 which_seg_(getWhichSeg()),
00149 total_segs_(getTotalSegs()),
00150 event_num_(getEventNumber()),
00151 run_num_(getRunNumber()),
00152 data_size_(getDataSize())
00153 { }
00154
00155
00156 EventMsg(void* buffer, int size,
00157 edm::EventNumber_t e,
00158 edm::RunNumber_t r,
00159 int which_seg,
00160 int total_segs):
00161 MsgCode(buffer,size),
00162 head_((EventMsgHeader*)MsgCode::payload()),
00163 which_seg_(which_seg),
00164 total_segs_(total_segs),
00165 event_num_(e),
00166 run_num_(r),
00167 data_size_(payloadSize() - sizeof(EventMsgHeader))
00168 {
00169 setCode(MsgCode::EVENT);
00170 setWhichSeg(which_seg);
00171 setTotalSegs(total_segs);
00172 setEventNumber(e);
00173 setRunNumber(r);
00174 setDataSize(data_size_);
00175 }
00176
00177
00178 void* data() const { return (char*)payload() + sizeof(EventMsgHeader); }
00179
00180 int dataSize() const { return data_size_ + sizeof(EventMsgHeader); }
00181
00182 int getDataSize() const
00183 {
00184 return decodeInt(head_->data_size_);
00185 }
00186
00187 void setDataSize(int s)
00188 {
00189 encodeInt(s,head_->data_size_);
00190 }
00191
00192 int getWhichSeg() const
00193 {
00194 return decodeInt(head_->which_seg_);
00195 }
00196
00197 void setWhichSeg(int s)
00198 {
00199 encodeInt(s,head_->which_seg_);
00200 }
00201
00202 int getTotalSegs() const
00203 {
00204 return decodeInt(head_->total_segs_);
00205 }
00206
00207 void setTotalSegs(int s)
00208 {
00209 encodeInt(s,head_->total_segs_);
00210 }
00211
00212 edm::EventNumber_t getEventNumber() const
00213 {
00214 assert(sizeof(edm::EventNumber_t) == sizeof(int) && "event ID streaming only knows how to work with 4 byte event ID numbers right now");
00215 return decodeInt(head_->event_num_);
00216 }
00217
00218 void setEventNumber(edm::EventNumber_t e)
00219 {
00220 assert(sizeof(edm::RunNumber_t) == sizeof(int) && "run number streaming only knows how to work with 4 byte event ID numbers right now");
00221 encodeInt(e,head_->event_num_);
00222 }
00223
00224 edm::RunNumber_t getRunNumber() const
00225 {
00226 assert(sizeof(edm::EventNumber_t) == sizeof(int) && "event ID streaming only knows how to work with 4 byte event ID numbers right now");
00227 return decodeInt(head_->run_num_);
00228 }
00229
00230 void setRunNumber(edm::RunNumber_t r)
00231 {
00232 assert(sizeof(edm::RunNumber_t) == sizeof(int) && "run number streaming only knows how to work with 4 byte event ID numbers right now");
00233 return encodeInt(r,head_->run_num_);
00234 }
00235
00236
00237 int msgSize() const
00238 {
00239 return codeSize()+sizeof(EventMsgHeader)+getDataSize();
00240 }
00241
00242 private:
00243 EventMsgHeader* head_;
00244 int which_seg_;
00245 int total_segs_;
00246 edm::EventNumber_t event_num_;
00247 edm::RunNumber_t run_num_;
00248 int data_size_;
00249 };
00250
00251
00252
00253
00254
00255
00256
00257 class InitMsg : public MsgCode
00258 {
00259 public:
00260 struct InitMsgHeader
00261 {
00262 unsigned char data_size_[4];
00263 };
00264
00265 InitMsg(MsgCode& m):
00266 MsgCode(m),
00267 head_((InitMsgHeader*)MsgCode::payload()),
00268 data_size_(payloadSize() - sizeof(InitMsgHeader))
00269 { setDataSize(data_size_); }
00270
00271 InitMsg(void* buffer, int size, bool setcode = false):
00272 MsgCode(buffer,size),
00273 head_((InitMsgHeader*)MsgCode::payload()),
00274 data_size_(payloadSize() - sizeof(InitMsgHeader))
00275 {
00276 if(setcode)
00277 {
00278
00279 setCode(MsgCode::INIT);
00280 setDataSize(data_size_);
00281 }
00282 else
00283
00284 data_size_ = getDataSize();
00285 }
00286
00287
00288
00289
00290
00291 void* data() const { return (char*)payload()+sizeof(InitMsgHeader); }
00292 int dataSize() const { return payloadSize() + sizeof(InitMsgHeader); }
00293
00294 int getDataSize() const
00295 {
00296 return decodeInt(head_->data_size_);
00297 }
00298
00299 void setDataSize(int s)
00300 {
00301 encodeInt(s,head_->data_size_);
00302 }
00303
00304
00305 int msgSize() const
00306 {
00307 return codeSize()+sizeof(InitMsgHeader)+getDataSize();
00308 }
00309
00310 private:
00311 InitMsgHeader* head_;
00312 int data_size_;
00313 };
00314
00315 }
00316 #endif
00317