-
Notifications
You must be signed in to change notification settings - Fork 0
/
slave_thread.cpp
394 lines (330 loc) · 10.4 KB
/
slave_thread.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "slave_thread.h"
#include "udsrf.h"
#include <signal.h>
#include "plugin_architecture_api.h"
#include "plugin_architecture_interface.h"
#include <pthread.h>
#include <memory>
#include "file_logger_plugin.h"
#include <iostream>
#include <vector>
#include "mysql_plugin.h"
#include "pgsql_plugin.h"
#include "tunables.h"
#include "slave_thread_auto.h"
pthread_t thread_slow;
pthread_t thread_fast;
pthread_cond_t wait_for_plugins = PTHREAD_COND_INITIALIZER;
pthread_mutex_t wait_for_plugins_mutex = PTHREAD_MUTEX_INITIALIZER;
bool plugin_created = false;
static volatile bool slow_runs = true;
static volatile bool fast_runs = true;
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
static volatile int slow_threads = 0;
static volatile int fast_threads = 0;
static std::unexpected_handler old_unexpected_handler;
static std::terminate_handler old_terminate_handler;
VSFPluginArchitectureSingleton pluginArchitecture;
static void * slow_event_loop(void *);
static void * fast_event_loop(void *);
static void *slow_listener(void * v_un);
static void *fast_listener(void * v_un);
extern "C" void handle_slave_signal();
void handle_signal(int);
static void handle_unexpected_exception();
static void handle_terminate();
void slave_thread_start(struct sockaddr_un server_address_slow, struct sockaddr_un server_address_fast);
extern "C" void handle_slave_signal() {
slow_runs = false;
fast_runs = false;
struct Event_ServQuit sigend;
try {
send_event_servQuit(&sigend);
} catch (...) {} //can happen you know..
}
void handle_signal(int) {
handle_slave_signal();
}
static void handle_unexpected_exception() {
try {
throw;
} catch (std::exception & e) {
cerr << "Caught an exception (" << typeid(e).name() << ")\n";
}
catch (...) {
cerr << "Caught an unknown exception\n";
}
printf("unexpected exception!!\n");
handle_slave_signal();
old_unexpected_handler();
}
static void handle_terminate() {
try {
throw;
} catch (std::exception & e) {
cerr << "Caught an exception (" << typeid(e).name() << ")\n";
}
catch (...) {
cerr << "Caught an unknown exception\n";
}
printf("unexpected terminate!!\n");
handle_slave_signal();
old_terminate_handler();
}
void slave_thread_start(struct sockaddr_un server_address_slow, struct sockaddr_un server_address_fast) {
std::auto_ptr<VSFPlugin> currentPlugin;
signal(SIGHUP, handle_signal);
signal(SIGINT, handle_signal);
signal(SIGQUIT, handle_signal);
old_unexpected_handler = std::set_unexpected(handle_unexpected_exception);
old_terminate_handler = std::set_terminate(handle_terminate);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_mutex_init(&count_mutex, 0);
if (pthread_create(&thread_slow,&attr,slow_listener,&server_address_slow))
{
printf("pthread_create(&thread_slow,&attr,slow_listener,&server_address_slow) failed!!\n");
exit(0);
}
if (pthread_create(&thread_fast,&attr,fast_listener,&server_address_fast))
{
printf("pthread_create(&thread_fast,&attr,fast_listener,&server_address_fast) failed!!\n");
exit(0);
}
currentPlugin = createCurrentSQLPlugin(pluginArchitecture);
pthread_mutex_lock(&wait_for_plugins_mutex);
plugin_created = true;
pthread_cond_broadcast(&wait_for_plugins);
pthread_mutex_unlock(&wait_for_plugins_mutex);
while(slow_runs || fast_runs || slow_threads > 0 || fast_threads > 0) {
sleep(1);
}
pthread_mutex_destroy(&count_mutex);
}
static void *slow_listener(void * v_un) {
struct sockaddr_un * slow_un = ((struct sockaddr_un*)v_un);
int socket_to_main_slow;
printf("trying to create a PF_UNIX, SOCK_SEQPACKET socket\n");
socket_to_main_slow = socket(PF_UNIX, SOCK_SEQPACKET, 0);
printf("slow listener at work\n");
if (bind(socket_to_main_slow, (struct sockaddr*)slow_un, sizeof(struct sockaddr_un)))
{
printf("bind error\n");
perror(0);
pthread_exit(0);
} else {
printf("bind ok\n");
}
if (listen(socket_to_main_slow,20))
{
printf("listen error\n");
perror(0);
pthread_exit(0);
} else {
printf("listen ok\n");
}
pthread_mutex_lock(&wait_for_plugins_mutex);
if (!plugin_created)
pthread_cond_wait(&wait_for_plugins, &wait_for_plugins_mutex);
pthread_mutex_unlock(&wait_for_plugins_mutex);
printf("<slow listener> entering accept loop\n");
while(slow_runs)
{
int * newSock = new int;
socklen_t sockaddr_len = 0;
struct sockaddr_un addr;
*newSock = accept(socket_to_main_slow, (struct sockaddr*)&addr, &sockaddr_len);
pthread_attr_t attr;
pthread_attr_init(&attr);
struct sched_param sparam;
sparam.sched_priority = 0;
pthread_attr_setschedparam(&attr, &sparam);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (*newSock == -1)
{
printf("accept error\n");
perror(0);
pthread_exit(0);
}
pthread_t thread;
if(pthread_create(&thread, &attr, slow_event_loop, newSock) == 0)
{
pthread_mutex_lock(&count_mutex);
slow_threads++;
pthread_mutex_unlock(&count_mutex);
}
}
pthread_exit(0);
}
static void *fast_listener(void * v_un) {
struct sockaddr_un * fast_un = ((struct sockaddr_un*)v_un);
int socket_to_main_fast;
printf("trying to create a PF_UNIX, SOCK_SEQPACKET socket\n");
socket_to_main_fast = socket(PF_UNIX, SOCK_SEQPACKET, 0);
printf("fast listener at work\n");
if (bind(socket_to_main_fast, (struct sockaddr*)fast_un, sizeof(struct sockaddr_un)))
{
printf("bind error\n");
perror(0);
pthread_exit(0);
}
if (listen(socket_to_main_fast,20))
{
printf("listen error\n");
perror(0);
pthread_exit(0);
}
pthread_mutex_lock(&wait_for_plugins_mutex);
if (!plugin_created)
pthread_cond_wait(&wait_for_plugins, &wait_for_plugins_mutex);
pthread_mutex_unlock(&wait_for_plugins_mutex);
printf("<fast listener> entering accept loop\n");
while (fast_runs)
{
int * newSock = new int;
socklen_t sockaddr_len = 0;
struct sockaddr_un addr;
*newSock = accept(socket_to_main_fast, (struct sockaddr*)&addr, &sockaddr_len);
pthread_attr_t attr;
pthread_attr_init(&attr);
struct sched_param sparam;
sparam.sched_priority = -15;
pthread_attr_setschedparam(&attr, &sparam);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (*newSock == -1)
{
printf("accept error\n");
perror(0);
pthread_exit(0);
}
pthread_t thread;
if (pthread_create(&thread, &attr, fast_event_loop, newSock) == 0)
{
pthread_mutex_lock(&count_mutex);
fast_threads++;
pthread_mutex_unlock(&count_mutex);
}
}
pthread_exit(0);
}
static void *slow_event_loop(void * v_sock) {
int socket = *((int*)v_sock);
delete (int*)v_sock;
bool running = true;
//proces iteratively
while (running)
{
int number;
try {
recvData(socket, number);
}
catch(SendRecvException & e) {
printf("<slow eloop> error in recv, bye\n");
goto end_thread;
}
switch (number & 0x7F000000)
{
case Event_Identifier: //event
{
std::auto_ptr<Event> event;
event = Event::readFromSocket(socket);
if (event->getNumber() == Event_Number_CliEnd)
{
running = false;
}
if (event->getNumber() == Event_Number_ServQuit)
{
running = false;
slow_runs = false;
fast_runs = false;
}
//printf("event number: %xd\n", event->getNumber());
//printf("doing: pluginArchitecture.getVSFPluginArchitecture().handleEvent(*event); \n");
pluginArchitecture.getVSFPluginArchitecture().handleEvent(*event);
//printf("phew, done :)\n");
}
break;
case Request_Identifier: //request
{
std::auto_ptr<Request> request;
request = Request::readFromSocket(socket);
printf("slow: request number: %xd\n", request->getNumber());
printf("doing: pluginArchitecture.getVSFPluginArchitecture().handleRequest(*request); \n");
pluginArchitecture.getVSFPluginArchitecture().handleRequest(*request).writeToSocket(socket);
printf("phew, done :)\n");
}
break;
default:
printf("unknown magic number in event/request\n");
handle_slave_signal();
goto end_thread;
break;
}//switch
}//while
end_thread:
pthread_mutex_lock(&count_mutex);
slow_threads--;
pthread_mutex_unlock(&count_mutex);
pthread_exit(0);
}
static void *fast_event_loop(void * v_sock) {
int socket = *((int*)v_sock);
delete (int*)v_sock;
bool running = true;
while (running)
{
int number;
try {
recvData(socket, number);
} catch(SendRecvException & e) {
printf("<fast eloop> error in recv, bye\n");
goto end_thread;
}
switch (number & 0x7F000000)
{
case Event_Identifier: //event
{
std::auto_ptr<Event> event;
event = Event::readFromSocket(socket);
if (event->getNumber() == Event_Number_CliEnd)
{
running = false;
break;
}
if (event->getNumber() == Event_Number_ServQuit)
{
running = false;
slow_runs = false;
fast_runs = false;
break;
}
//printf("event number: %xd\n", event->getNumber());
//printf("doing: pluginArchitecture.getVSFPluginArchitecture().handleEvent(*event); \n");
pluginArchitecture.getVSFPluginArchitecture().handleEvent(*event);
//printf("phew, done :)\n");
}
break;
case Request_Identifier: //request
{
std::auto_ptr<Request> request;
request = Request::readFromSocket(socket);
printf("fast: request number: %xd\n", request->getNumber());
printf("doing: pluginArchitecture.getVSFPluginArchitecture().handleRequest(*request); \n");
pluginArchitecture.getVSFPluginArchitecture().handleRequest(*request).writeToSocket(socket);
printf("phew, done :)\n");
}
break;
default:
printf("unknown magic number in event/request\n");
handle_slave_signal();
goto end_thread;
break;
}//switch
}//while
end_thread:
pthread_mutex_lock(&count_mutex);
fast_threads--;
pthread_mutex_unlock(&count_mutex);
pthread_exit(0);
}