2015-08-10 13:54:10 +02:00
|
|
|
# Hooks
|
|
|
|
|
|
|
|
Hooks are simple callback functions which are called whenever a message is processed by a path.
|
|
|
|
|
|
|
|
There are several built-in hooks for:
|
|
|
|
- collecting, show & reset statistics
|
|
|
|
- drop reordered messages
|
|
|
|
- verify message headers
|
|
|
|
- handle simulation restarts
|
|
|
|
|
|
|
|
But main goal of this mechanism is to provide extensibility for the end user.
|
|
|
|
Example applications for hooks might be:
|
|
|
|
|
|
|
|
1. Filter sample values
|
|
|
|
2. Manipulate sample values: FIR
|
|
|
|
3. Transform sample values: FFT, DCT
|
|
|
|
4. Update network emulation settings based on sample values
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
Each path is allowed to have multiple active hooks.
|
|
|
|
Those can be configured in the path section using the `hooks` setting:
|
|
|
|
|
2015-12-12 12:37:57 +01:00
|
|
|
paths = [
|
|
|
|
{
|
|
|
|
in = "input_node",
|
|
|
|
out = "output_node",
|
|
|
|
hooks = [ "fir", "print" ]
|
|
|
|
# hooks = "decimate" // alternative syntax for a single hook
|
|
|
|
}
|
|
|
|
]
|
2015-08-10 13:54:10 +02:00
|
|
|
|
|
|
|
**Please note:** the hooks will be executed in the order they are given in the configuration file!
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
2015-12-12 12:37:57 +01:00
|
|
|
The interface for user-defined hook functions is defined in (`server/include/hooks.h`):
|
2015-08-10 13:54:10 +02:00
|
|
|
|
2015-12-12 12:37:57 +01:00
|
|
|
```
|
|
|
|
typedef int (*hook_cb_t)(struct path *p, struct hook *h, int when);
|
2015-08-10 13:54:10 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
There are several occasions when a hook can be executed in the program flow (@see hook_type).
|
2015-12-12 12:37:57 +01:00
|
|
|
There is a dedicated queue for each occasion.
|
2015-08-10 13:54:10 +02:00
|
|
|
|
2015-12-12 12:37:57 +01:00
|
|
|
The queue for which a hook can be called is encoded as a mask and specified during registration of that hook.
|
|
|
|
|
|
|
|
| Queue | Desciption |
|
|
|
|
| --------------------- | --------------------------------------------------------------------- |
|
|
|
|
| `HOOK_PATH_START` | Called whenever a path is started; before threads are created. |
|
|
|
|
| `HOOK_PATH_STOP` | Called whenever a path is stopped; after threads are destoyed. |
|
2015-08-10 13:54:10 +02:00
|
|
|
| `HOOK_PATH_RESTART` | Called whenever a new simulation case is started. This is detected by a sequence no equal to zero. |
|
2015-12-12 12:37:57 +01:00
|
|
|
| `HOOK_PRE` | Called when a new packet of messages (samples) was received. |
|
|
|
|
| `HOOK_POST` | Called after each message (sample) of a packet was processed. |
|
|
|
|
| `HOOK_MSG` | Called for each message (sample) in a packet. |
|
|
|
|
| `HOOK_PERIODIC` | Called periodically. Period is set by global 'stats' option in the configuration file. |
|
2015-08-10 13:54:10 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
There are already several hooks defined in (server/src/hooks.c).
|
|
|
|
Use one of those as a starting point for your own hook.
|
|
|
|
|
|
|
|
A typical hook function might look like this one:
|
|
|
|
|
2015-12-12 12:37:57 +01:00
|
|
|
int my_super_hook_function(struct path *p, struct hook *h, int when)
|
|
|
|
{
|
|
|
|
struct msg *last_received_msg = p->current;
|
|
|
|
|
|
|
|
printf("The last message contained %u values\n", last_received_msg->length);
|
|
|
|
}
|