-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponder.c
executable file
·86 lines (74 loc) · 2.58 KB
/
responder.c
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
#include "responder.h"
extern pthread_mutex_t responder_mutex;
extern struct config g_config;
static int current_host_index = 0;
void *responder(void *info)
{
char buffer[1024], output[1024];
struct client_info *ci = (struct client_info *) info;
int sd_client = ci->sd_client;
while (1)
{
memset(buffer, 0, sizeof(buffer));
memset(output, 0, sizeof(output));
if (recv(sd_client, buffer, sizeof(buffer), 0) == -1)
break;
if (strncmp("next", buffer, 4) == 0)
{
if (!g_config.available_count)
{
syslog(LOG_ERR, "no_host_available(1)");
strcpy(output, "error:no_host_available(1)\r\n");
}
else
{
int next_host;
strcpy(output, "next_mysql_server:");
pthread_mutex_lock(&responder_mutex);
if ((next_host = find_available_next_host_index()) == -1)
{
syslog(LOG_ERR, "no_host_available(2)");
strcpy(output, "error:no_host_available(2)\r\n");
}
else
{
strcat(output, g_config.host_infos[next_host]->host_name);
strcat(output, ",");
strcat(output, g_config.host_infos[next_host]->user_name);
strcat(output, ",");
strcat(output, g_config.host_infos[next_host]->password);
strcat(output, ",");
strcat(output, itoa(g_config.host_infos[next_host]->port, 10));
strcat(output, "\r\n");
}
pthread_mutex_unlock(&responder_mutex);
}
}
else if (strncmp(buffer, "quit", 4) == 0)
break;
else
strcpy(output, "error:unknown_command\r\n");
if (send(sd_client, output, strlen(output), 0) == -1)
break;
}
close(sd_client);
return NULL;
}
int find_available_next_host_index(void)
{
int host_check_count = 0;
current_host_index++;
if (current_host_index >= g_config.host_count)
current_host_index = 0;
while (!g_config.host_infos[current_host_index]->status)
{
syslog(LOG_INFO, "finding %ld", time(NULL));
current_host_index++;
host_check_count++;
if (host_check_count >= g_config.host_count)
return -1;
if (current_host_index >= g_config.host_count)
current_host_index = 0;
}
return current_host_index;
}