70,020
社区成员




int
main(int argc, char *argv[])
{
struct timeval start;
struct timeval end;
unsigned long timer;
gettimeofday(&start, NULL);
welcome();
if (sql_login(argv[1], argv[2], argv[3]))
fprintf(stdout, "Welcome to Donkey\n");
else
{
fprintf(stdout, "Login MySQL failure\n");
exit(0);
}
struct DOMAIN domain[PTHREAD_NUM];
pthread_t tid[PTHREAD_NUM];
int res;
MYSQL_RES *result = NULL;
MYSQL_ROW row = NULL;
MYSQL_FIELD *fields;
int exe_num;
unsigned int i = 0;
unsigned int num_fields = 0;
#ifdef DEBUG
unsigned int exe_time = 0;
#endif
int m;
struct tm *now;
res = pthread_mutex_init(&mutext, NULL);
while (1)
{
fprintf(stdout, "S-------T-----------A---------R--------T---->\n");
memset(domain, 0, sizeof(domain));
// update the time_out
result = exe_sql("select distinct user_id, domain, ip, status, last_check from domain");
exe_num = mysql_num_rows(result);
fprintf(stdout, "select num rows: %d\n", exe_num);
timeout = exe_num / PTHREAD_NUM;
if ((timeout > 10) || (timeout < 1) )
timeout = 10;
fprintf(stdout, "timeout %d\n", timeout);
// Get info from MySQL
num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
#ifdef DEBUG
int count = 0;
#endif
i = 0;
while ((row = mysql_fetch_row(result)) != NULL)
{
domain[i].user_id = atoi(row[0]);
sprintf(domain[i].domain, "%s", row[1]);
sprintf(domain[i].ip, "%s", row[2]);
sprintf(domain[i].status, "%s", row[3]);
strncpy(domain[i].last_check, row[4], 16);
strcat(domain[i].last_check, "\0");
#ifdef DEBUG
fprintf(stdout, "%-3d:user_id %d domain %-30s ip %-10s status %s last_check %s\n",
count, domain[i].user_id, domain[i].domain, domain[i].ip, domain[i].status, domain[i].last_check);
count++;
#endif
i++;
if (i == PTHREAD_NUM)
{
// get current time
now = getlocaltime();
#ifdef DEBUG
fprintf(stdout, "%d now time: %d-%d-%d %d:%d:%d\n",
count, now->tm_year+1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
#endif
// check the status
for (m = 0; m < PTHREAD_NUM; m++)
if (comparetime(*now, domain[m].last_check, timeout))
if ((res = pthread_create(&tid[m], NULL, check, (void *)&domain[m] )) != 0)
fprintf(stderr, "pthread_create:%s\n", strerror(res));
for (m = 0; m < PTHREAD_NUM; m++)
res = pthread_join(tid[m], NULL);
i = 0;
memset(domain, 0, sizeof(domain));
}
}
// check the status
for (m = 0; m < i; m++)
if (comparetime(*now, domain[m].last_check, timeout))
if ((res = pthread_create(&tid[m], NULL, check, (void *)&domain[m] )) != 0)
fprintf(stderr, "pthread_create:%s\n", strerror(res));
for (m = 0; m < i; m++)
res = pthread_join(tid[m], NULL);
mysql_free_result(result);
fprintf(stdout, "E-------------------N------------------D---->\n");
}
gettimeofday(&end,NULL);
timer = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
fprintf(stdout, "Goodbye Dokey runs :SQL Number [%d] Time [%ld] us\n", exe_num, timer);
return 0;
}