EUDAQ
 All Classes Namespaces Files Functions Variables Pages
AidaPacket.hh
1 
2 #ifndef EUDAQ_INCLUDED_AidaPacket
3 #define EUDAQ_INCLUDED_AidaPacket
4 
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <iosfwd>
9 #include <iostream>
10 #include <memory>
11 
12 #include "eudaq/Serializable.hh"
13 #include "eudaq/Serializer.hh"
14 #include "eudaq/Event.hh"
15 #include "eudaq/Exception.hh"
16 #include "eudaq/Utils.hh"
17 #include "eudaq/SmartEnum.hh"
18 #include "eudaq/Platform.hh"
19 #include "eudaq/MetaData.hh"
20 
21 #define EUDAQ_DECLARE_PACKET() \
22 public: \
23  static uint64_t eudaq_static_type(); \
24  virtual uint64_t get_type() const { return eudaq_static_type(); }
25 // private: \
26 //static const int EUDAQ_DUMMY_VAR_DONT_USE = 0
27 
28 #define EUDAQ_DEFINE_PACKET(type, name) \
29  uint64_t type::eudaq_static_type() { \
30  static const uint64_t type_(name); \
31  return type_; \
32  } \
33  namespace _eudaq_dummy_ { \
34  static eudaq::RegisterPacketType<type> eudaq_packet_reg; \
35  }
36 
37 namespace eudaq {
38 
39  class DLLEXPORT AidaPacket : public Serializable {
40  public:
41  AidaPacket(uint64_t type, uint64_t subtype) : AidaPacket() {
42  m_header.data.packetType = type;
43  m_header.data.packetSubType = subtype;
44  };
46 
47  //
48  // packet header
49  //
50 
51  class PacketHeader {
52  public:
53  struct {
54  uint64_t marker; // 8 byte string: #PACKET#
55  uint64_t packetType; // 8 byte string
56  uint64_t packetSubType; // 8 byte string
57  uint64_t packetNumber;
58  } data;
59  };
60 
61  void SerializeHeader(Serializer &) const;
62 
63  uint64_t GetPacketMarker() const { return m_header.data.marker; };
64  uint64_t GetPacketType() const { return m_header.data.packetType; };
65  uint64_t GetPacketSubType() const { return m_header.data.packetSubType; };
66  uint64_t GetPacketNumber() const { return m_header.data.packetNumber; };
67 
68  uint64_t GetPacketDataSize() const { return m_data_size; };
69 
70  void SetPacketType(uint64_t type) { m_header.data.packetType = type; };
71  void SetPacketSubType(uint64_t type) {
72  m_header.data.packetSubType = type;
73  };
74 
75  //
76  // meta data
77  //
78 
79  MetaData &GetMetaData() { return m_meta_data; }
80 
81  void SerializeMetaData(Serializer &) const;
82 
83  //
84  // data
85  //
86 
87  void SetData(uint64_t *data, uint64_t size) {
88  m_data = data;
89  m_data_size = size;
90  }
91 
92  void SetData(std::vector<uint64_t> &data) {
93  m_data_size = data.size();
94  m_data = data.data();
95  }
96 
97  void SetData(std::vector<uint64_t> *data) {
98  m_data = data->data();
99  m_data_size = data->size();
100  }
101 
102  virtual void DeserializeData(Deserializer &);
103 
104  virtual void Serialize(Serializer &) const;
105 
106  static PacketHeader DeserializeHeader(Deserializer &);
107 
108  virtual void Print(std::ostream &os) const;
109 
110  //
111  // static helper methods
112  //
113 
114  typedef struct {
115  uint64_t number;
116  std::string string;
118 
119  static const PacketIdentifier &identifier();
120 
121  static uint64_t str2type(const std::string &str);
122  static std::string type2str(uint64_t id);
123 
124  static const uint64_t *const bit_mask();
125 
126  protected:
127  friend class PacketFactory;
128  friend class AidaIndexData;
129  AidaPacket() : m_data_size(0) {
130  m_header.data.marker = identifier().number;
131  m_header.data.packetNumber = getNextPacketNumber();
132  };
133 
134  AidaPacket(const PacketHeader &header, const MetaData &meta);
135  AidaPacket(PacketHeader &header, Deserializer &ds);
136 
137  static uint64_t getNextPacketNumber() {
138  static uint64_t packetCounter = 0;
139  return ++packetCounter;
140  }
141 
142  PacketHeader m_header;
143  MetaData m_meta_data;
144  uint64_t checksum;
145 
146  private:
147  std::unique_ptr<uint64_t[]> placeholder;
148  uint64_t m_data_size;
149  uint64_t *m_data;
150  };
151 
152  class DLLEXPORT EventPacket : public AidaPacket {
153  EUDAQ_DECLARE_PACKET();
154 
155  public:
156  EventPacket(const Event &ev); // wrapper for old-style events
157  virtual void Serialize(Serializer &) const;
158 
159  protected:
160  template <typename T_Packet> friend struct RegisterPacketType;
161 
162  EventPacket(PacketHeader &header, Deserializer &ds);
163  const Event *m_ev;
164  };
165 
166  DLLEXPORT std::ostream &operator<<(std::ostream &, const AidaPacket &);
167 
168  class DLLEXPORT PacketFactory {
169  public:
170  static std::shared_ptr<AidaPacket> Create(Deserializer &ds);
171 
172  typedef std::shared_ptr<AidaPacket>(*packet_creator)(
174  static void Register(uint64_t id, packet_creator func);
175  static packet_creator GetCreator(int id);
176 
177  private:
178  typedef std::map<int, packet_creator> map_t;
179  static map_t &get_map();
180  };
181 
185  template <typename T_Packet> struct RegisterPacketType {
187  PacketFactory::Register(T_Packet::eudaq_static_type(), &factory_func);
188  }
189  static std::shared_ptr<AidaPacket>
190  factory_func(AidaPacket::PacketHeader &header, Deserializer &ds) {
191  return std::shared_ptr<AidaPacket>(new T_Packet(header, ds));
192  }
193  };
194 }
195 
196 #endif // EUDAQ_INCLUDED_AidaPacket
Definition: AidaIndexData.hh:14
Definition: AidaPacket.hh:114
Definition: Serializer.hh:156
Definition: AidaPacket.hh:168
Definition: MetaData.hh:15
Definition: Serializable.hh:13
Definition: AidaPacket.hh:39
Definition: AidaPacket.hh:152
Definition: Serializer.hh:19
Definition: AidaPacket.hh:185
Definition: AidaPacket.hh:51
Definition: Event.hh:38