This commit is contained in:
183
src/rest/restd.c
Normal file
183
src/rest/restd.c
Normal file
@@ -0,0 +1,183 @@
|
||||
|
||||
|
||||
/*!
|
||||
* restd.h
|
||||
*
|
||||
* Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 07/02/2020
|
||||
*
|
||||
*/
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/event.h>
|
||||
#include <event2/listener.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
#include "restd.h"
|
||||
/*
|
||||
* Local variables.
|
||||
*/
|
||||
static bool initialized = false;
|
||||
|
||||
/*
|
||||
* Global variables.
|
||||
*/
|
||||
int _restd_log_level = RESTD_LOG_WARN;
|
||||
|
||||
/*--------------------------- PUBLIC FUNCTIONS -------------------------------*/
|
||||
|
||||
/**
|
||||
* Set debug output level.
|
||||
*
|
||||
* @param debug_level debug output level. 0 to disable.
|
||||
*
|
||||
* @return previous debug level.
|
||||
*
|
||||
* @note
|
||||
* debug_level:
|
||||
* REST_LOG_DISABLE
|
||||
* REST_LOG_ERROR
|
||||
* REST_LOG_WARN (default)
|
||||
* REST_LOG_INFO
|
||||
* REST_LOG_DEBUG
|
||||
* REST_LOG_DEBUG2
|
||||
*/
|
||||
enum restd_log_e restd_log_level(enum restd_log_e log_level)
|
||||
{
|
||||
int prev = _restd_log_level;
|
||||
_restd_log_level = log_level;
|
||||
return prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a server object.
|
||||
*/
|
||||
restd_server_t *restd_server_new(void)
|
||||
{
|
||||
if (initialized)
|
||||
{
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
restd_server_t *server = NEW_OBJECT(restd_server_t);
|
||||
if (server == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize instance.
|
||||
server->options = qhashtbl(0, 0);
|
||||
server->stats = qhashtbl(100, QHASHTBL_THREADSAFE);
|
||||
server->hooks = qlist(0);
|
||||
//server->call_hooks = call_hooks;
|
||||
if (server->options == NULL || server->stats == NULL || server->hooks == NULL)
|
||||
{
|
||||
restd_server_free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DEBUG("Created a server object.");
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release server object and all the resources.
|
||||
*/
|
||||
void restd_server_free(restd_server_t *server)
|
||||
{
|
||||
if (server == NULL)
|
||||
return;
|
||||
#if 0
|
||||
int thread = restd_server_get_option_int(server, "server.thread");
|
||||
if (thread && server->thread)
|
||||
{
|
||||
notify_loopexit(server);
|
||||
sleep(1);
|
||||
close_server(server);
|
||||
}
|
||||
|
||||
if (server->evbase)
|
||||
{
|
||||
event_base_free(server->evbase);
|
||||
}
|
||||
|
||||
if (server->options)
|
||||
{
|
||||
server->options->free(server->options);
|
||||
}
|
||||
if (server->stats)
|
||||
{
|
||||
server->stats->free(server->stats);
|
||||
}
|
||||
if (server->hooks)
|
||||
{
|
||||
qlist_t *tbl = server->hooks;
|
||||
restd_hook_t *hook;
|
||||
while ((hook = tbl->popfirst(tbl, NULL)))
|
||||
{
|
||||
if (hook->method)
|
||||
free(hook->method);
|
||||
if (hook->path)
|
||||
free(hook->path);
|
||||
free(hook);
|
||||
}
|
||||
server->hooks->free(server->hooks);
|
||||
}
|
||||
#endif
|
||||
free(server);
|
||||
DEBUG("Server terminated.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Start server.
|
||||
*
|
||||
* @return 0 if successful, otherwise -1.
|
||||
*/
|
||||
int restd_server_start(restd_server_t *server)
|
||||
{
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int restd_server_attach_event_loop(restd_server_t *server, struct event_base *ev_base)
|
||||
{
|
||||
if (server == NULL)
|
||||
return -1;
|
||||
|
||||
server->evbase = ev_base;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void restd_server_set_option(restd_server_t *server, const char *key, const char *value)
|
||||
{
|
||||
}
|
||||
|
||||
void restd_server_register_hook_on_path(restd_server_t *server, const char *method, const char *path,
|
||||
restd_callback cb, void *userdata)
|
||||
{
|
||||
}
|
||||
|
||||
/*--------------------------- LOCAL FUNCTIONS -------------------------------*/
|
||||
Reference in New Issue
Block a user