mirror of
https://github.com/lxsang/ant-http
synced 2025-07-13 13:04:23 +02:00
Enhance scheduler, cleanup lib
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "dbhelper.h"
|
||||
#include "utils.h"
|
||||
|
||||
sqlite3 * database(const char* file)
|
||||
{
|
||||
|
@ -1,7 +1,6 @@
|
||||
#ifndef DB_HELPER
|
||||
#define DB_HELPER
|
||||
#include <sqlite3.h>
|
||||
#include "utils.h"
|
||||
|
||||
sqlite3 * database(const char*);
|
||||
typedef struct _dbfield
|
||||
|
@ -21,8 +21,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
#include "dictionary.h"
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
#include "dictionary.h"
|
||||
|
||||
dictionary_t dict()
|
||||
{
|
||||
|
25
lib/handle.c
25
lib/handle.c
@ -1,4 +1,27 @@
|
||||
#include "handle.h"
|
||||
#include "handle.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
//open ssl
|
||||
#ifdef USE_OPENSSL
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#endif
|
||||
#ifdef USE_ZLIB
|
||||
#include <zlib.h>
|
||||
#endif
|
||||
#ifdef USE_DB
|
||||
#include "dbhelper.h"
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HTML_TPL "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY><h2>%s</h2></BODY></HTML>"
|
||||
|
||||
static const char* S_100 = "Continue";
|
||||
|
23
lib/handle.h
23
lib/handle.h
@ -1,26 +1,11 @@
|
||||
#ifndef HANDLE_H
|
||||
#define HANDLE_H
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
//open ssl
|
||||
#ifdef USE_OPENSSL
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#endif
|
||||
#ifdef USE_ZLIB
|
||||
#include <zlib.h>
|
||||
#endif
|
||||
#ifdef USE_DB
|
||||
#include "dbhelper.h"
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include "dictionary.h"
|
||||
#include <time.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "ini.h"
|
||||
#include "dictionary.h"
|
||||
|
||||
|
||||
#define SERVER_NAME "Antd"
|
||||
#define IS_POST(method) (strcmp(method,"POST")== 0)
|
||||
|
@ -21,7 +21,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "utils.h"
|
||||
|
||||
list_t list_init()
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ THE SOFTWARE.
|
||||
*/
|
||||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
#define LIST_TYPE_ARRAY 0x5
|
||||
#define LIST_TYPE_POINTER 0x4
|
||||
|
@ -1,11 +1,8 @@
|
||||
#ifndef PLUGIN_H
|
||||
#define PLUGIN_H
|
||||
|
||||
#ifdef USE_DB
|
||||
#include "dbhelper.h"
|
||||
#endif
|
||||
#include "ws.h"
|
||||
#include "scheduler.h"
|
||||
#include "utils.h"
|
||||
#include "handle.h"
|
||||
|
||||
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "scheduler.h"
|
||||
#include "utils.h"
|
||||
|
||||
static void enqueue(antd_task_queue_t* q, antd_task_t* task)
|
||||
{
|
||||
@ -94,6 +98,11 @@ static void execute_callback(antd_scheduler_t* scheduler, antd_task_t* task)
|
||||
// call the first come call back
|
||||
task->handle = cb->handle;
|
||||
task->callback = task->callback->next;
|
||||
task->priority = task->priority + 1;
|
||||
if(task->priority > N_PRIORITY - 1)
|
||||
{
|
||||
task->priority = N_PRIORITY - 1;
|
||||
}
|
||||
free(cb);
|
||||
antd_add_task(scheduler, task);
|
||||
}
|
||||
@ -150,7 +159,7 @@ static void* work(antd_worker_t* worker)
|
||||
init the main scheduler
|
||||
*/
|
||||
|
||||
void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
{
|
||||
scheduler->n_workers = n;
|
||||
scheduler->status = 1;
|
||||
@ -163,13 +172,13 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
if (scheduler->scheduler_sem == SEM_FAILED)
|
||||
{
|
||||
ERROR("Cannot open semaphore for scheduler");
|
||||
exit(-1);
|
||||
return -1;
|
||||
}
|
||||
scheduler->worker_sem = sem_open("worker", O_CREAT, 0600, 0);
|
||||
if (!scheduler->worker_sem)
|
||||
{
|
||||
ERROR("Cannot open semaphore for workers");
|
||||
exit(-1);
|
||||
return -1;
|
||||
}
|
||||
// init lock
|
||||
pthread_mutex_init(&scheduler->scheduler_lock,NULL);
|
||||
@ -183,7 +192,7 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
if(!scheduler->workers)
|
||||
{
|
||||
ERROR("Cannot allocate memory for worker");
|
||||
exit(-1);
|
||||
return -1;
|
||||
}
|
||||
for(int i = 0; i < scheduler->n_workers;i++)
|
||||
{
|
||||
@ -191,7 +200,8 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
scheduler->workers[i].manager = (void*)scheduler;
|
||||
if (pthread_create(&scheduler->workers[i].tid, NULL,(void *(*)(void *))work, (void*)&scheduler->workers[i]) != 0)
|
||||
{
|
||||
perror("pthread_create: cannot create worker\n");
|
||||
ERROR("pthread_create: cannot create worker: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -200,6 +210,7 @@ void antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
}
|
||||
}
|
||||
LOG("Antd scheduler initialized with %d worker", scheduler->n_workers);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
destroy all pending task
|
||||
@ -227,7 +238,7 @@ antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callba
|
||||
task->data = data;
|
||||
task->handle = handle;
|
||||
task->callback = callback_of(callback);
|
||||
task->priority = NORMAL_PRIORITY;
|
||||
task->priority = HIGH_PRIORITY;
|
||||
task->type = HEAVY;
|
||||
//task->type = LIGHT;
|
||||
task->access_time = atime;
|
||||
@ -258,6 +269,7 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
if(!taski)
|
||||
return;
|
||||
// execute the task
|
||||
LOG("Execute task with priority: %d", taski->task->priority);
|
||||
void *ret = (*(taski->task->handle))(taski->task->data);
|
||||
// check the return data if it is a new task
|
||||
if(!ret)
|
||||
@ -289,6 +301,11 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
}
|
||||
else
|
||||
{
|
||||
rtask->priority = taski->task->priority + 1;
|
||||
if(rtask->priority > N_PRIORITY - 1)
|
||||
{
|
||||
rtask->priority = N_PRIORITY - 1;
|
||||
}
|
||||
antd_add_task(scheduler, rtask);
|
||||
free(taski->task);
|
||||
free(taski);
|
||||
@ -324,10 +341,10 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
|
||||
}
|
||||
// has the task now
|
||||
// validate the task
|
||||
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL)
|
||||
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL && it->task->priority == N_PRIORITY - 1)
|
||||
{
|
||||
// data task is not valid
|
||||
LOG("Task data is not valid, task will be killed");
|
||||
// LOG("Task is no longer valid and will be killed");
|
||||
if(scheduler->destroy_data)
|
||||
scheduler->destroy_data(it->task->data);
|
||||
if(it->task->callback)
|
||||
|
@ -1,10 +1,9 @@
|
||||
#ifndef ANT_SCHEDULER
|
||||
#define ANT_SCHEDULER
|
||||
|
||||
#include "utils.h"
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#define N_PRIORITY 10
|
||||
#define NORMAL_PRIORITY ((int)((N_PRIORITY - 1) / 2))
|
||||
#define LOW_PRIORITY (N_PRIORITY - 1)
|
||||
@ -98,7 +97,7 @@ typedef struct
|
||||
/*
|
||||
init the main scheduler
|
||||
*/
|
||||
void antd_scheduler_init(antd_scheduler_t *, int);
|
||||
int antd_scheduler_init(antd_scheduler_t *, int);
|
||||
/*
|
||||
destroy all pending task
|
||||
*/
|
||||
|
23
lib/utils.c
23
lib/utils.c
@ -23,6 +23,29 @@ THE SOFTWARE.
|
||||
*/
|
||||
#include "utils.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
#include <openssl/sha.h>
|
||||
#else
|
||||
#include "sha1.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include "dictionary.h"
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
||||
void error_log(const char* fmt, ...)
|
||||
{
|
||||
|
23
lib/utils.h
23
lib/utils.h
@ -23,27 +23,12 @@ THE SOFTWARE.
|
||||
*/
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <strings.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <regex.h>
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <syslog.h>
|
||||
#ifdef USE_OPENSSL
|
||||
#include <openssl/sha.h>
|
||||
#else
|
||||
#include "sha1.h"
|
||||
#endif
|
||||
#include "base64.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "dictionary.h"
|
||||
|
||||
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
|
||||
|
15
lib/ws.c
15
lib/ws.c
@ -1,7 +1,20 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include<netdb.h> //hostent
|
||||
#ifdef USE_OPENSSL
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#endif
|
||||
#include <sys/time.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "handle.h"
|
||||
|
||||
#include "ws.h"
|
||||
static void ws_gen_mask_key(ws_msg_header_t * header)
|
||||
{
|
||||
@ -332,7 +345,7 @@ int request_socket(const char* ip, int port)
|
||||
struct sockaddr_in dest;
|
||||
|
||||
// time out setting
|
||||
struct timeval timeout;
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = CONN_TIME_OUT_S;
|
||||
timeout.tv_usec = 0;//3 s
|
||||
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
|
||||
|
15
lib/ws.h
15
lib/ws.h
@ -1,19 +1,10 @@
|
||||
#ifndef WS_H
|
||||
#define WS_H
|
||||
#include <resolv.h>
|
||||
#include <errno.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include<netdb.h> //hostent
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <libgen.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include "handle.h"
|
||||
|
||||
#define CONN_TIME_OUT_S 3
|
||||
#define BITV(v,i) ((v & (1 << i)) >> i)
|
||||
#define WS_TEXT 0x1
|
||||
|
Reference in New Issue
Block a user