Commit 5e91a0b6 authored by Marek Vavruša's avatar Marek Vavruša

modules: use engine instead of context, doc fixups

parent 7f12b24e
......@@ -52,13 +52,12 @@ static int l_quit(lua_State *L)
return 0;
}
/** Trampoline function for modules. */
/** Trampoline function for module properties. */
static int l_trampoline(lua_State *L)
{
const char *name = lua_tostring(L, lua_upvalueindex(1));
const char *property = lua_tostring(L, lua_upvalueindex(2));
struct engine *engine = engine_luaget(L);
struct kr_context *ctx = &engine->resolver;
/* Find module. */
for (unsigned i = 0; i < engine->modules.len; ++i) {
......@@ -69,7 +68,7 @@ static int l_trampoline(lua_State *L)
/* Find property. */
for (struct kr_prop *p = module->props; p && p->name; ++p) {
if (strcmp(p->name, property) == 0) {
auto_free char *ret = p->cb(ctx, module, NULL);
auto_free char *ret = p->cb(engine, module, NULL);
lua_pushstring(L, ret);
return 1;
}
......
......@@ -121,6 +121,8 @@ The project can be built with code coverage tracking using the ``COVERAGE=1`` va
.. _MinGW: http://www.mingw.org/
.. _Dockerfile: https://registry.hub.docker.com/u/cznic/knot-resolver/dockerfile/
.. _Lua: http://www.lua.org/about.html
.. _LuaJIT: http://luajit.org/luajit.html
.. _GCCGO: https://golang.org/doc/install/gccgo
.. _Doxygen: http://www.stack.nl/~dimitri/doxygen/manual/index.html
.. _breathe: https://github.com/michaeljones/breathe
......
......@@ -250,16 +250,23 @@ A module can offer NULL-terminated list of *properties*, each property is essent
JSON was chosen as an interchangeable format that doesn't require any schema beforehand, so you can do two things - query the module properties
from external applications or between modules (i.e. `statistics` module can query `cache` module for memory usage).
JSON was chosen not because it's the most efficient protocol, but because it's easy to read and write and interface to outside world.
.. note:: The ``void *env`` is a generic module interface. Since we're implementing daemon modules, the pointer can be cast to ``struct engine*``.
This is guaranteed by the implemented API version (see `Writing a module in C`_).
Here's an example how a module can expose its property:
.. code-block:: c
char* get_size(struct kr_context *ctx, struct kr_module *m,
char* get_size(void *env, struct kr_module *m,
const char *args)
{
/* Get cache from engine. */
struct engine *engine = env;
namedb_t *cache = engine->resolver.cache;
/* Open read transaction */
namedb_txn_t txn;
namedb_t *cache = ctx->cache;
int ret = kr_cache_txn_begin(cache, &txn, NAMEDB_RDONLY);
if (ret != 0) {
return NULL;
......@@ -292,9 +299,9 @@ Once you load the module, you can call the module property from the interactive
$ kresolved
...
[system] started in interactive mode, type 'help'
> load cached
> cached.cached_size
[system] started in interactive mode, type 'help()'
> modules.load('cached')
> return cached.size()
{ "size": 53 }
*Note* |---| this relies on function pointers, so the same ``static inline`` trick as for the ``Layer()`` is required for C/Go.
......
......@@ -27,8 +27,8 @@
#include <time.h>
#include "daemon/engine.h"
#include "lib/module.h"
#include "lib/resolve.h"
#include "lib/cache.h"
/*
......@@ -45,12 +45,12 @@
static char* get_size(void *env, struct kr_module *module, const char *args)
{
char *result = NULL;
struct kr_context *ctx = env;
struct engine *engine = env;
const namedb_api_t *storage = kr_cache_storage();
/* Fetch item count */
namedb_txn_t txn;
int ret = kr_cache_txn_begin(ctx->cache, &txn, NAMEDB_RDONLY);
int ret = kr_cache_txn_begin(engine->resolver.cache, &txn, NAMEDB_RDONLY);
if (ret == 0) {
asprintf(&result, "{ \"size\": %d }", storage->count(&txn));
kr_cache_txn_abort(&txn);
......@@ -86,11 +86,11 @@ static int is_expired(struct kr_cache_rrset *rr, uint32_t drift)
*/
static char* prune(void *env, struct kr_module *module, const char *args)
{
struct kr_context *ctx = env;
struct engine *engine = env;
const namedb_api_t *storage = kr_cache_storage();
namedb_txn_t txn;
int ret = kr_cache_txn_begin(ctx->cache, &txn, 0);
int ret = kr_cache_txn_begin(engine->resolver.cache, &txn, 0);
if (ret != 0) {
return NULL;
}
......@@ -135,10 +135,10 @@ static char* prune(void *env, struct kr_module *module, const char *args)
*/
static char* clear(void *env, struct kr_module *module, const char *args)
{
struct kr_context *ctx = env;
struct engine *engine = env;
namedb_txn_t txn;
int ret = kr_cache_txn_begin(ctx->cache, &txn, 0);
int ret = kr_cache_txn_begin(engine->resolver.cache, &txn, 0);
if (ret != 0) {
return NULL;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment