Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / plugins-source / callbackcounter / callbackcounter.c
1 /*-------------------------------------------------------------------------
2  * C-Pluff, a plug-in framework for C
3  * Copyright 2007 Johannes Lehtinen
4  * 
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:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
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  *-----------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <cpluff.h>
29 #include "callbackcounter.h"
30
31 struct runtime_data {
32         cp_context_t *ctx;
33         cbc_counters_t *counters;
34 };
35
36 static void *create(cp_context_t *ctx) {
37         struct runtime_data *data;
38         
39         if ((data = malloc(sizeof(struct runtime_data))) == NULL) {
40                 return NULL;
41         }
42         data->ctx = ctx;
43         
44         /*
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
47          * function.
48          */
49         if ((data->counters = malloc(sizeof(cbc_counters_t))) == NULL) {
50                 free(data);
51                 return NULL;
52         }
53         memset(data->counters, 0, sizeof(cbc_counters_t));
54         data->counters->context_arg_0 = NULL;
55         data->counters->create++;
56         
57         return data;
58 }
59
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;
62         
63         data->counters->logger++;
64 }
65
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;
68         
69         data->counters->listener++;
70 }
71
72 static int run(void *d) {
73         struct runtime_data *data = d;
74         
75         data->counters->run++;
76         return (data->counters->run < 3);
77 }
78
79 static int start(void *d) {
80         struct runtime_data *data = d;
81         char **argv;
82         
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;
88                 }
89         }
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;
95         } else {
96                 return CP_OK;
97         }       
98 }
99
100 static void stop(void *d) {
101         struct runtime_data *data = d;
102
103         data->counters->stop++;
104
105         /* 
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.
108          */
109 }
110
111 static void destroy(void *d) {
112         struct runtime_data *data = d;
113
114         data->counters->destroy++;      
115         data->counters = NULL;
116         free(data);
117 }
118
119 CP_EXPORT cp_plugin_runtime_t cbc_runtime = {
120         create,
121         start,
122         stop,
123         destroy
124 };