-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdaqBuffer.cc
353 lines (279 loc) · 7.71 KB
/
daqBuffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <daqBuffer.h>
#include <daqONCSEvent.h>
#include <daqPRDFEvent.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <string.h>
#include <lzo/lzo1x.h>
using namespace std;
int daqBuffer::lzo_initialized = 0;
int readn (int fd, char *ptr, const int nbytes);
int writen (int fd, char *ptr, const int nbytes);
// the constructor first ----------------
daqBuffer::daqBuffer (const int irun, const int length
, const int iseq, md5_state_t *md5state)
{
int *b = new int [length];
bptr = ( buffer_ptr ) b;
data_ptr = &(bptr->data[0]);
max_length = length; // in 32bit units
max_size = max_length;
_broken = 0;
current_event = 0;
current_etype = -1;
// preset everything to ONCS format
format=DAQONCSFORMAT;
currentBufferID = ONCSBUFFERHEADER;
_md5state = md5state;
wants_compression = 0;
md5_enabled = 1;
wrkmem = 0;
outputarraylength = 0;
outputarray = 0;
prepare_next (iseq, irun);
}
// the destructor... ----------------
daqBuffer::~daqBuffer ()
{
int *b = (int *) bptr;
delete [] b;
if (outputarray) delete [] outputarray;
if (wrkmem) delete [] wrkmem;
}
int daqBuffer::prepare_next( const int iseq
, const int irun)
{
// cout << __FILE__ << " " << __LINE__ << " bptr: " << bptr << endl;
if ( current_event) delete current_event;
current_event = 0;
// re-initialize the event header length
bptr->Length = BUFFERHEADERLENGTH*4;
bptr->ID = currentBufferID;
bptr->Bufseq = iseq;
if (irun>0) bptr->Runnr = irun;
current_index = 0;
left = max_size - BUFFERHEADERLENGTH - EOBLENGTH;
has_end = 0;
return 0;
}
// ---------------------------------------------------------
int daqBuffer::nextEvent(const int etype, const int evtseq, const int evtsize)
{
if (current_event) delete current_event;
current_event = 0;
current_etype = -1;
if (evtsize > left-EOBLENGTH) return -1;
if ( format)
{
current_event = new daqPRDFEvent(&(bptr->data[current_index]), evtsize
,bptr->Runnr, etype, evtseq);
left -= (EVTHEADERLENGTH + 8);
current_index += EVTHEADERLENGTH+8;
bptr->Length += (EVTHEADERLENGTH+8)*4;
}
else
{
current_event = new daqONCSEvent(&(bptr->data[current_index]), evtsize
,bptr->Runnr, etype, evtseq);
left -= EVTHEADERLENGTH;
current_index += EVTHEADERLENGTH;
bptr->Length += EVTHEADERLENGTH*4;
}
current_etype = etype;
return 0;
}
// ----------------------------------------------------------
unsigned int daqBuffer::addSubevent( daq_device *dev)
{
unsigned int len;
len = current_event->addSubevent(current_etype, dev);
left -= len;
current_index += len;
bptr->Length += len*4;
return len;
}
// ----------------------------------------------------------
unsigned int daqBuffer::addEoB()
{
if (has_end) return -1;
bptr->data[current_index++] = 2;
bptr->data[current_index++] = 0;
bptr->Length += 2*4;
has_end = 1;
if ( current_event) delete current_event;
current_event = 0;
return 0;
}
// ----------------------------------------------------------
// int daqBuffer::transfer(dataProtocol * protocol)
// {
// if (protocol)
// return protocol->transfer((char *) bptr, bptr->Length);
// else
// return 0;
// }
unsigned int daqBuffer::writeout ( int fd)
{
if ( _broken) return 0;
if (!has_end) addEoB();
unsigned int bytes = 0;;
if ( ! wants_compression)
{
int blockcount = ( getLength() + 8192 -1)/8192;
int bytecount = blockcount*8192;
bytes = writen ( fd, (char *) bptr , bytecount );
if ( _md5state && md5_enabled)
{
//cout << __FILE__ << " " << __LINE__ << " updating md5 with " << bytes << " bytes" << endl;
md5_append(_md5state, (const md5_byte_t *)bptr,bytes );
}
return bytes;
}
else // we want compression
{
compress();
int blockcount = ( outputarray[0] + 8192 -1)/8192;
int bytecount = blockcount*8192;
bytes = writen ( fd, (char *) outputarray , bytecount );
if ( _md5state && md5_enabled)
{
//cout << __FILE__ << " " << __LINE__ << " updating md5 with " << bytes << " bytes" << endl;
md5_append(_md5state, (const md5_byte_t *)outputarray,bytes );
}
return bytes;
}
}
#define ACKVALUE 101
unsigned int daqBuffer::sendout ( int fd )
{
if ( _broken) return 0;
if (!has_end) addEoB();
int total = getLength();
//std::cout << __FILE__ << " " << __LINE__ << " sending opcode ctrl_data" << CTRL_DATA << std::endl ;
// send "CTRL_DATA" opcode in network byte ordering
int opcode = htonl(CTRL_DATA);
int status = writen(fd, (char *) &opcode, sizeof(int));
// std::cout << __FILE__ << " " << __LINE__ << " sending buffer size " << total << std::endl ;
// re-use variable to send the length in network byte ordering
opcode = htonl(total);
status |= writen(fd, (char *) &opcode, sizeof(int));
// now send the actual data
char *p = (char *) bptr;
int sent = writen(fd,p,total);
// wait for acknowledge... we re-use the opcode variable once more
readn (fd, (char *) &opcode, sizeof(int));
opcode = ntohl(opcode);
if ( opcode != CTRL_REMOTESUCCESS) return -1; // signal error
return sent;
}
// this is sending the monitoring data to a client
unsigned int daqBuffer::sendData ( int fd, const int max_length)
{
if ( _broken) return 0;
if (!has_end) addEoB();
int total = getLength();
if ( total > max_length)
{
cout << "Monitoring: data size exceeds limit -- " << total << " limit: " << max_length << endl;
total = max_length;
}
int ntotal = htonl(total);
int status = writen(fd, (char *) &ntotal, 4);
char *p = (char *) bptr;
int sent = writen(fd,p,total);
return sent;
}
int daqBuffer::setCompression(const int flag)
{
if ( !flag)
{
wants_compression = 0;
return 0;
}
else
{
if ( ! lzo_initialized )
{
if (lzo_init() != LZO_E_OK)
{
std::cerr << "Could not initialize LZO" << std::endl;
_broken = 1;
}
lzo_initialized = 1;
}
if ( !wrkmem)
{
wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_12_MEM_COMPRESS);
if (wrkmem)
{
memset(wrkmem, 0, LZO1X_1_12_MEM_COMPRESS);
}
else
{
std::cerr << "Could not allocate LZO memory" << std::endl;
_broken = 1;
return -1;
}
outputarraylength = max_length + 8192;
outputarray = new unsigned int[outputarraylength];
}
wants_compression = 1;
//cout << " LZO compression enabled" << endl;
return 0;
}
}
int daqBuffer::compress ()
{
if ( _broken) return -1;
lzo_uint outputlength_in_bytes = outputarraylength*4-16;
lzo_uint in_len = getLength();
lzo1x_1_12_compress( (lzo_byte *) bptr,
in_len,
(lzo_byte *)&outputarray[4],
&outputlength_in_bytes,wrkmem);
outputarray[0] = outputlength_in_bytes +4*BUFFERHEADERLENGTH;
outputarray[1] = LZO1XBUFFERMARKER;
outputarray[2] = bptr->Bufseq;
outputarray[3] = getLength();
return 0;
}
// ----------------------------------------------------------
int daqBuffer::setMaxSize(const int size)
{
if (size < 0) return -1;
if (size == 0) max_size = max_length;
else
{
max_size = (size + 8191)/8192;
max_size *= 2048;
if (max_size > max_length)
{
max_size = max_length;
return -2;
}
}
return 0;
}
// ----------------------------------------------------------
int daqBuffer::getMaxSize() const
{
return max_size*4;
}
int daqBuffer::setEventFormat(const int f)
{
if (f)
{
format = DAQPRDFFORMAT;
currentBufferID = PRDFBUFFERHEADER;
}
else
{
format = DAQONCSFORMAT;
currentBufferID = ONCSBUFFERHEADER;
}
bptr->ID = currentBufferID;
return 0;
}