Newer
Older
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
//============================================================================//
// A C++ wrapper class for C based ET //
// //
// Chao Peng //
// 02/27/2016 //
//============================================================================//
#include "PRadETChannel.h"
#include "PRadETStation.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
using namespace std;
PRadETChannel::PRadETChannel(size_t size)
: curr_stat(nullptr), et_id(nullptr), bufferSize(size)
{
buffer = new uint32_t[bufferSize];
}
PRadETChannel::~PRadETChannel()
{
if(buffer != nullptr)
delete[](buffer), buffer = nullptr;
// force close ET
ForceClose();
}
// Close ET connection
void PRadETChannel::ForceClose()
{
if((et_id != nullptr) && et_alive(et_id)) {
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
et_forcedclose(et_id);
et_id = nullptr;
}
}
// Open ET
void PRadETChannel::Open(const char* ipAddr, int tcpPort, const char* etFile)
{
// Use a direct connection to the ET system
config.SetCast(ET_DIRECT);
// Set the ip address and tcp port
config.SetHost(ipAddr);
config.SetServerPort(tcpPort);
int charSize = strlen(etFile)+1;
char *fileName = new char[charSize];
strncpy(fileName, etFile, charSize);
// Open et client
int status = et_open(&et_id, fileName, config.Get());
delete fileName;
if(status != ET_OK) {
throw(PRadException(PRadException::ET_CONNECT_ERROR, "et_client: cannot open et client!"));
}
/* set level of debug output */
et_system_setdebug(et_id, ET_DEBUG_INFO);
}
void PRadETChannel::NewStation(const string &name)
{
auto it = stations.find(name);
if(it == stations.end()) {
curr_stat = new PRadETStation(this, name);
stations[string(name)] = curr_stat;
}
}
void PRadETChannel::SwitchStation(const string &name)
{
auto it = stations.find(name);
if(it != stations.end()) {
curr_stat = it->second;
} else {
cout << "ET Channel Warning: station " << name << " does not exist!" << endl;
}
}
void PRadETChannel::RemoveStation(const string &name)
{
try {
if(et_id != nullptr && et_alive(et_id)) {
auto it = stations.find(name);
if(it != stations.end()) {
it->second->Remove();
stations.erase(it);
} else {
cout << "ET Channel Warning: station " << name << " does not exist!" << endl;
}
} else {
cout << "ET Channel Warning: cannot remove station while disconnected from ET!" << endl;
}
} catch (PRadException e) {
throw e;
}
}
PRadETStation* PRadETChannel::GetStation(const string &name)
{
auto it = stations.find(name);
if(it != stations.end()) {
return it->second;
} else {
return nullptr;
}
}
// Attach station
void PRadETChannel::AttachStation()
{
try {
curr_stat->Create();
curr_stat->Attach();
} catch (PRadException e) {
throw(e);
}
cout << "Successfully attached to ET!" << endl;
}
// Read one event from ET station, return true if success
bool PRadETChannel::Read()
{
// check if et is opened or alive
if(et_id == nullptr || !et_alive(et_id))
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is not opened or dead!"));
et_att_id att = curr_stat->GetAttachID();
// get the event
int status = et_event_get(et_id, att, &etEvent, ET_ASYNC, nullptr);
switch(status)
{
case ET_OK:
break;
case ET_ERROR_EMPTY:
return false;
case ET_ERROR_DEAD:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is dead!"));
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: got timeout!!"));
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: station is busy!"));
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: someone told me to wake up."));
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: unkown error!"));
// copy the data buffer
copyEvent();
// put back the event
status = et_event_put(et_id, att, etEvent);
switch(status)
{
case ET_OK:
break;
case ET_ERROR_DEAD:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is dead!"));
default:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: unkown error!"));
}
return true;
}
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
bool PRadETChannel::Write(void *buf, int nbytes)
{
// check if et is opened or alive
if(et_id == nullptr || !et_alive(et_id))
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is not opened or dead!"));
et_att_id att = curr_stat->GetAttachID();
int status = et_event_new(et_id, att, &etEvent, ET_SLEEP, nullptr, nbytes);
switch(status) {
case ET_OK:
break;
case ET_ERROR_EMPTY:
return false;
case ET_ERROR_DEAD:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is dead!"));
case ET_ERROR_TIMEOUT:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: got timeout!!"));
case ET_ERROR_BUSY:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: station is busy!"));
case ET_ERROR_WAKEUP:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: someone told me to wake up."));
default:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: unkown error!"));
}
// build et event
void *data;
et_event_getdata(etEvent, &data);
memcpy((void *) data, (const void *) buf, nbytes);
et_event_setlength(etEvent, nbytes);
// put back the event
status = et_event_put(et_id, att, etEvent);
switch(status)
{
case ET_OK:
break;
case ET_ERROR_DEAD:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: et is dead!"));
default:
throw(PRadException(PRadException::ET_READ_ERROR,"et_client: unkown error!"));
}
return true;
}
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
void PRadETChannel::copyEvent()
{
void *data;
et_event_getdata(etEvent, &data);
et_event_getlength(etEvent, &bufferSize);
bufferSize /= 4; // from byte to int32 words
uint32_t *data_buffer = (uint32_t*) data;
size_t index = 0;
// check if it is a block header
if(bufferSize >= 8 && data_buffer[7] == 0xc0da0100) {
index += 8;
bufferSize -= 8;
}
for(size_t i = 0; i < bufferSize; ++i)
{
buffer[i] = data_buffer[index+i];
}
}
// nested config classes
// et_openconfig
PRadETChannel::Configuration::Configuration()
{
Initialize();
}
PRadETChannel::Configuration::~Configuration()
{
et_open_config_destroy(config);
}
// wrapper functions
void PRadETChannel::Configuration::Initialize()
{
et_open_config_init(&config);
}
void PRadETChannel::Configuration::SetWait(int val)
{
et_open_config_setwait(config, val);
}
void PRadETChannel::Configuration::SetTimeOut(struct timespec val)
{
et_open_config_settimeout(config, val);
}
void PRadETChannel::Configuration::SetHost(const char *val)
{
et_open_config_sethost(config, val);
}
void PRadETChannel::Configuration::SetCast(int val)
{
et_open_config_setcast(config, val);
}
void PRadETChannel::Configuration::SetTTL(int val)
{
et_open_config_setTTL(config, val);
}
void PRadETChannel::Configuration::SetPort(unsigned short val)
{
et_open_config_setport(config, val);
}
void PRadETChannel::Configuration::SetServerPort(unsigned short val)
{
et_open_config_setserverport(config, val);
}
void PRadETChannel::Configuration::AddBroadCast(const char *val)
{
et_open_config_addbroadcast(config, val);
}
void PRadETChannel::Configuration::RemoveBroadCast(const char *val)
{
et_open_config_removebroadcast(config, val);
}
void PRadETChannel::Configuration::AddMultiCast(const char *val)
{
et_open_config_addmulticast(config, val);
}
void PRadETChannel::Configuration::RemoveMultiCast(const char *val)
{
et_open_config_removemulticast(config, val);
}
void PRadETChannel::Configuration::SetPolicy(int val)
{
et_open_config_setpolicy(config, val);
}
void PRadETChannel::Configuration::SetMode(int val)
{
et_open_config_setmode(config, val);
}
void PRadETChannel::Configuration::SetDebugDefault(int val)
{
et_open_config_setdebugdefault(config, val);
}
void PRadETChannel::Configuration::SetInterface(const char *val)
{
et_open_config_setinterface(config, val);
}
void PRadETChannel::Configuration::SetTCP(int rBufSize, int sBufSize, int noDelay)
{
et_open_config_settcp(config, rBufSize, sBufSize, noDelay);
}