Belle II Software light-2406-ragdoll
EvtMessage.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/pcore/EvtMessage.h>
10
11#include <new>
12#include <cstring>
13
14#include <sys/time.h>
15
16using namespace std;
17using namespace Belle2;
18
19namespace {
21 int roundToNearestInt(int bytes)
22 {
23 return sizeof(int) * ((bytes + sizeof(int) - 1) / sizeof(int));
24 }
25}
26
28 m_data(data),
29 m_ownsBuffer(false)
30{
31}
32
37EvtMessage::EvtMessage(const char* msg, int size, ERecordType type = MSG_EVENT)
38{
39 int fullsize = size + sizeof(EvtHeader);
40 int bufsize = roundToNearestInt(fullsize);
41 m_data = new char[bufsize]; // Allocate new buffer
42 // zero extra bytes
43 memset(m_data + fullsize, 0, bufsize - fullsize);
45 m_ownsBuffer = true;
46}
47
51{
52 *this = evtmsg;
53}
54
57{
58 if (m_ownsBuffer)
59 delete [] m_data;
60}
61
65{
66 if (this != &obj) {
67 if (m_ownsBuffer)
68 delete [] m_data;
69 buffer(obj.m_data); //copy m_data
70 m_ownsBuffer = true; //already set by buffer(), but let's be explicit
71 }
72
73 return *this;
74}
75
77{
78 return m_data;
79}
80
81void EvtMessage::buffer(const char* bufadr)
82{
83 UInt_t size = *(UInt_t*)bufadr;
84 int bufsize = roundToNearestInt(size);
85 m_data = new char[bufsize];
86 memcpy(m_data, bufadr, size);
87 for (int i = bufsize - size; i > 0; i--)
88 m_data[bufsize - i] = '\0'; //zero extra bytes
89 m_ownsBuffer = true;
90}
91
92// @brief size
93// @return record size
95{
96 return ((reinterpret_cast<EvtHeader*>(m_data))->size);
97}
98
100{
101 const int sizeBytes = size();
102 //round up to next int boundary
103 return roundToNearestInt(sizeBytes) / sizeof(int);
104}
105
106// @brief msgsize
107// @return message size
109{
110 return ((reinterpret_cast<EvtHeader*>(m_data))->size - sizeof(EvtHeader));
111}
112
113// Record type
115{
116 return ((reinterpret_cast<EvtHeader*>(m_data))->rectype);
117}
118
120{
121 (reinterpret_cast<EvtHeader*>(m_data))->rectype = type;
122}
123
124// Source of this record
126{
127 return ((reinterpret_cast<EvtHeader*>(m_data))->src);
128}
129
130void EvtMessage::src(int src)
131{
132 (reinterpret_cast<EvtHeader*>(m_data))->src = src;
133}
134
135// Destination of this record
137{
138 return ((reinterpret_cast<EvtHeader*>(m_data))->dest);
139}
140
141void EvtMessage::dest(int dest)
142{
143 (reinterpret_cast<EvtHeader*>(m_data))->dest = dest;
144}
145
146// Time stamp
147struct timeval EvtMessage::time() const {
148 struct timeval tv;
149 tv.tv_sec = (reinterpret_cast<EvtHeader*>(m_data))->time_sec;
150 tv.tv_usec = (reinterpret_cast<EvtHeader*>(m_data))->time_usec;
151 return tv;
152}
153
154void EvtMessage::setTime(const struct timeval& tbuf)
155{
156 (reinterpret_cast<EvtHeader*>(m_data))->time_sec = tbuf.tv_sec;
157 (reinterpret_cast<EvtHeader*>(m_data))->time_usec = tbuf.tv_usec;
158}
159
160// Event Header
162{
163 return (reinterpret_cast<EvtHeader*>(m_data));
164}
165
167{
168 return (reinterpret_cast<const EvtHeader*>(m_data));
169}
170
171// Message body
173{
174 return (m_data + sizeof(EvtHeader));
175}
176
177// set_message
178
179void EvtMessage::setMsg(char const* msgin, int size, ERecordType type)
180{
181 //initialize message header properly
182 new (m_data) EvtHeader(size + sizeof(EvtHeader), type);
183 struct timeval tv;
184 gettimeofday(&tv, nullptr);
185 this->setTime(tv);
186 if (size > 0)
187 memcpy(m_data + sizeof(EvtHeader), msgin, size);
188}
189
190
Class to manage streamed object.
Definition: EvtMessage.h:59
int dest() const
Get destination IP of message.
Definition: EvtMessage.cc:136
int msg_size() const
Get size of message body.
Definition: EvtMessage.cc:108
int paddedSize() const
Same as size(), but as size of an integer array.
Definition: EvtMessage.cc:99
EvtMessage(char *buf=nullptr)
build EvtMessage from existing buffer (no copy, but does not take ownership).
Definition: EvtMessage.cc:27
ERecordType type() const
Get record type.
Definition: EvtMessage.cc:114
int src() const
Get source IP of message.
Definition: EvtMessage.cc:125
char * m_data
Pointer to the internal EvtMessage buffer.
Definition: EvtMessage.h:148
EvtHeader * header()
Get pointer to EvtHeader.
Definition: EvtMessage.cc:161
char * buffer()
Get buffer address.
Definition: EvtMessage.cc:76
~EvtMessage()
Destructor.
Definition: EvtMessage.cc:56
void setMsg(const char *msg, int size, ERecordType type)
Copy message into newly allocated buffer.
Definition: EvtMessage.cc:179
char * msg()
Get pointer to message body.
Definition: EvtMessage.cc:172
bool m_ownsBuffer
Wether to clean up m_data in destructor.
Definition: EvtMessage.h:149
void setTime(const struct timeval &time)
Set time stamp.
Definition: EvtMessage.cc:154
const EvtHeader * getHeader() const
Get pointer to EvtHeader.
Definition: EvtMessage.cc:166
EvtMessage & operator=(const EvtMessage &obj)
Assignment (m_data is copied).
Definition: EvtMessage.cc:64
int size() const
Get size of message including headers.
Definition: EvtMessage.cc:94
ERecordType
What type of message is this?
Definition: EvtMessage.h:25
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24
STL namespace.
Header structure of streamed object list.
Definition: EvtMessage.h:28