-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmbus-agent.c
More file actions
259 lines (220 loc) · 6.54 KB
/
mbus-agent.c
File metadata and controls
259 lines (220 loc) · 6.54 KB
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <time.h>
#include "mbus-agent.h"
#include "aspp.h"
#include "cfg.h"
#include "rtu.h"
static struct cfg *config = NULL;
void *rtu_thread(void *arg)
{
int ep;
struct rtu_desc *ri;
uint8_t buf[BUF_SIZE];
struct epoll_event *evs;
struct cfg *cfg = (struct cfg *)arg;
ep = epoll_create(VLEN(cfg->rtu_list));
if (ep == -1) {
perror("epoll_create() failed");
return NULL;
}
evs = malloc(sizeof(struct epoll_event) * VLEN(cfg->rtu_list));
VFOREACH(cfg->rtu_list, ri) {
rtu_open(ri, ep);
}
for (;;) {
int n;
time_t cur_time;
int nfds = epoll_wait(ep, evs, VLEN(cfg->rtu_list), 10);
if (nfds == -1) {
if (errno == EINTR)
continue;
perror("epoll_wait(rtu) failed");
goto err;
}
#if 0
if (getsockopt (socketFD, SOL_SOCKET, SO_ERROR, &retVal, &retValLen) < 0)
{
// ERROR, fail somehow, close socket
}
if (retVal != 0)
{
// ERROR: connect did not "go through"
}
#endif
/* Process RTU data */
for (n = 0; n < nfds; ++n) {
int len;
if (!(evs[n].events & EPOLLIN)) {
if (evs[n].events & EPOLLHUP) {
goto reconnect;
} else if (evs[n].events & EPOLLOUT) {
// epoll_ctl(self->ep, EPOLL_CTL_DEL, evs[n].data.fd, NULL);
// printf("ev=%x\n", evs[n].events);
}
continue;
}
len = read(evs[n].data.fd, buf, BUF_SIZE);
if (len <= 0) {
reconnect:
/* TODO: reset "retries" counters after a delay */
/* Re-open required */
fprintf(stderr, "Read failed (%d), trying to re-open %d\n",
errno, ri->fd);
rtu_close(ri, ep);
rtu_open(ri, ep);
continue;
}
DEBUGF("Read %d from %d\n", len, evs[n].data.fd);
/* Process RealCOM command */
if (ri->type == REALCOM && ri->fd != evs[n].data.fd) {
printf("Process CMD %d (%d,%d)\n", len, ri->fd, evs[n].data.fd);
realcom_process_cmd(ri, buf, len);
continue;
}
}
cur_time = time(NULL);
}
err:
return NULL;
}
void *tcp_thread(void *p)
{
int n;
uint8_t buf[BUF_SIZE];
struct workers *self = (struct workers *)p;
struct epoll_event evs[MAX_EVENTS];
self->ep = epoll_create(MAX_EVENTS);
if (self->ep == -1) {
perror("epoll_create() failed");
return NULL;
}
/* Main loop */
for (;;) {
int nfds = epoll_wait(self->ep, evs, MAX_EVENTS, -1);
if (nfds == -1) {
if (errno == EINTR)
continue;
perror("epoll_wait(tcp) failed");
goto err;
}
for (n = 0; n < nfds; ++n) {
int len;
if (!(evs[n].events & EPOLLIN))
continue;
len = read(evs[n].data.fd, buf, BUF_SIZE);
if (len == 0) {
//c_close:
epoll_ctl(self->ep, EPOLL_CTL_DEL, evs[n].data.fd, NULL);
close(evs[n].data.fd);
} else if (len < 0) {
perror("Error occured");
} else {
#if 0
char ans[] = "HTTP/1.1 200 OK\r\nServer: fake/1.0\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<html>OK</html>\r\n";
int sz = sizeof(ans);
write(evs[n].data.fd, ans, sz);
#endif
// fprintf(stderr, "%d Read %d bytes from %d\n", self->n, len, evs[n].data.fd);
// goto c_close;
}
}
}
err:
close(self->ep);
return NULL;
}
int main(int argc, char **argv)
{
int c;
int n;
int ep;
int cur_child = 0;
pthread_t rtu_proc;
pthread_attr_t attr;
struct rtu_desc *ri;
struct epoll_event ev;
struct epoll_event *evs;
static struct workers *workers;
config = cfg_load("mbus.conf");
if (!config) {
return 1;
}
ep = epoll_create(VLEN(config->rtu_list));
if (ep == -1) {
perror("epoll_create() failed");
return 1;
}
evs = malloc(sizeof(struct epoll_event) * VLEN(config->rtu_list));
VFOREACH(config->rtu_list, ri) {
rtu_open(ri, ep);
}
#if 0
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&rtu_proc, &attr, rtu_thread, cfg) < 0) {
perror("pthread_create() failed");
return 2;
}
childs = malloc(sizeof(struct childs) * cfg->workers);
for (n = 0; n < cfg->workers; ++n) {
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
childs[n].n = n;
childs[n].cfg = cfg;
if (pthread_create(&childs[n].th, &attr, tcp_thread, &childs[n]) < 0) {
perror("pthread_create() failed");
return 2;
}
}
for (;;) {
struct sockaddr_in6 local;
socklen_t addrlen = sizeof(local);
int nfds;
nfds = epoll_wait(ep, evs, 1, -1);
if (nfds == -1) {
if (errno == EINTR)
continue;
perror("epoll_wait(main) failed");
return 2;
} else if (nfds == 0) {
continue;
}
for (n = 0; n < nfds; ++n) {
if (!(evs[n].events & EPOLLIN))
continue;
// fprintf(stderr, "%d events=%d\n", nfds, evs[0].events);
c = accept(evs[n].data.fd, (struct sockaddr *)&local, &addrlen);
if (setnonblocking(c) < 0) {
perror("setnonblocking()");
close(c);
} else {
ev.events = EPOLLIN;
ev.data.fd = c;
// fprintf(stderr, "%d Adding(%d) %d %d\n", ep, i, c, ((struct sockaddr_in6 *)&local)->sin6_port);
if (epoll_ctl(childs[cur_child++].ep, EPOLL_CTL_ADD, c, &ev) < 0) {
perror("epoll_ctl ADD()");
close(c);
}
cur_child %= cfg->workers;
}
}
}
#endif
return 0;
}