Reindented server/core/filter.c

This commit is contained in:
Johan Wikman
2015-11-30 12:51:24 +02:00
parent bd94d8967a
commit 20c4a0aa67
2 changed files with 294 additions and 264 deletions

View File

@ -53,10 +53,12 @@ static FILTER_DEF *allFilters = NULL; /**< The list of all filters */
FILTER_DEF * FILTER_DEF *
filter_alloc(char *name, char *module) filter_alloc(char *name, char *module)
{ {
FILTER_DEF *filter; FILTER_DEF *filter;
if ((filter = (FILTER_DEF *)malloc(sizeof(FILTER_DEF))) == NULL) if ((filter = (FILTER_DEF *)malloc(sizeof(FILTER_DEF))) == NULL)
{
return NULL; return NULL;
}
filter->name = strdup(name); filter->name = strdup(name);
filter->module = strdup(module); filter->module = strdup(module);
filter->filter = NULL; filter->filter = NULL;
@ -84,7 +86,7 @@ FILTER_DEF *filter;
void void
filter_free(FILTER_DEF *filter) filter_free(FILTER_DEF *filter)
{ {
FILTER_DEF *ptr; FILTER_DEF *ptr;
if (filter) if (filter)
{ {
@ -102,8 +104,10 @@ FILTER_DEF *ptr;
ptr = ptr->next; ptr = ptr->next;
} }
if (ptr) if (ptr)
{
ptr->next = filter->next; ptr->next = filter->next;
} }
}
spinlock_release(&filter_spin); spinlock_release(&filter_spin);
/* Clean up session and free the memory */ /* Clean up session and free the memory */
@ -123,14 +127,16 @@ FILTER_DEF *ptr;
FILTER_DEF * FILTER_DEF *
filter_find(char *name) filter_find(char *name)
{ {
FILTER_DEF *filter; FILTER_DEF *filter;
spinlock_acquire(&filter_spin); spinlock_acquire(&filter_spin);
filter = allFilters; filter = allFilters;
while (filter) while (filter)
{ {
if (strcmp(filter->name, name) == 0) if (strcmp(filter->name, name) == 0)
{
break; break;
}
filter = filter->next; filter = filter->next;
} }
spinlock_release(&filter_spin); spinlock_release(&filter_spin);
@ -146,7 +152,9 @@ int
filter_standard_parameter(char *name) filter_standard_parameter(char *name)
{ {
if (strcmp(name, "type") == 0 || strcmp(name, "module") == 0) if (strcmp(name, "type") == 0 || strcmp(name, "module") == 0)
{
return 1; return 1;
}
return 0; return 0;
} }
@ -159,8 +167,8 @@ filter_standard_parameter(char *name)
void void
dprintAllFilters(DCB *dcb) dprintAllFilters(DCB *dcb)
{ {
FILTER_DEF *ptr; FILTER_DEF *ptr;
int i; int i;
spinlock_acquire(&filter_spin); spinlock_acquire(&filter_spin);
ptr = allFilters; ptr = allFilters;
@ -172,13 +180,19 @@ int i;
{ {
dcb_printf(dcb, "\tOptions: "); dcb_printf(dcb, "\tOptions: ");
for (i = 0; ptr->options && ptr->options[i]; i++) for (i = 0; ptr->options && ptr->options[i]; i++)
{
dcb_printf(dcb, "%s ", ptr->options[i]); dcb_printf(dcb, "%s ", ptr->options[i]);
}
dcb_printf(dcb, "\n"); dcb_printf(dcb, "\n");
} }
if (ptr->obj && ptr->filter) if (ptr->obj && ptr->filter)
{
ptr->obj->diagnostics(ptr->filter, NULL, dcb); ptr->obj->diagnostics(ptr->filter, NULL, dcb);
}
else else
{
dcb_printf(dcb, "\tModule not loaded.\n"); dcb_printf(dcb, "\tModule not loaded.\n");
}
ptr = ptr->next; ptr = ptr->next;
} }
spinlock_release(&filter_spin); spinlock_release(&filter_spin);
@ -193,7 +207,7 @@ int i;
void void
dprintFilter(DCB *dcb, FILTER_DEF *filter) dprintFilter(DCB *dcb, FILTER_DEF *filter)
{ {
int i; int i;
dcb_printf(dcb, "Filter %p (%s)\n", filter, filter->name); dcb_printf(dcb, "Filter %p (%s)\n", filter, filter->name);
dcb_printf(dcb, "\tModule: %s\n", filter->module); dcb_printf(dcb, "\tModule: %s\n", filter->module);
@ -201,11 +215,15 @@ int i;
{ {
dcb_printf(dcb, "\tOptions: "); dcb_printf(dcb, "\tOptions: ");
for (i = 0; filter->options && filter->options[i]; i++) for (i = 0; filter->options && filter->options[i]; i++)
{
dcb_printf(dcb, "%s ", filter->options[i]); dcb_printf(dcb, "%s ", filter->options[i]);
}
dcb_printf(dcb, "\n"); dcb_printf(dcb, "\n");
} }
if (filter->obj && filter->filter) if (filter->obj && filter->filter)
{
filter->obj->diagnostics(filter->filter, NULL, dcb); filter->obj->diagnostics(filter->filter, NULL, dcb);
}
} }
/** /**
@ -215,8 +233,8 @@ int i;
void void
dListFilters(DCB *dcb) dListFilters(DCB *dcb)
{ {
FILTER_DEF *ptr; FILTER_DEF *ptr;
int i; int i;
spinlock_acquire(&filter_spin); spinlock_acquire(&filter_spin);
ptr = allFilters; ptr = allFilters;
@ -233,12 +251,17 @@ int i;
dcb_printf(dcb, "%-19s | %-15s | ", dcb_printf(dcb, "%-19s | %-15s | ",
ptr->name, ptr->module); ptr->name, ptr->module);
for (i = 0; ptr->options && ptr->options[i]; i++) for (i = 0; ptr->options && ptr->options[i]; i++)
{
dcb_printf(dcb, "%s ", ptr->options[i]); dcb_printf(dcb, "%s ", ptr->options[i]);
}
dcb_printf(dcb, "\n"); dcb_printf(dcb, "\n");
ptr = ptr->next; ptr = ptr->next;
} }
if (allFilters) if (allFilters)
dcb_printf(dcb, "--------------------+-----------------+----------------------------------------\n\n"); {
dcb_printf(dcb,
"--------------------+-----------------+----------------------------------------\n\n");
}
spinlock_release(&filter_spin); spinlock_release(&filter_spin);
} }
@ -251,7 +274,7 @@ int i;
void void
filterAddOption(FILTER_DEF *filter, char *option) filterAddOption(FILTER_DEF *filter, char *option)
{ {
int i; int i;
spinlock_acquire(&filter->spin); spinlock_acquire(&filter->spin);
if (filter->options == NULL) if (filter->options == NULL)
@ -263,9 +286,10 @@ int i;
else else
{ {
for (i = 0; filter->options[i]; i++) for (i = 0; filter->options[i]; i++)
{
; ;
filter->options = (char **)realloc(filter->options, }
(i + 2) * sizeof(char *)); filter->options = (char **)realloc(filter->options, (i + 2) * sizeof(char *));
filter->options[i] = strdup(option); filter->options[i] = strdup(option);
filter->options[i+1] = NULL; filter->options[i+1] = NULL;
} }
@ -282,7 +306,7 @@ int i;
void void
filterAddParameter(FILTER_DEF *filter, char *name, char *value) filterAddParameter(FILTER_DEF *filter, char *name, char *value)
{ {
int i; int i;
spinlock_acquire(&filter->spin); spinlock_acquire(&filter->spin);
if (filter->parameters == NULL) if (filter->parameters == NULL)
@ -293,7 +317,9 @@ int i;
else else
{ {
for (i = 0; filter->parameters[i]; i++) for (i = 0; filter->parameters[i]; i++)
{
; ;
}
filter->parameters = (FILTER_PARAMETER **)realloc(filter->parameters, filter->parameters = (FILTER_PARAMETER **)realloc(filter->parameters,
(i + 2) * sizeof(FILTER_PARAMETER *)); (i + 2) * sizeof(FILTER_PARAMETER *));
} }
@ -394,14 +420,16 @@ filterApply(FILTER_DEF *filter, SESSION *session, DOWNSTREAM *downstream)
UPSTREAM * UPSTREAM *
filterUpstream(FILTER_DEF *filter, void *fsession, UPSTREAM *upstream) filterUpstream(FILTER_DEF *filter, void *fsession, UPSTREAM *upstream)
{ {
UPSTREAM *me = NULL; UPSTREAM *me = NULL;
/* /*
* The the filter has no setUpstream entry point then is does * The the filter has no setUpstream entry point then is does
* not require to see results and can be left out of the chain. * not require to see results and can be left out of the chain.
*/ */
if (filter->obj->setUpstream == NULL) if (filter->obj->setUpstream == NULL)
{
return upstream; return upstream;
}
if (filter->obj->clientReply != NULL) if (filter->obj->clientReply != NULL)
{ {

View File

@ -41,7 +41,8 @@ typedef void *FILTER;
/** /**
* The structure used to pass name, value pairs to the filter instances * The structure used to pass name, value pairs to the filter instances
*/ */
typedef struct { typedef struct
{
char *name; /**< Name of the parameter */ char *name; /**< Name of the parameter */
char *value; /**< Value of the parameter */ char *value; /**< Value of the parameter */
} FILTER_PARAMETER; } FILTER_PARAMETER;
@ -69,7 +70,8 @@ typedef struct {
* *
* @see load_module * @see load_module
*/ */
typedef struct filter_object { typedef struct filter_object
{
FILTER *(*createInstance)(char **options, FILTER_PARAMETER **); FILTER *(*createInstance)(char **options, FILTER_PARAMETER **);
void *(*newSession)(FILTER *instance, SESSION *session); void *(*newSession)(FILTER *instance, SESSION *session);
void (*closeSession)(FILTER *instance, void *fsession); void (*closeSession)(FILTER *instance, void *fsession);
@ -92,17 +94,16 @@ typedef struct filter_object {
* This is basically the link between a plugin to load and the * This is basically the link between a plugin to load and the
* optons to pass to that plugin. * optons to pass to that plugin.
*/ */
typedef struct filter_def { typedef struct filter_def
{
char *name; /**< The Filter name */ char *name; /**< The Filter name */
char *module; /**< The module to load */ char *module; /**< The module to load */
char **options; /**< The options set for this filter */ char **options; /**< The options set for this filter */
FILTER_PARAMETER FILTER_PARAMETER **parameters; /**< The filter parameters */
**parameters; /**< The filter parameters */
FILTER filter; /**< The runtime filter */ FILTER filter; /**< The runtime filter */
FILTER_OBJECT *obj; /**< The "MODULE_OBJECT" for the filter */ FILTER_OBJECT *obj; /**< The "MODULE_OBJECT" for the filter */
SPINLOCK spin; /**< Spinlock to protect the filter definition */ SPINLOCK spin; /**< Spinlock to protect the filter definition */
struct filter_def struct filter_def *next; /**< Next filter in the chain of all filters */
*next; /**< Next filter in the chain of all filters */
} FILTER_DEF; } FILTER_DEF;
FILTER_DEF *filter_alloc(char *, char *); FILTER_DEF *filter_alloc(char *, char *);
@ -117,4 +118,5 @@ int filter_standard_parameter(char *);
void dprintAllFilters(DCB *); void dprintAllFilters(DCB *);
void dprintFilter(DCB *, FILTER_DEF *); void dprintFilter(DCB *, FILTER_DEF *);
void dListFilters(DCB *); void dListFilters(DCB *);
#endif #endif