mirror of
https://github.com/lxsang/ant-http
synced 2024-12-26 08:48:21 +01:00
add data validity to scheduler
This commit is contained in:
parent
5155e9e0f5
commit
52ede859bd
@ -159,7 +159,7 @@ void *accept_request(void *data)
|
|||||||
antd_task_t *task;
|
antd_task_t *task;
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
|
|
||||||
task = antd_create_task(NULL, (void *)rq, NULL);
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
fd_set read_flags, write_flags;
|
fd_set read_flags, write_flags;
|
||||||
// first verify if the socket is ready
|
// first verify if the socket is ready
|
||||||
@ -180,7 +180,7 @@ void *accept_request(void *data)
|
|||||||
}
|
}
|
||||||
if (sel == 0 || (!FD_ISSET(client->sock, &read_flags) && !FD_ISSET(client->sock, &write_flags)))
|
if (sel == 0 || (!FD_ISSET(client->sock, &read_flags) && !FD_ISSET(client->sock, &write_flags)))
|
||||||
{
|
{
|
||||||
if(client->last_wait == 0) client->last_wait = time(NULL);
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
||||||
// retry it later
|
// retry it later
|
||||||
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
||||||
{
|
{
|
||||||
@ -188,9 +188,8 @@ void *accept_request(void *data)
|
|||||||
server_config.connection++;
|
server_config.connection++;
|
||||||
unknow(rq->client);
|
unknow(rq->client);
|
||||||
return task;
|
return task;
|
||||||
}
|
}*/
|
||||||
task->handle = accept_request;
|
task->handle = accept_request;
|
||||||
task->status = TASK_ACCEPT_PEND;
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
// perform the ssl handshake if enabled
|
// perform the ssl handshake if enabled
|
||||||
@ -208,48 +207,49 @@ void *accept_request(void *data)
|
|||||||
case SSL_ERROR_WANT_WRITE:
|
case SSL_ERROR_WANT_WRITE:
|
||||||
case SSL_ERROR_NONE:
|
case SSL_ERROR_NONE:
|
||||||
//LOG("RETRY SSL %d\n", client->sock);
|
//LOG("RETRY SSL %d\n", client->sock);
|
||||||
task->status = TASK_ACCEPT_SSL_CONT;
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
||||||
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
||||||
|
{
|
||||||
|
server_config.connection++;
|
||||||
|
unknow(rq->client);
|
||||||
|
LOG("SSL timeout, give up on %d\n", client->sock);
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
task->status = TASK_ACCEPT_SSL_CONT;*/
|
||||||
task->handle = accept_request;
|
task->handle = accept_request;
|
||||||
//task->priority = HIGH_PRIORITY;
|
|
||||||
//task->type = LIGHT;
|
|
||||||
return task;
|
return task;
|
||||||
default:
|
default:
|
||||||
LOG("Error performing SSL handshake %d %d %lu\n", stat, ret, ERR_get_error());
|
LOG("Error performing SSL handshake %d %d %lu\n", stat, ret, ERR_get_error());
|
||||||
server_config.connection++;
|
//server_config.connection++;
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
unknow(rq->client);
|
unknow(rq->client);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client->status = 1;
|
client->status = 1;
|
||||||
// reset the waiting
|
|
||||||
client->last_wait = 0;
|
|
||||||
task->handle = accept_request;
|
task->handle = accept_request;
|
||||||
task->status = TASK_ACCEPT_HS_DONE;
|
//LOG("Handshake finish for %d\n", client->sock);
|
||||||
LOG("Handshake finish for %d\n", client->sock);
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!FD_ISSET(client->sock, &read_flags))
|
if (!FD_ISSET(client->sock, &read_flags))
|
||||||
{
|
{
|
||||||
if(client->last_wait == 0) client->last_wait = time(NULL);
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
||||||
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
||||||
{
|
{
|
||||||
server_config.connection++;
|
server_config.connection++;
|
||||||
unknow(rq->client);
|
unknow(rq->client);
|
||||||
LOG("Read timeout, give up on %d\n", client->sock);
|
LOG("Read timeout, give up on %d\n", client->sock);
|
||||||
return task;
|
return task;
|
||||||
}
|
}*/
|
||||||
task->handle = accept_request;
|
task->handle = accept_request;
|
||||||
task->status = TASK_ACCEPT_READWAIT;
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
LOG("Ready for reading %d\n", client->sock);
|
LOG("Ready for reading %d\n", client->sock);
|
||||||
client->last_wait = time(NULL);
|
//server_config.connection++;
|
||||||
server_config.connection++;
|
|
||||||
read_buf(rq->client, buf, sizeof(buf));
|
read_buf(rq->client, buf, sizeof(buf));
|
||||||
line = buf;
|
line = buf;
|
||||||
// get the method string
|
// get the method string
|
||||||
@ -283,7 +283,6 @@ void *accept_request(void *data)
|
|||||||
// decode request
|
// decode request
|
||||||
// now return the task
|
// now return the task
|
||||||
task->handle = decode_request_header;
|
task->handle = decode_request_header;
|
||||||
task->status = TASK_DECODE_HEADER;
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,7 +291,7 @@ void *resolve_request(void *data)
|
|||||||
struct stat st;
|
struct stat st;
|
||||||
char path[2 * BUFFLEN];
|
char path[2 * BUFFLEN];
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
char *url = (char *)dvalue(rq->request, "RESOURCE_PATH");
|
char *url = (char *)dvalue(rq->request, "RESOURCE_PATH");
|
||||||
char *newurl = NULL;
|
char *newurl = NULL;
|
||||||
@ -383,7 +382,6 @@ void *resolve_request(void *data)
|
|||||||
{
|
{
|
||||||
task->type = HEAVY;
|
task->type = HEAVY;
|
||||||
task->handle = serve_file;
|
task->handle = serve_file;
|
||||||
task->status = TASK_SERVE_FILE;
|
|
||||||
}
|
}
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
@ -480,7 +478,7 @@ static void error_die(const char *sc)
|
|||||||
void *serve_file(void *data)
|
void *serve_file(void *data)
|
||||||
{
|
{
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
char *path = (char *)dvalue(rq->request, "ABS_RESOURCE_PATH");
|
char *path = (char *)dvalue(rq->request, "ABS_RESOURCE_PATH");
|
||||||
char *mime_type = (char *)dvalue(rq->request, "RESOURCE_MIME");
|
char *mime_type = (char *)dvalue(rq->request, "RESOURCE_MIME");
|
||||||
@ -615,8 +613,7 @@ void *decode_request_header(void *data)
|
|||||||
if (host)
|
if (host)
|
||||||
free(host);
|
free(host);
|
||||||
// header ok, now checkmethod
|
// header ok, now checkmethod
|
||||||
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL,rq->client->last_io);
|
||||||
task->status = TASK_DECODE_RQ;
|
|
||||||
|
|
||||||
task->priority++;
|
task->priority++;
|
||||||
return task;
|
return task;
|
||||||
@ -636,7 +633,7 @@ void *decode_request(void *data)
|
|||||||
if (tmp && strcasecmp(tmp, "websocket") == 0)
|
if (tmp && strcasecmp(tmp, "websocket") == 0)
|
||||||
ws = 1;
|
ws = 1;
|
||||||
method = (char *)dvalue(rq->request, "METHOD");
|
method = (char *)dvalue(rq->request, "METHOD");
|
||||||
task = antd_create_task(NULL, (void *)rq, NULL);
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0)
|
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0)
|
||||||
{
|
{
|
||||||
@ -651,13 +648,11 @@ void *decode_request(void *data)
|
|||||||
}
|
}
|
||||||
// resolve task
|
// resolve task
|
||||||
task->handle = resolve_request;
|
task->handle = resolve_request;
|
||||||
task->status = TASK_RESOLVE_RQ;
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
else if (strcmp(method, "POST") == 0)
|
else if (strcmp(method, "POST") == 0)
|
||||||
{
|
{
|
||||||
task->handle = resolve_request;
|
task->handle = resolve_request;
|
||||||
task->status = TASK_RESOLVE_RQ;
|
|
||||||
//task->type = HEAVY;
|
//task->type = HEAVY;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
@ -682,7 +677,7 @@ void *decode_post_request(void *data)
|
|||||||
if (tmp)
|
if (tmp)
|
||||||
clen = atoi(tmp);
|
clen = atoi(tmp);
|
||||||
char *method = (char *)dvalue(rq->request, "METHOD");
|
char *method = (char *)dvalue(rq->request, "METHOD");
|
||||||
task = antd_create_task(NULL, (void *)rq, NULL);
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
task->type = HEAVY;
|
task->type = HEAVY;
|
||||||
if (!method || strcmp(method, "POST") != 0)
|
if (!method || strcmp(method, "POST") != 0)
|
||||||
@ -799,9 +794,8 @@ void *decode_multi_part_request(void *data, const char *ctype)
|
|||||||
char *str_copy = strdup(ctype);
|
char *str_copy = strdup(ctype);
|
||||||
char *orgcpy = str_copy;
|
char *orgcpy = str_copy;
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
task->status = TASK_DECODE_MP_DATA;
|
|
||||||
//dictionary dic = NULL;
|
//dictionary dic = NULL;
|
||||||
boundary = strsep(&str_copy, "="); //discard first part
|
boundary = strsep(&str_copy, "="); //discard first part
|
||||||
boundary = str_copy;
|
boundary = str_copy;
|
||||||
@ -840,7 +834,7 @@ void *decode_multi_part_request_data(void *data)
|
|||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
char *token, *keytoken, *valtoken;
|
char *token, *keytoken, *valtoken;
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY");
|
char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY");
|
||||||
dictionary dic = (dictionary)dvalue(rq->request, "REQUEST_DATA");
|
dictionary dic = (dictionary)dvalue(rq->request, "REQUEST_DATA");
|
||||||
@ -979,7 +973,6 @@ void *decode_multi_part_request_data(void *data)
|
|||||||
// continue upload
|
// continue upload
|
||||||
task->type = HEAVY;
|
task->type = HEAVY;
|
||||||
task->handle = decode_multi_part_request_data;
|
task->handle = decode_multi_part_request_data;
|
||||||
task->status = TASK_DECODE_MP_DATA;
|
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
free(boundend);
|
free(boundend);
|
||||||
@ -1072,7 +1065,7 @@ void *execute_plugin(void *data, const char *pname)
|
|||||||
struct plugin_entry *plugin;
|
struct plugin_entry *plugin;
|
||||||
char *error;
|
char *error;
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
LOG("Plugin name '%s'\n", pname);
|
LOG("Plugin name '%s'\n", pname);
|
||||||
|
|
||||||
@ -1107,14 +1100,12 @@ void *execute_plugin(void *data, const char *pname)
|
|||||||
{
|
{
|
||||||
task->handle = fn;
|
task->handle = fn;
|
||||||
task->type = HEAVY;
|
task->type = HEAVY;
|
||||||
task->status = TASK_EXEC_PLUGIN_RAW;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
free(task);
|
free(task);
|
||||||
task = antd_create_task(decode_post_request, (void *)rq, fn);
|
task = antd_create_task(decode_post_request, (void *)rq, fn, rq->client->last_io);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
task->status = TASK_EXEC_PLUGIN_COOK;
|
|
||||||
}
|
}
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
@ -17,21 +17,6 @@
|
|||||||
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
||||||
#define CONFIG "config.ini"
|
#define CONFIG "config.ini"
|
||||||
|
|
||||||
// define all task status here
|
|
||||||
// for debug purpose
|
|
||||||
#define TASK_ACCEPT 0x01
|
|
||||||
#define TASK_ACCEPT_PEND 0xA0
|
|
||||||
#define TASK_ACCEPT_SSL_CONT 0xA1
|
|
||||||
#define TASK_ACCEPT_HS_DONE 0xA2
|
|
||||||
#define TASK_ACCEPT_READWAIT 0xA3
|
|
||||||
#define TASK_DECODE_HEADER 0x02
|
|
||||||
#define TASK_DECODE_RQ 0x03
|
|
||||||
#define TASK_RESOLVE_RQ 0x04
|
|
||||||
#define TASK_EXEC_PLUGIN_RAW 0x05 // with raw data
|
|
||||||
#define TASK_EXEC_PLUGIN_COOK 0x06 // with decoded post request data
|
|
||||||
#define TASK_SERVE_FILE 0x07
|
|
||||||
#define TASK_DECODE_MP_DATA 0x08
|
|
||||||
|
|
||||||
config_t* config();
|
config_t* config();
|
||||||
void destroy_config();
|
void destroy_config();
|
||||||
void load_config(const char* file);
|
void load_config(const char* file);
|
||||||
|
33
httpd.c
33
httpd.c
@ -72,6 +72,7 @@ void configure_context(SSL_CTX *ctx)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void stop_serve(int dummy) {
|
void stop_serve(int dummy) {
|
||||||
UNUSED(dummy);
|
UNUSED(dummy);
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
@ -99,25 +100,7 @@ void stop_serve(int dummy) {
|
|||||||
close(server_sock);
|
close(server_sock);
|
||||||
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
||||||
}
|
}
|
||||||
void antd_scheduler_dump(antd_scheduler_t* scheduler)
|
|
||||||
{
|
|
||||||
antd_task_queue_t queue = NULL;
|
|
||||||
antd_task_item_t it = NULL;
|
|
||||||
LOG("[[[[SCHEDULER]]]] : dumping all value:\n");
|
|
||||||
pthread_mutex_lock(&scheduler->scheduler_lock);
|
|
||||||
for(int i = 0; i < N_PRIORITY; i++)
|
|
||||||
{
|
|
||||||
queue = scheduler->task_queue[i];
|
|
||||||
for(it = queue; it != NULL && it->next != NULL; it = it->next)
|
|
||||||
{
|
|
||||||
antd_request_t* request = it->task->data;
|
|
||||||
LOG("From: %s [%d]\n", request->client->ip, request->client->sock);
|
|
||||||
LOG("\tStamp: %ul\n", it->task->stamp);
|
|
||||||
LOG("\tstatus: %x\n", it->task->status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
|
||||||
}
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// load the config first
|
// load the config first
|
||||||
@ -150,6 +133,8 @@ int main(int argc, char* argv[])
|
|||||||
LOG("httpd running on port %d\n", port);
|
LOG("httpd running on port %d\n", port);
|
||||||
// default to 4 workers
|
// default to 4 workers
|
||||||
antd_scheduler_init(&scheduler, config()->n_workers);
|
antd_scheduler_init(&scheduler, config()->n_workers);
|
||||||
|
scheduler.validate_data = 1;
|
||||||
|
scheduler.destroy_data = finish_request;
|
||||||
// use blocking server_sock
|
// use blocking server_sock
|
||||||
// make the scheduler wait for event on another thread
|
// make the scheduler wait for event on another thread
|
||||||
// this allow to ged rid of high cpu usage on
|
// this allow to ged rid of high cpu usage on
|
||||||
@ -189,10 +174,6 @@ int main(int argc, char* argv[])
|
|||||||
client_ip = inet_ntoa(client_name.sin_addr);
|
client_ip = inet_ntoa(client_name.sin_addr);
|
||||||
client->ip = strdup(client_ip);
|
client->ip = strdup(client_ip);
|
||||||
LOG("Client IP: %s\n", client_ip);
|
LOG("Client IP: %s\n", client_ip);
|
||||||
if(strcmp(client->ip, "193.48.235.2") == 0)
|
|
||||||
{
|
|
||||||
antd_scheduler_dump(&scheduler);
|
|
||||||
}
|
|
||||||
//LOG("socket: %d\n", client_sock);
|
//LOG("socket: %d\n", client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,10 +190,10 @@ int main(int argc, char* argv[])
|
|||||||
perror("setsockopt failed\n");
|
perror("setsockopt failed\n");
|
||||||
*/
|
*/
|
||||||
client->sock = client_sock;
|
client->sock = client_sock;
|
||||||
|
time(&client->last_io);
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
client->status = 0;
|
client->status = 0;
|
||||||
client->last_wait = 0;
|
|
||||||
if(config()->usessl == 1)
|
if(config()->usessl == 1)
|
||||||
{
|
{
|
||||||
client->ssl = (void*)SSL_new(ctx);
|
client->ssl = (void*)SSL_new(ctx);
|
||||||
@ -227,9 +208,9 @@ int main(int argc, char* argv[])
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
config()->connection++;
|
||||||
// create callback for the server
|
// create callback for the server
|
||||||
task = antd_create_task(accept_request,(void*)request, finish_request );
|
task = antd_create_task(accept_request,(void*)request, finish_request, client->last_io);
|
||||||
task->status = TASK_ACCEPT;
|
|
||||||
//task->type = LIGHT;
|
//task->type = LIGHT;
|
||||||
antd_add_task(&scheduler, task);
|
antd_add_task(&scheduler, task);
|
||||||
}
|
}
|
||||||
|
@ -193,6 +193,8 @@ int antd_send(void *src, const void* data, int len)
|
|||||||
{
|
{
|
||||||
antd_close(src);
|
antd_close(src);
|
||||||
}*/
|
}*/
|
||||||
|
if(written > 0)
|
||||||
|
time(&source->last_io);
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
int antd_recv(void *src, void* data, int len)
|
int antd_recv(void *src, void* data, int len)
|
||||||
@ -340,6 +342,8 @@ int antd_recv(void *src, void* data, int len)
|
|||||||
{
|
{
|
||||||
antd_close(src);
|
antd_close(src);
|
||||||
}*/
|
}*/
|
||||||
|
if(read > 0)
|
||||||
|
time(&source->last_io);
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
void set_nonblock(int socket) {
|
void set_nonblock(int socket) {
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
||||||
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
|
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
|
||||||
#define FORM_MULTI_PART "multipart/form-data"
|
#define FORM_MULTI_PART "multipart/form-data"
|
||||||
#define MAX_WAIT_S 20 // 1/3 minute
|
#define MAX_WAIT_S 2 // 1/3 minute
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
int __attribute__((weak)) usessl();
|
int __attribute__((weak)) usessl();
|
||||||
#endif
|
#endif
|
||||||
@ -38,7 +38,7 @@ typedef struct{
|
|||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
int status;
|
int status;
|
||||||
#endif
|
#endif
|
||||||
time_t last_wait;
|
time_t last_io;
|
||||||
} antd_client_t;
|
} antd_client_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -154,6 +154,8 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
|||||||
scheduler->status = 1;
|
scheduler->status = 1;
|
||||||
scheduler->workers_queue = NULL;
|
scheduler->workers_queue = NULL;
|
||||||
scheduler->pending_task = 0 ;
|
scheduler->pending_task = 0 ;
|
||||||
|
scheduler->validate_data = 0;
|
||||||
|
scheduler->destroy_data = NULL;
|
||||||
// init semaphore
|
// init semaphore
|
||||||
scheduler->scheduler_sem = sem_open("scheduler", O_CREAT, 0600, 0);
|
scheduler->scheduler_sem = sem_open("scheduler", O_CREAT, 0600, 0);
|
||||||
if (scheduler->scheduler_sem == SEM_FAILED)
|
if (scheduler->scheduler_sem == SEM_FAILED)
|
||||||
@ -216,7 +218,7 @@ void antd_scheduler_destroy(antd_scheduler_t* scheduler)
|
|||||||
/*
|
/*
|
||||||
create a task
|
create a task
|
||||||
*/
|
*/
|
||||||
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*))
|
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*), time_t atime)
|
||||||
{
|
{
|
||||||
antd_task_t* task = (antd_task_t*)malloc(sizeof *task);
|
antd_task_t* task = (antd_task_t*)malloc(sizeof *task);
|
||||||
task->stamp = (unsigned long)time(NULL);
|
task->stamp = (unsigned long)time(NULL);
|
||||||
@ -225,7 +227,7 @@ antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callba
|
|||||||
task->callback = callback_of(callback);
|
task->callback = callback_of(callback);
|
||||||
task->priority = NORMAL_PRIORITY;
|
task->priority = NORMAL_PRIORITY;
|
||||||
task->type = LIGHT;
|
task->type = LIGHT;
|
||||||
task->status = NOSTATUS;
|
task->access_time = atime;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,11 +314,26 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
||||||
|
// no task
|
||||||
if(!it)
|
if(!it)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// has the task now
|
// has the task now
|
||||||
|
// validate the task
|
||||||
|
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL)
|
||||||
|
{
|
||||||
|
// data task is not valid
|
||||||
|
LOG("Task data is not valid \n");
|
||||||
|
if(scheduler->destroy_data)
|
||||||
|
scheduler->destroy_data(it->task->data);
|
||||||
|
if(it->task->callback)
|
||||||
|
free_callback(it->task->callback);
|
||||||
|
free(it->task);
|
||||||
|
free(it);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// check the type of task
|
// check the type of task
|
||||||
if(it->task->type == LIGHT || scheduler->n_workers <= 0)
|
if(it->task->type == LIGHT || scheduler->n_workers <= 0)
|
||||||
{
|
{
|
||||||
|
@ -9,40 +9,44 @@
|
|||||||
#define NORMAL_PRIORITY ((int)((N_PRIORITY - 1) / 2))
|
#define NORMAL_PRIORITY ((int)((N_PRIORITY - 1) / 2))
|
||||||
#define LOW_PRIORITY (N_PRIORITY - 1)
|
#define LOW_PRIORITY (N_PRIORITY - 1)
|
||||||
#define HIGH_PRIORITY 0
|
#define HIGH_PRIORITY 0
|
||||||
#define NOSTATUS 0x00
|
#define MAX_VALIDITY_INTERVAL 20 // 10 s for task validity
|
||||||
typedef enum { LIGHT, HEAVY } antd_task_type_t;
|
typedef enum
|
||||||
|
{
|
||||||
|
LIGHT,
|
||||||
|
HEAVY
|
||||||
|
} antd_task_type_t;
|
||||||
// callback definition
|
// callback definition
|
||||||
typedef struct __callback_t{
|
typedef struct __callback_t
|
||||||
void* (*handle)(void*);
|
{
|
||||||
struct __callback_t * next;
|
void *(*handle)(void *);
|
||||||
|
struct __callback_t *next;
|
||||||
} antd_callback_t;
|
} antd_callback_t;
|
||||||
// task definition
|
// task definition
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
/*
|
/*
|
||||||
creation time of a task
|
creation time of a task
|
||||||
*/
|
*/
|
||||||
unsigned long stamp;
|
unsigned long stamp;
|
||||||
|
/*
|
||||||
|
Last access time of
|
||||||
|
task data
|
||||||
|
*/
|
||||||
|
time_t access_time;
|
||||||
/*
|
/*
|
||||||
priority from 0 to N_PRIORITY - 1
|
priority from 0 to N_PRIORITY - 1
|
||||||
higher value is lower priority
|
higher value is lower priority
|
||||||
*/
|
*/
|
||||||
uint8_t priority;
|
uint8_t priority;
|
||||||
/*
|
|
||||||
The status of a task
|
|
||||||
should be set by the application
|
|
||||||
default value 0x00 means that
|
|
||||||
there is no status set
|
|
||||||
*/
|
|
||||||
uint8_t status;
|
|
||||||
/*
|
/*
|
||||||
the callback
|
the callback
|
||||||
*/
|
*/
|
||||||
void* (*handle)(void*);
|
void *(*handle)(void *);
|
||||||
antd_callback_t* callback;
|
antd_callback_t *callback;
|
||||||
/*
|
/*
|
||||||
user data if any
|
user data if any
|
||||||
*/
|
*/
|
||||||
void * data;
|
void *data;
|
||||||
/*
|
/*
|
||||||
type of a task
|
type of a task
|
||||||
light tasks are executed directly
|
light tasks are executed directly
|
||||||
@ -51,21 +55,23 @@ typedef struct {
|
|||||||
antd_task_type_t type;
|
antd_task_type_t type;
|
||||||
} antd_task_t;
|
} antd_task_t;
|
||||||
|
|
||||||
|
typedef struct __task_item_t
|
||||||
typedef struct __task_item_t{
|
{
|
||||||
antd_task_t* task;
|
antd_task_t *task;
|
||||||
struct __task_item_t* next;
|
struct __task_item_t *next;
|
||||||
}* antd_task_item_t;
|
} * antd_task_item_t;
|
||||||
|
|
||||||
typedef antd_task_item_t antd_task_queue_t;
|
typedef antd_task_item_t antd_task_queue_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
int id;
|
int id;
|
||||||
pthread_t tid;
|
pthread_t tid;
|
||||||
void* manager;
|
void *manager;
|
||||||
} antd_worker_t;
|
} antd_worker_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
// data lock
|
// data lock
|
||||||
pthread_mutex_t scheduler_lock;
|
pthread_mutex_t scheduler_lock;
|
||||||
pthread_mutex_t worker_lock;
|
pthread_mutex_t worker_lock;
|
||||||
@ -77,44 +83,56 @@ typedef struct {
|
|||||||
antd_task_queue_t task_queue[N_PRIORITY];
|
antd_task_queue_t task_queue[N_PRIORITY];
|
||||||
antd_task_queue_t workers_queue;
|
antd_task_queue_t workers_queue;
|
||||||
uint8_t status; // 0 stop, 1 working
|
uint8_t status; // 0 stop, 1 working
|
||||||
antd_worker_t* workers;
|
antd_worker_t *workers;
|
||||||
int n_workers;
|
int n_workers;
|
||||||
int pending_task;
|
int pending_task;
|
||||||
|
/*
|
||||||
|
function pointer that free data in a task if
|
||||||
|
the task is not valid
|
||||||
|
default to NULL
|
||||||
|
*/
|
||||||
|
void* (*destroy_data)(void*);
|
||||||
|
int validate_data;
|
||||||
} antd_scheduler_t;
|
} antd_scheduler_t;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
init the main scheduler
|
init the main scheduler
|
||||||
*/
|
*/
|
||||||
void antd_scheduler_init(antd_scheduler_t*, int);
|
void antd_scheduler_init(antd_scheduler_t *, int);
|
||||||
/*
|
/*
|
||||||
destroy all pending task
|
destroy all pending task
|
||||||
*/
|
*/
|
||||||
void antd_scheduler_destroy(antd_scheduler_t*);
|
void antd_scheduler_destroy(antd_scheduler_t *);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
create a task
|
create a task
|
||||||
|
parameter:
|
||||||
|
- handle
|
||||||
|
- data
|
||||||
|
- callback
|
||||||
|
- last data access time
|
||||||
*/
|
*/
|
||||||
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*));
|
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
add a task
|
add a task
|
||||||
*/
|
*/
|
||||||
void antd_add_task(antd_scheduler_t*, antd_task_t*);
|
void antd_add_task(antd_scheduler_t *, antd_task_t *);
|
||||||
/*
|
/*
|
||||||
execute and free a task a task
|
execute and free a task a task
|
||||||
*/
|
*/
|
||||||
void antd_execute_task(antd_scheduler_t*, antd_task_item_t);
|
void antd_execute_task(antd_scheduler_t *, antd_task_item_t);
|
||||||
/*
|
/*
|
||||||
scheduler status
|
scheduler status
|
||||||
*/
|
*/
|
||||||
int antd_scheduler_busy(antd_scheduler_t*);
|
int antd_scheduler_busy(antd_scheduler_t *);
|
||||||
/*
|
/*
|
||||||
schedule a task
|
schedule a task
|
||||||
*/
|
*/
|
||||||
int antd_task_schedule(antd_scheduler_t*);
|
int antd_task_schedule(antd_scheduler_t *);
|
||||||
/*
|
/*
|
||||||
wait for event
|
wait for event
|
||||||
*/
|
*/
|
||||||
void antd_wait(antd_scheduler_t*);
|
void antd_wait(antd_scheduler_t *);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user