Skip to content

Commit

Permalink
new module interface (please, update your modules)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachan committed Jul 26, 2013
1 parent 1d65f7f commit 15790be
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 45 deletions.
2 changes: 1 addition & 1 deletion ugh/client.c
Expand Up @@ -88,7 +88,7 @@ void ugh_client_ccb_handle(void *arg)
{
ugh_client_t *c = arg;

int status = ugh_module_handle(c);
int status = ugh_module_handle_all(c);

if (UGH_AGAIN == status)
{
Expand Down
21 changes: 10 additions & 11 deletions ugh/daemon.c
Expand Up @@ -3,20 +3,19 @@
struct ev_loop *loop;

#if 1
ugh_module_handle_fp ugh_module_handles [UGH_MODULE_HANDLES_MAX];
void * ugh_module_configs [UGH_MODULE_HANDLES_MAX];
ugh_module_handle_t ugh_module_handles [UGH_MODULE_HANDLES_MAX];
size_t ugh_module_handles_size = 0;

int ugh_module_handle(ugh_client_t *c)
int ugh_module_handle_all(ugh_client_t *c)
{
size_t i;
int status = UGH_HTTP_OK;

for (i = 0; i < ugh_module_handles_size; ++i)
{
if (NULL == ugh_module_handles[i]) continue;
if (NULL == ugh_module_handles[i].handle) continue;

int tmp_status = ugh_module_handles[i](c, ugh_module_configs[i], &c->bufs[i]);
int tmp_status = ugh_module_handles[i].handle(c, ugh_module_handles[i].config, &c->bufs[i]);

#if 1 /* XXX UGH_AGAIN means here, that module was not supposed to be called */
if (tmp_status != UGH_AGAIN)
Expand Down Expand Up @@ -93,11 +92,11 @@ int ugh_daemon_exec(const char *cfg_filename, unsigned daemon)

size_t i;

for (i = 0; i < ugh_modules_size; ++i)
for (i = 0; i < ugh_module_handles_size; ++i)
{
if (ugh_modules[i]->init)
if (ugh_module_handles[i].module->init)
{
rc = ugh_modules[i]->init(&d.cfg);
rc = ugh_module_handles[i].module->init(&d.cfg, ugh_module_handles[i].config);
if (0 > rc) return -1;
}
}
Expand All @@ -112,11 +111,11 @@ int ugh_daemon_exec(const char *cfg_filename, unsigned daemon)

ugh_server_enough(&d.srv);

for (i = 0; i < ugh_modules_size; ++i)
for (i = 0; i < ugh_module_handles_size; ++i)
{
if (ugh_modules[i]->free)
if (ugh_module_handles[i].module->free)
{
rc = ugh_modules[i]->free();
rc = ugh_module_handles[i].module->free(&d.cfg, ugh_module_handles[i].config);
if (0 > rc) return -1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions ugh/module.h
Expand Up @@ -14,8 +14,8 @@ struct ugh_module
{
ugh_command_t *cmds;

int (*init)(ugh_config_t *cfg);
int (*free)();
int (*init)(ugh_config_t *cfg, void *data); /* data is module conf */
int (*free)(ugh_config_t *cfg, void *data); /* data is module conf */
};

#define UGH_MODULES_MAX 16
Expand Down
6 changes: 5 additions & 1 deletion ugh/module_add_header.c
Expand Up @@ -6,6 +6,8 @@ typedef struct
ugh_template_t value;
} ugh_module_add_header_conf_t;

extern ugh_module_t ugh_module_add_header;

static
int ugh_module_add_header_handle(ugh_client_t *c, void *data, strp body)
{
Expand All @@ -30,7 +32,9 @@ int ugh_command_add_header(ugh_config_t *cfg, int argc, char **argv, ugh_command
ugh_template_compile(&conf->key, argv[1], strlen(argv[1]), cfg);
ugh_template_compile(&conf->value, argv[2], strlen(argv[2]), cfg);

ugh_module_handle_add(ugh_module_add_header_handle, conf);
log_info("handle_add (add_header) %p", conf);

ugh_module_handle_add(ugh_module_add_header, conf, ugh_module_add_header_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_map.c
Expand Up @@ -7,6 +7,8 @@ typedef struct
strt default_value;
} ugh_module_map_conf_t;

extern ugh_module_t ugh_module_map;

/* TODO XXX JudyLFreeArray */

static
Expand Down Expand Up @@ -74,7 +76,7 @@ int ugh_command_map(ugh_config_t *cfg, int argc, char **argv, ugh_command_t *cmd
conf->default_value.data = NULL;
conf->default_value.size = 0;

ugh_module_handle_add(NULL, conf);
ugh_module_handle_add(ugh_module_map, conf, NULL);

/* variable */

Expand Down
4 changes: 3 additions & 1 deletion ugh/module_proxy.c
Expand Up @@ -8,6 +8,8 @@ typedef struct

} ugh_module_proxy_conf_t;

extern ugh_module_t ugh_module_proxy;

static
int ugh_module_proxy_handle(ugh_client_t *c, void *data, strp body)
{
Expand Down Expand Up @@ -55,7 +57,7 @@ int ugh_command_proxy_pass(ugh_config_t *cfg, int argc, char **argv, ugh_command

ugh_template_compile(&conf->url, argv[1], strlen(argv[1]), cfg);

ugh_module_handle_add(ugh_module_proxy_handle, conf);
ugh_module_handle_add(ugh_module_proxy, conf, ugh_module_proxy_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_push_pass.c
Expand Up @@ -12,6 +12,8 @@ typedef struct

} ugh_module_push_pass_conf_t;

extern ugh_module_t ugh_module_push_pass;

static
int ugh_module_push_pass_handle(ugh_client_t *c, void *data, strp body)
{
Expand Down Expand Up @@ -63,7 +65,7 @@ int ugh_command_push_pass(ugh_config_t *cfg, int argc, char **argv, ugh_command_
ugh_template_compile(&conf->channel_id, argv[1], strlen(argv[1]), cfg);
ugh_template_compile(&conf->url, argv[2], strlen(argv[2]), cfg);

ugh_module_handle_add(ugh_module_push_pass_handle, conf);
ugh_module_handle_add(ugh_module_push_pass, conf, ugh_module_push_pass_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_push_publisher.c
Expand Up @@ -23,6 +23,8 @@ typedef struct

} ugh_module_push_publisher_conf_t;

extern ugh_module_t ugh_module_push_publisher;

static
int ugh_module_push_publisher_handle(ugh_client_t *c, void *data, strp body)
{
Expand Down Expand Up @@ -106,7 +108,7 @@ int ugh_command_push_publisher(ugh_config_t *cfg, int argc, char **argv, ugh_com

ugh_template_compile(&conf->channel_id, argv[1], strlen(argv[1]), cfg);

ugh_module_handle_add(ugh_module_push_publisher_handle, conf);
ugh_module_handle_add(ugh_module_push_publisher, conf, ugh_module_push_publisher_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_push_subscriber.c
Expand Up @@ -11,6 +11,8 @@ typedef struct

} ugh_module_push_subscriber_conf_t;

extern ugh_module_t ugh_module_push_subscriber;

static
int ugh_module_push_subscriber_handle(ugh_client_t *c, void *data, strp body)
{
Expand Down Expand Up @@ -94,7 +96,7 @@ int ugh_command_push_subscriber(ugh_config_t *cfg, int argc, char **argv, ugh_co
}
}

ugh_module_handle_add(ugh_module_push_subscriber_handle, conf);
ugh_module_handle_add(ugh_module_push_subscriber, conf, ugh_module_push_subscriber_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_return.c
Expand Up @@ -5,6 +5,8 @@ typedef struct
ugh_template_t template;
} ugh_module_return_conf_t;

extern ugh_module_t ugh_module_return;

static
int ugh_module_return_handle(ugh_client_t *c, void *data, strp body)
{
Expand All @@ -28,7 +30,7 @@ int ugh_command_return(ugh_config_t *cfg, int argc, char **argv, ugh_command_t *

ugh_template_compile(&conf->template, argv[1], strlen(argv[1]), cfg);

ugh_module_handle_add(ugh_module_return_handle, conf);
ugh_module_handle_add(ugh_module_return, conf, ugh_module_return_handle);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion ugh/module_set.c
Expand Up @@ -9,6 +9,8 @@ typedef struct
size_t curr;
} ugh_module_set_conf_t;

extern ugh_module_t ugh_module_set;

static
int ugh_command_set_handle_line(ugh_config_t *cfg, int argc, char **argv)
{
Expand Down Expand Up @@ -45,7 +47,7 @@ int ugh_command_set(ugh_config_t *cfg, int argc, char **argv, ugh_command_t *cmd
conf = aux_pool_calloc(cfg->pool, sizeof(*conf));
if (NULL == conf) return -1;

ugh_module_handle_add(NULL, conf);
ugh_module_handle_add(ugh_module_set, conf, NULL);

/* variable */

Expand Down
4 changes: 3 additions & 1 deletion ugh/module_upstream.c
@@ -1,5 +1,7 @@
#include "ugh.h"

extern ugh_module_t ugh_module_upstream;

static
int ugh_command_upstream_handle_line(ugh_config_t *cfg, int argc, char **argv)
{
Expand Down Expand Up @@ -47,7 +49,7 @@ int ugh_command_upstream(ugh_config_t *cfg, int argc, char **argv, ugh_command_t

upstream = ugh_upstream_add(cfg, argv[1], strlen(argv[1]));

ugh_module_handle_add(NULL, upstream);
ugh_module_handle_add(ugh_module_upstream, upstream, NULL);

ugh_config_parser(cfg, ugh_command_upstream_handle_line);

Expand Down
25 changes: 17 additions & 8 deletions ugh/ugh.h
Expand Up @@ -486,22 +486,31 @@ extern const char *ugh_status_header [UGH_HTTP_STATUS_MAX];
/* ### module ### */

#if 1
typedef int (*ugh_module_handle_fp) (ugh_client_t *c, void *data, strp body);
typedef struct ugh_module_handle
ugh_module_handle_t;

struct ugh_module_handle
{
ugh_module_t *module;
void *config;
int (*handle) (ugh_client_t *c, void *data, strp body);
};

#define UGH_MODULE_HANDLES_MAX 1024

extern ugh_module_handle_fp ugh_module_handles [UGH_MODULE_HANDLES_MAX];
extern void * ugh_module_configs [UGH_MODULE_HANDLES_MAX];
extern ugh_module_handle_t ugh_module_handles [UGH_MODULE_HANDLES_MAX];
extern size_t ugh_module_handles_size;

#define ugh_module_handle_add(handle, config) \
ugh_module_handles[ugh_module_handles_size] = handle; \
ugh_module_configs[ugh_module_handles_size] = config; \
#define ugh_module_handle_add(_module, _config, _handle) \
ugh_module_handles[ugh_module_handles_size].module = &_module; \
ugh_module_handles[ugh_module_handles_size].config = _config; \
ugh_module_handles[ugh_module_handles_size].handle = _handle; \
ugh_module_handles_size++

int ugh_module_handle(ugh_client_t *c);
int ugh_module_handle_all(ugh_client_t *c);

#define ugh_module_config_get_last() ugh_module_configs[ugh_module_handles_size - 1]
/* XXX remove this macro */
#define ugh_module_config_get_last() ugh_module_handles[ugh_module_handles_size - 1].config
#endif

#if 1
Expand Down
32 changes: 18 additions & 14 deletions ugh_example/module.c
Expand Up @@ -8,19 +8,7 @@ typedef struct
ugh_template_t logger_host;
} ugh_module_example_conf_t;

int ugh_module_example_init(ugh_config_t *cfg)
{
log_info("ugh_module_example_init (called each time server is started)");

return 0;
}

int ugh_module_example_free()
{
log_info("ugh_module_example_free (called each time server is stopped)");

return 0;
}
extern ugh_module_t ugh_module_example;

int ugh_module_example_handle(ugh_client_t *c, void *data, strp body)
{
Expand Down Expand Up @@ -93,14 +81,30 @@ int ugh_module_example_handle(ugh_client_t *c, void *data, strp body)
return UGH_HTTP_OK;
}

int ugh_module_example_init(ugh_config_t *cfg, void *data)
{
ugh_module_example_conf_t *conf = data;

log_info("ugh_module_example_init (called for each added handle, each time server is starting), conf=%p", &conf);

return 0;
}

int ugh_module_example_free(ugh_config_t *cfg, void *data)
{
log_info("ugh_module_example_free (called for each added handle, each time server is stopped)");

return 0;
}

int ugh_command_example(ugh_config_t *cfg, int argc, char **argv, ugh_command_t *cmd)
{
ugh_module_example_conf_t *conf;

conf = aux_pool_malloc(cfg->pool, sizeof(*conf));
if (NULL == conf) return -1;

ugh_module_handle_add(ugh_module_example_handle, conf);
ugh_module_handle_add(ugh_module_example, conf, ugh_module_example_handle);

return 0;
}
Expand Down

0 comments on commit 15790be

Please sign in to comment.