1 /*-------------------------------------------------------------------------
2 * C-Pluff, a plug-in framework for C
3 * Copyright 2007 Johannes Lehtinen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *-----------------------------------------------------------------------*/
29 #include "callbackcounter.h"
33 cbc_counters_t *counters;
36 static void *create(cp_context_t *ctx) {
37 struct runtime_data *data;
39 if ((data = malloc(sizeof(struct runtime_data))) == NULL) {
45 * Normally data->counters would be initialized in start function.
46 * We do it already here to be able to record count for the create
49 if ((data->counters = malloc(sizeof(cbc_counters_t))) == NULL) {
53 memset(data->counters, 0, sizeof(cbc_counters_t));
54 data->counters->context_arg_0 = NULL;
55 data->counters->create++;
60 static void logger(cp_log_severity_t severity, const char *msg, const char *apid, void *user_data) {
61 struct runtime_data *data = user_data;
63 data->counters->logger++;
66 static void listener(const char *plugin_id, cp_plugin_state_t old_state, cp_plugin_state_t new_state, void *user_data) {
67 struct runtime_data *data = user_data;
69 data->counters->listener++;
72 static int run(void *d) {
73 struct runtime_data *data = d;
75 data->counters->run++;
76 return (data->counters->run < 3);
79 static int start(void *d) {
80 struct runtime_data *data = d;
83 data->counters->start++;
84 argv = cp_get_context_args(data->ctx, NULL);
85 if (argv != NULL && argv[0] != NULL) {
86 if ((data->counters->context_arg_0 = strdup(argv[0])) == NULL) {
87 return CP_ERR_RESOURCE;
90 if (cp_define_symbol(data->ctx, "cbc_counters", data->counters) != CP_OK
91 || cp_register_logger(data->ctx, logger, data, CP_LOG_WARNING) != CP_OK
92 || cp_register_plistener(data->ctx, listener, data) != CP_OK
93 || cp_run_function(data->ctx, run) != CP_OK) {
94 return CP_ERR_RUNTIME;
100 static void stop(void *d) {
101 struct runtime_data *data = d;
103 data->counters->stop++;
106 * Normally data->counters would be freed here. However, we do not free
107 * it so that the test program can read counters after plug-in stops.
111 static void destroy(void *d) {
112 struct runtime_data *data = d;
114 data->counters->destroy++;
115 data->counters = NULL;
119 CP_EXPORT cp_plugin_runtime_t cbc_runtime = {