-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtavant.cxx
188 lines (153 loc) · 4.01 KB
/
dtavant.cxx
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
/*
*
*/
#include <iostream>
#include <iomanip>
#include <thread>
#include <atomic>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include "zmq.hpp"
#include "koltcp.h"
#include "daqtask.cxx"
const int default_bufsize = 1024 * sizeof(unsigned int);
const int default_quelen = 10000;
std::atomic<int> g_avant_depth(0);
//const char* g_snd_endpoint = "tcp://localhost:5558";
//const char* g_snd_endpoint = "ipc://./hello";
//const char* g_snd_endpoint = "inproc://hello";
void buf_free(void *buf, void *hint)
{
char *bufbuf = reinterpret_cast<char *>(buf);
delete bufbuf;
g_avant_depth--;
}
class DTavant : public DAQTask
{
public:
DTavant(int i) : DAQTask(i) {};
DTavant(int, char *, int, bool);
int get_bufsize() {return m_bufsize;};
void set_bufsize(int size) {m_bufsize = size;};
int get_quelen() {return m_quelen;};
void set_quelen(int len) {m_quelen = len;};
protected:
virtual int st_init(void *) override;
virtual int st_idle(void *) override;
virtual int st_running(void *) override;
private:
char *m_host;
int m_port;
kol::TcpClient *tcp;
bool m_is_dummy;
int m_bufsize;
int m_quelen;
};
DTavant::DTavant(int i, char *host, int port, bool is_dummy)
: DAQTask(i), m_host(host), m_port(port),
m_is_dummy(is_dummy), m_bufsize(default_bufsize), m_quelen(default_quelen)
{
}
int DTavant::st_init(void *context)
{
{
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "avant(" << m_id << ") init" << std::endl;
}
if (! m_is_dummy) {
try {
tcp = new kol::TcpClient(m_host, m_port);
} catch(kol::SocketException &e) {
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "Socket open error. (" << m_host << ", " << m_port
<< ") " << e.what() << std::endl;
m_is_good = false;
return 1;
}
} else {
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "Dummy mode" << std::endl;
}
return 0;
}
int DTavant::st_idle(void *context)
{
#if 0
{
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "avant(" << m_id << ") idle" << std::endl;
}
#endif
usleep(100000);
return 0;
}
int DTavant::st_running(void *context)
{
{
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "avant(" << m_id << ") running" << std::endl;
}
zmq::socket_t sender(
*(reinterpret_cast<zmq::context_t *>(context)),
ZMQ_PUSH);
//sender.setsockopt(ZMQ_SNDBUF, m_bufsize + 8); //ireruto tcp no toki osokunaru ??
//sender.setsockopt(ZMQ_SNDHWM, 512*1024);
sender.setsockopt(ZMQ_SNDHWM, m_quelen);
#if 1
std::cout << "avant: ZMQ_SNDBUF : " << sender.getsockopt<int>(ZMQ_SNDBUF) << std::endl;
std::cout << "avant: ZMQ_SNDHWM : " << sender.getsockopt<int>(ZMQ_SNDHWM) << std::endl;
#endif
sender.connect(g_snd_endpoint);
int segnum = 0;
while (1) {
if (c_state != SM_RUNNING) break;
char *buf;
try {
buf = new char[m_bufsize + sizeof(unsigned int)];
} catch (std::exception &e){
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cerr << "avant; Memory allocation fail. " << e.what() << std:: endl;
return -1;
}
g_avant_depth++;
unsigned int *head = reinterpret_cast<unsigned int *>(buf);
*head = 0xff000000 | m_id;
char *data = reinterpret_cast<char *>(head + 1);
int nread = 0;
if (! m_is_dummy) {
try {
tcp->read(data, m_bufsize);
} catch (kol::SocketException &e) {
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cerr << "#E tcp read err. " << e.what() << std::endl;
break;
}
nread = tcp->gcount();
} else {
for (int i = 0 ; i < m_bufsize ; i++) data[i] = i & 0xff;
nread = m_bufsize;
}
zmq::message_t message(
reinterpret_cast<void *>(buf),
nread + sizeof(unsigned int),
buf_free,
NULL);
try {
sender.send(message);
} catch (zmq::error_t &e) {
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cerr << "#E send zmq err. " << e.what() << std::endl;
return -1;
}
#if 1
if ((segnum % 1000) == 0) {
std::lock_guard<std::mutex> lock(*c_dtmtx);
std::cout << "\rLSQue: "
<< std::setw(4) << g_avant_depth << " " << std::flush;
}
#endif
segnum++;
}
return 0;
}