commit 71e0dbf: [Project] Remove libfann, gd and other unsupported stuff
Vsevolod Stakhov
vsevolod at highsecure.ru
Sat Jun 22 12:14:12 UTC 2019
Author: Vsevolod Stakhov
Date: 2019-06-16 09:57:21 +0100
URL: https://github.com/rspamd/rspamd/commit/71e0dbf9608026c347279886097790e51e9e5506
[Project] Remove libfann, gd and other unsupported stuff
---
CMakeLists.txt | 34 +-
config.h.in | 2 -
contrib/libev/CMakeLists.txt | 1 -
src/CMakeLists.txt | 3 +-
src/libserver/worker_util.c | 92 ++--
src/libserver/worker_util.h | 14 +-
src/libutil/addr.c | 39 +-
src/libutil/addr.h | 10 +-
src/libutil/util.c | 36 --
src/libutil/util.h | 13 -
src/lua/CMakeLists.txt | 1 -
src/lua/lua_common.c | 1 -
src/lua/lua_common.h | 1 -
src/lua/lua_fann.c | 1032 ------------------------------------------
src/rspamd.c | 77 ++--
src/rspamd.h | 10 +-
16 files changed, 126 insertions(+), 1240 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 893cce7f6..d5f37900a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,9 +52,7 @@ OPTION(WANT_SYSTEMD_UNITS "Install systemd unit files on Linux [default: OFF]"
OPTION(ENABLE_SNOWBALL "Enable snowball stemmer [default: ON]" ON)
OPTION(ENABLE_CLANG_PLUGIN "Enable clang static analysing plugin [default: OFF]" OFF)
OPTION(ENABLE_HYPERSCAN "Enable hyperscan for fast regexp processing [default: OFF]" OFF)
-OPTION(ENABLE_FANN "Enable fann for neural network plugin [default: OFF]" OFF)
OPTION(ENABLE_PCRE2 "Enable pcre2 instead of pcre [default: OFF]" OFF)
-OPTION(ENABLE_GD "Enable libgd for images processing [default: OFF]" OFF)
OPTION(ENABLE_JEMALLOC "Build rspamd with jemalloc allocator [default: OFF]" OFF)
OPTION(ENABLE_COVERAGE "Build rspamd with code coverage options [default: OFF]" OFF)
OPTION(ENABLE_FULL_DEBUG "Build rspamd with all possible debug [default: OFF]" OFF)
@@ -634,9 +632,7 @@ IF(ENABLE_LIBUNWIND MATCHES "ON")
ROOT ${LIBUNWIND_ROOT_DIR} MODULES libunwind)
SET(WITH_LIBUNWIND "1")
ENDIF()
-ProcessPackage(GTHREAD2 LIBRARY gthread-2.0 INCLUDE glib.h
- INCLUDE_SUFFIXES include/glib include/glib-2.0
- ROOT ${GLIB_ROOT_DIR} MODULES gthread-2.0>=2.28)
+
ProcessPackage(GLIB2 LIBRARY glib-2.0 INCLUDE glib.h
INCLUDE_SUFFIXES include/glib include/glib-2.0
ROOT ${GLIB_ROOT_DIR} MODULES glib-2.0>=2.28)
@@ -672,34 +668,6 @@ IF(ENABLE_HYPERSCAN MATCHES "ON")
ROOT ${HYPERSCAN_ROOT_DIR} MODULES libhs)
SET(WITH_HYPERSCAN 1)
ENDIF()
-IF (ENABLE_FANN MATCHES "ON")
- ProcessPackage(FANN LIBRARY fann INCLUDE fann.h INCLUDE_SUFFIXES
- include/fann
- ROOT ${FANN_ROOT_DIR} MODULES fann)
- SET(WITH_FANN 1)
-ENDIF ()
-
-IF (ENABLE_GD MATCHES "ON")
- ProcessPackage(GD LIBRARY gd INCLUDE gd.h INCLUDE_SUFFIXES
- include/gd include/libgd
- ROOT ${GD_ROOT_DIR} MODULES gd)
- LIST(APPEND CMAKE_REQUIRED_INCLUDES "${GD_INCLUDE}")
- LIST(APPEND CMAKE_REQUIRED_LIBRARIES "${GD_LIBRARY}")
-
- CHECK_SYMBOL_EXISTS(gdImageSetInterpolationMethod gd.h GD_INTERPOLATION)
- CHECK_SYMBOL_EXISTS(gdImageScale gd.h GD_SCALE)
- CHECK_SYMBOL_EXISTS(gdImageGrayScale gd.h GD_GRAYSCALE)
- CHECK_SYMBOL_EXISTS(gdImageCreateFromJpegPtr gd.h GD_JPEG)
- CHECK_SYMBOL_EXISTS(gdImageCreateFromPngPtr gd.h GD_PNG)
- CHECK_SYMBOL_EXISTS(gdImageCreateFromBmpPtr gd.h GD_BMP)
- CHECK_SYMBOL_EXISTS(gdImageCreateFromGifPtr gd.h GD_GIF)
-
- IF(GD_INTERPOLATION AND GD_SCALE AND GD_GRAYSCALE AND GD_JPEG AND GD_PNG AND GD_GIF AND GD_BMP)
- SET(USABLE_GD 1)
- ELSE()
- MESSAGE(STATUS "Libgd is found but it is unusable")
- ENDIF()
-ENDIF ()
#Check for openssl (required for dkim)
SET(HAVE_OPENSSL 1)
diff --git a/config.h.in b/config.h.in
index 0693cd55c..9dcf51a50 100644
--- a/config.h.in
+++ b/config.h.in
@@ -139,8 +139,6 @@
#cmakedefine LIBEVENT_EVHTTP 1
#cmakedefine PARAM_H_HAS_BITSET 1
#cmakedefine WITH_DB 1
-#cmakedefine WITH_FANN 1
-#cmakedefine USABLE_GD 1
#cmakedefine WITH_GPERF_TOOLS 1
#cmakedefine WITH_HIREDIS 1
#cmakedefine WITH_HYPERSCAN 1
diff --git a/contrib/libev/CMakeLists.txt b/contrib/libev/CMakeLists.txt
index ca216d180..e681aeb91 100644
--- a/contrib/libev/CMakeLists.txt
+++ b/contrib/libev/CMakeLists.txt
@@ -59,7 +59,6 @@ ADD_LIBRARY(rspamd-libev STATIC ${LIBEVSRC})
ADD_DEFINITIONS("-DEV_CONFIG_H=\"${CMAKE_CURRENT_BINARY_DIR}/libev-config.h\""
-DEV_MULTIPLICITY=1
-DEV_USE_FLOOR=1
- -DEV_USE_MONOTONIC=1
-DEV_NO_THREADS=1 # We do not have threads in Rspamd!
-DEV_FEATURES=127 # Enable all features
)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 3e6abd4e8..da5c19e2d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -101,8 +101,7 @@ SET(PLUGINSSRC plugins/surbl.c
plugins/fuzzy_check.c
plugins/spf.c
plugins/dkim_check.c
- libserver/rspamd_control.c
- lua/lua_fann.c)
+ libserver/rspamd_control.c)
SET(MODULES_LIST surbl regexp chartable fuzzy_check spf dkim)
SET(WORKERS_LIST normal controller fuzzy lua rspamd_proxy log_helper)
diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 59698be32..0d4c4db17 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -299,12 +299,12 @@ rspamd_worker_init_signals (struct rspamd_worker *worker, struct ev_loop *base)
struct ev_loop *
rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
- void (*accept_handler)(int, short, void *))
+ rspamd_accept_handler hdl)
{
- struct ev_loop *ev_base;
- struct event *accept_events;
+ struct ev_loop *event_loop;
GList *cur;
struct rspamd_worker_listen_socket *ls;
+ struct rspamd_worker_accept_event *accept_ev;
#ifdef WITH_PROFILER
extern void _start (void), etext (void);
@@ -316,65 +316,58 @@ rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
worker->signal_events = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, rspamd_sigh_free);
- ev_base = event_init ();
+ event_loop = ev_default_loop (EVFLAG_SIGNALFD);
- rspamd_worker_init_signals (worker, ev_base);
- rspamd_control_worker_add_default_handler (worker, ev_base);
+ rspamd_worker_init_signals (worker, event_loop);
+ rspamd_control_worker_add_default_handler (worker, event_loop);
#ifdef WITH_HIREDIS
rspamd_redis_pool_config (worker->srv->cfg->redis_pool,
- worker->srv->cfg, ev_base);
+ worker->srv->cfg, event_loop);
#endif
/* Accept all sockets */
- if (accept_handler) {
+ if (hdl) {
cur = worker->cf->listen_socks;
while (cur) {
ls = cur->data;
if (ls->fd != -1) {
- accept_events = g_malloc0 (sizeof (struct event) * 2);
- event_set (&accept_events[0], ls->fd, EV_READ | EV_PERSIST,
- accept_handler, worker);
- event_base_set (ev_base, &accept_events[0]);
- event_add (&accept_events[0], NULL);
- worker->accept_events = g_list_prepend (worker->accept_events,
- accept_events);
+ accept_ev = g_malloc0 (sizeof (*accept_ev));
+ accept_ev->event_loop = event_loop;
+ ev_io_init (&accept_ev->accept_ev, hdl, ls->fd, EV_READ);
+ ev_io_start (event_loop, &accept_ev->accept_ev);
+
+ DL_APPEND (worker->accept_events, accept_ev);
}
cur = g_list_next (cur);
}
}
- return ev_base;
+ return event_loop;
}
void
rspamd_worker_stop_accept (struct rspamd_worker *worker)
{
- GList *cur;
- struct event *events;
+ struct rspamd_worker_accept_event *cur, *tmp;
/* Remove all events */
- cur = worker->accept_events;
- while (cur) {
- events = cur->data;
+ DL_FOREACH_SAFE (worker->accept_events, cur, tmp) {
- if (rspamd_event_pending (&events[0], EV_TIMEOUT|EV_READ|EV_WRITE)) {
- event_del (&events[0]);
+ if (ev_is_active (&cur->accept_ev) || ev_is_pending (&cur->accept_ev)) {
+ ev_io_stop (cur->event_loop, &cur->accept_ev);
}
- if (rspamd_event_pending (&events[1], EV_TIMEOUT|EV_READ|EV_WRITE)) {
- event_del (&events[1]);
+
+ if (ev_is_active (&cur->throttling_ev) || ev_is_pending (&cur->throttling_ev)) {
+ ev_timer_stop (cur->event_loop, &cur->throttling_ev);
}
- cur = g_list_next (cur);
- g_free (events);
+ g_free (cur);
}
- if (worker->accept_events != NULL) {
- g_list_free (worker->accept_events);
- }
/* XXX: we need to do it much later */
#if 0
g_hash_table_iter_init (&it, worker->signal_events);
@@ -721,16 +714,6 @@ rspamd_fork_worker (struct rspamd_main *rspamd_main,
rspamd_log_open (rspamd_main->logger);
wrk->start_time = rspamd_get_calendar_ticks ();
-#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION <= 30))
- # if (GLIB_MINOR_VERSION > 20)
- /* Ugly hack for old glib */
- if (!g_thread_get_initialized ()) {
- g_thread_init (NULL);
- }
-# else
- g_thread_init (NULL);
-# endif
-#endif
if (cf->bind_conf) {
msg_info_main ("starting %s process %P (%d); listen on: %s",
cf->worker->name,
@@ -1129,4 +1112,33 @@ rspamd_set_crash_handler (struct rspamd_main *rspamd_main)
sigaction (SIGFPE, &sa, NULL);
sigaction (SIGSYS, &sa, NULL);
#endif
+}
+
+static void
+rspamd_enable_accept_event (gint fd, short what, gpointer d)
+{
+ struct event *events = d;
+
+ event_del (&events[1]);
+ event_add (&events[0], NULL);
+}
+
+void
+rspamd_worker_throttle_accept_events (gint sock, void *data)
+{
+ struct rspamd_worker_accept_event *head, *cur;
+ const gdouble throttling = 0.5;
+ struct ev_loop *ev_base;
+
+ head = (struct rspamd_worker_accept_event *)data;
+
+ DL_FOREACH (head, cur) {
+
+ ev_base = event_get_base (&events[0]);
+ event_del (&events[0]);
+ event_set (&events[1], sock, EV_TIMEOUT, rspamd_enable_accept_event,
+ events);
+ event_base_set (ev_base, &events[1]);
+ event_add (&events[1], &tv);
+ }
}
\ No newline at end of file
diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h
index 2dc78dfc7..67b54e5c9 100644
--- a/src/libserver/worker_util.h
+++ b/src/libserver/worker_util.h
@@ -36,6 +36,9 @@ struct rspamd_worker_signal_handler;
* @param base
*/
void rspamd_worker_init_signals (struct rspamd_worker *worker, struct ev_loop *base);
+
+typedef void (*rspamd_accept_handler)(struct ev_loop *loop, ev_io *w, int revents);
+
/**
* Prepare worker's startup
* @param worker worker structure
@@ -46,7 +49,7 @@ void rspamd_worker_init_signals (struct rspamd_worker *worker, struct ev_loop *b
*/
struct ev_loop *
rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
- void (*accept_handler)(int, short, void *));
+ rspamd_accept_handler hdl);
/**
* Set special signal handler for a worker
@@ -124,8 +127,6 @@ void rspamd_controller_send_ucl (struct rspamd_http_connection_entry *entry,
*/
worker_t * rspamd_get_worker_by_type (struct rspamd_config *cfg, GQuark type);
-void rspamd_worker_stop_accept (struct rspamd_worker *worker);
-
/**
* Block signals before terminations
*/
@@ -202,6 +203,13 @@ void rspamd_worker_init_monitored (struct rspamd_worker *worker,
struct ev_loop *ev_base,
struct rspamd_dns_resolver *resolver);
+/**
+ * Performs throttling for accept events
+ * @param sock
+ * @param data struct rspamd_worker_accept_event * list
+ */
+void rspamd_worker_throttle_accept_events (gint sock, void *data);
+
#define msg_err_main(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
rspamd_main->server_pool->tag.tagname, rspamd_main->server_pool->tag.uid, \
G_STRFUNC, \
diff --git a/src/libutil/addr.c b/src/libutil/addr.c
index 339f5facb..112c5d2cd 100644
--- a/src/libutil/addr.c
+++ b/src/libutil/addr.c
@@ -203,41 +203,10 @@ rspamd_ip_is_valid (const rspamd_inet_addr_t *addr)
return ret;
}
-static void
-rspamd_enable_accept_event (gint fd, short what, gpointer d)
-{
- struct event *events = d;
-
- event_del (&events[1]);
- event_add (&events[0], NULL);
-}
-
-static void
-rspamd_disable_accept_events (gint sock, GList *accept_events)
-{
- GList *cur;
- struct event *events;
- const gdouble throttling = 0.5;
- struct timeval tv;
- struct ev_loop *ev_base;
-
- double_to_tv (throttling, &tv);
-
- for (cur = accept_events; cur != NULL; cur = g_list_next (cur)) {
- events = cur->data;
-
- ev_base = event_get_base (&events[0]);
- event_del (&events[0]);
- event_set (&events[1], sock, EV_TIMEOUT, rspamd_enable_accept_event,
- events);
- event_base_set (ev_base, &events[1]);
- event_add (&events[1], &tv);
- }
-}
-
gint
rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target,
- GList *accept_events)
+ rspamd_accept_throttling_handler hdl,
+ void *hdl_data)
{
gint nfd, serrno;
union sa_union su;
@@ -254,7 +223,9 @@ rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **target,
}
else if (errno == EMFILE || errno == ENFILE) {
/* Temporary disable accept event */
- rspamd_disable_accept_events (sock, accept_events);
+ if (hdl) {
+ hdl (sock, hdl_data);
+ }
return 0;
}
diff --git a/src/libutil/addr.h b/src/libutil/addr.h
index bfe586ad1..7efa5e318 100644
--- a/src/libutil/addr.h
+++ b/src/libutil/addr.h
@@ -221,15 +221,17 @@ int rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type,
*/
gboolean rspamd_ip_is_valid (const rspamd_inet_addr_t *addr);
+typedef void (*rspamd_accept_throttling_handler)(gint, void *);
/**
* Accept from listening socket filling addr structure
* @param sock listening socket
- * @param addr allocated inet addr structure
- * @param accept_events events for accepting new sockets
+ * @param target allocated inet addr structure
* @return
*/
-gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **addr,
- GList *accept_events);
+gint rspamd_accept_from_socket (gint sock,
+ rspamd_inet_addr_t **target,
+ rspamd_accept_throttling_handler hdl,
+ void *hdl_data);
/**
* Parse host[:port[:priority]] line
diff --git a/src/libutil/util.c b/src/libutil/util.c
index 665b6accb..e7a5c2601 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -1612,42 +1612,6 @@ rspamd_thread_func (gpointer ud)
return ud;
}
-/**
- * Create new named thread
- * @param name name pattern
- * @param func function to start
- * @param data data to pass to function
- * @param err error pointer
- * @return new thread object that can be joined
- */
-GThread *
-rspamd_create_thread (const gchar *name,
- GThreadFunc func,
- gpointer data,
- GError **err)
-{
- GThread *new;
- struct rspamd_thread_data *td;
- static gint32 id;
- guint r;
-
- r = strlen (name);
- td = g_malloc (sizeof (struct rspamd_thread_data));
- td->id = ++id;
- td->name = g_malloc (r + sizeof ("4294967296"));
- td->func = func;
- td->data = data;
-
- rspamd_snprintf (td->name, r + sizeof ("4294967296"), "%s-%d", name, id);
-#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 32))
- new = g_thread_try_new (td->name, rspamd_thread_func, td, err);
-#else
- new = g_thread_create (rspamd_thread_func, td, TRUE, err);
-#endif
-
- return new;
-}
-
struct hash_copy_callback_data {
gpointer (*key_copy_func)(gconstpointer data, gpointer ud);
gpointer (*value_copy_func)(gconstpointer data, gpointer ud);
diff --git a/src/libutil/util.h b/src/libutil/util.h
index b7d6055ce..21e4b320e 100644
--- a/src/libutil/util.h
+++ b/src/libutil/util.h
@@ -262,19 +262,6 @@ void rspamd_mutex_unlock (rspamd_mutex_t *mtx);
*/
void rspamd_mutex_free (rspamd_mutex_t *mtx);
-/**
- * Create new named thread
- * @param name name pattern
- * @param func function to start
- * @param data data to pass to function
- * @param err error pointer
- * @return new thread object that can be joined
- */
-GThread * rspamd_create_thread (const gchar *name,
- GThreadFunc func,
- gpointer data,
- GError **err);
-
/**
* Deep copy of one hash table to another
* @param src source hash
diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt
index c3f1a84c1..4a2003605 100644
--- a/src/lua/CMakeLists.txt
+++ b/src/lua/CMakeLists.txt
@@ -22,7 +22,6 @@ SET(LUASRC ${CMAKE_CURRENT_SOURCE_DIR}/lua_common.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_util.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_tcp.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_html.c
- ${CMAKE_CURRENT_SOURCE_DIR}/lua_fann.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_sqlite3.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_cryptobox.c
${CMAKE_CURRENT_SOURCE_DIR}/lua_map.c
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index 5f31ebbb0..689dcd1c4 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -910,7 +910,6 @@ rspamd_lua_init (bool wipe_mem)
luaopen_util (L);
luaopen_tcp (L);
luaopen_html (L);
- luaopen_fann (L);
luaopen_sqlite3 (L);
luaopen_cryptobox (L);
luaopen_dns (L);
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h
index f3d006743..8919a46fd 100644
--- a/src/lua/lua_common.h
+++ b/src/lua/lua_common.h
@@ -281,7 +281,6 @@ void luaopen_text (lua_State *L);
void luaopen_util (lua_State * L);
void luaopen_tcp (lua_State * L);
void luaopen_html (lua_State * L);
-void luaopen_fann (lua_State *L);
void luaopen_sqlite3 (lua_State *L);
void luaopen_cryptobox (lua_State *L);
void luaopen_dns (lua_State *L);
diff --git a/src/lua/lua_fann.c b/src/lua/lua_fann.c
deleted file mode 100644
index 7eb493740..000000000
--- a/src/lua/lua_fann.c
+++ /dev/null
@@ -1,1032 +0,0 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "lua_common.h"
-
-#ifdef WITH_FANN
-#include <fann.h>
-#endif
-
-#include "unix-std.h"
-
-/***
- * @module rspamd_fann
- * This module enables [fann](http://libfann.github.io) interaction in rspamd
- * Please note, that this module works merely if you have `ENABLE_FANN=ON` option
- * definition when building rspamd
- */
-
-/*
- * Fann functions
- */
-LUA_FUNCTION_DEF (fann, is_enabled);
-LUA_FUNCTION_DEF (fann, create);
-LUA_FUNCTION_DEF (fann, create_full);
-LUA_FUNCTION_DEF (fann, load_file);
-LUA_FUNCTION_DEF (fann, load_data);
-
-/*
- * Fann methods
- */
-LUA_FUNCTION_DEF (fann, train);
-LUA_FUNCTION_DEF (fann, train_threaded);
-LUA_FUNCTION_DEF (fann, test);
-LUA_FUNCTION_DEF (fann, save);
-LUA_FUNCTION_DEF (fann, data);
-LUA_FUNCTION_DEF (fann, get_inputs);
-LUA_FUNCTION_DEF (fann, get_outputs);
-LUA_FUNCTION_DEF (fann, get_layers);
-LUA_FUNCTION_DEF (fann, get_mse);
-LUA_FUNCTION_DEF (fann, dtor);
-
-static const struct luaL_reg fannlib_f[] = {
- LUA_INTERFACE_DEF (fann, is_enabled),
- LUA_INTERFACE_DEF (fann, create),
- LUA_INTERFACE_DEF (fann, create_full),
- LUA_INTERFACE_DEF (fann, load_file),
- {"load", lua_fann_load_file},
- LUA_INTERFACE_DEF (fann, load_data),
- {NULL, NULL}
-};
-
-static const struct luaL_reg fannlib_m[] = {
- LUA_INTERFACE_DEF (fann, train),
- LUA_INTERFACE_DEF (fann, train_threaded),
- LUA_INTERFACE_DEF (fann, test),
- LUA_INTERFACE_DEF (fann, save),
- LUA_INTERFACE_DEF (fann, data),
- LUA_INTERFACE_DEF (fann, get_inputs),
- LUA_INTERFACE_DEF (fann, get_outputs),
- LUA_INTERFACE_DEF (fann, get_layers),
- LUA_INTERFACE_DEF (fann, get_mse),
- {"__gc", lua_fann_dtor},
- {"__tostring", rspamd_lua_class_tostring},
- {NULL, NULL}
-};
-
-#ifdef WITH_FANN
-struct fann *
-rspamd_lua_check_fann (lua_State *L, gint pos)
-{
- void *ud = rspamd_lua_check_udata (L, pos, "rspamd{fann}");
- luaL_argcheck (L, ud != NULL, pos, "'fann' expected");
- return ud ? *((struct fann **) ud) : NULL;
-}
-#endif
-
-/***
- * @function rspamd_fann.is_enabled()
- * Checks if fann is enabled for this rspamd build
- * @return {boolean} true if fann is enabled
- */
-static gint
-lua_fann_is_enabled (lua_State *L)
-{
-#ifdef WITH_FANN
- lua_pushboolean (L, true);
-#else
- lua_pushboolean (L, false);
-#endif
- return 1;
-}
-
-/***
- * @function rspamd_fann.create(nlayers, [layer1, ... layern])
- * Creates new neural network with `nlayers` that contains `layer1`...`layern`
- * neurons in each layer
- * @param {number} nlayers number of layers
- * @param {number} layerI number of neurons in each layer
- * @return {fann} fann object
- */
-static gint
-lua_fann_create (lua_State *L)
-{
-#ifndef WITH_FANN
- return 0;
-#else
- struct fann *f, **pfann;
- guint nlayers, *layers, i;
-
- nlayers = luaL_checknumber (L, 1);
-
- if (nlayers > 0) {
- layers = g_malloc (nlayers * sizeof (layers[0]));
-
- if (lua_type (L, 2) == LUA_TNUMBER) {
- for (i = 0; i < nlayers; i ++) {
- layers[i] = luaL_checknumber (L, i + 2);
- }
- }
- else if (lua_type (L, 2) == LUA_TTABLE) {
- for (i = 0; i < nlayers; i ++) {
- lua_rawgeti (L, 2, i + 1);
- layers[i] = luaL_checknumber (L, -1);
- lua_pop (L, 1);
- }
- }
-
- f = fann_create_standard_array (nlayers, layers);
- fann_set_activation_function_hidden (f, FANN_SIGMOID_SYMMETRIC);
- fann_set_activation_function_output (f, FANN_SIGMOID_SYMMETRIC);
- fann_set_training_algorithm (f, FANN_TRAIN_INCREMENTAL);
- fann_randomize_weights (f, 0, 1);
-
- if (f != NULL) {
- pfann = lua_newuserdata (L, sizeof (gpointer));
- *pfann = f;
- rspamd_lua_setclass (L, "rspamd{fann}", -1);
- }
- else {
- lua_pushnil (L);
- }
-
- g_free (layers);
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
-#endif
-}
-
-#ifdef WITH_FANN
-static enum fann_activationfunc_enum
-string_to_activation_func (const gchar *str)
-{
- if (str == NULL) {
- return FANN_SIGMOID_SYMMETRIC;
- }
- if (strcmp (str, "sigmoid") == 0) {
- return FANN_SIGMOID;
- }
*** OUTPUT TRUNCATED, 1047 LINES SKIPPED ***
More information about the Commits
mailing list