diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-06-26 12:51:11 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-06-26 12:51:11 +0000 |
commit | 3ccf16e152da02c842220d1d653f5f303eee9b4f (patch) | |
tree | 28b87d3431d26a90139cc8ffada16c3c0aebff89 /src/sensor | |
parent | 65f0143f442b479afac08a6550174e6db203d2e5 (diff) | |
download | gnunet-3ccf16e152da02c842220d1d653f5f303eee9b4f.tar.gz gnunet-3ccf16e152da02c842220d1d653f5f303eee9b4f.zip |
DLL instead of multihashmap
Diffstat (limited to 'src/sensor')
-rw-r--r-- | src/sensor/gnunet-service-sensor-analysis.c | 84 |
1 files changed, 44 insertions, 40 deletions
diff --git a/src/sensor/gnunet-service-sensor-analysis.c b/src/sensor/gnunet-service-sensor-analysis.c index 5a613559b..37844f30c 100644 --- a/src/sensor/gnunet-service-sensor-analysis.c +++ b/src/sensor/gnunet-service-sensor-analysis.c | |||
@@ -38,6 +38,16 @@ struct SensorModel | |||
38 | { | 38 | { |
39 | 39 | ||
40 | /* | 40 | /* |
41 | * DLL | ||
42 | */ | ||
43 | struct SensorModel *prev; | ||
44 | |||
45 | /* | ||
46 | * DLL | ||
47 | */ | ||
48 | struct SensorModel *next; | ||
49 | |||
50 | /* | ||
41 | * Pointer to sensor info structure | 51 | * Pointer to sensor info structure |
42 | */ | 52 | */ |
43 | struct SensorInfo *sensor; | 53 | struct SensorInfo *sensor; |
@@ -62,12 +72,7 @@ static char *model_lib_name; | |||
62 | /* | 72 | /* |
63 | * Model handle | 73 | * Model handle |
64 | */ | 74 | */ |
65 | static struct GNUNET_SENSOR_ModelFunctions *model; | 75 | static struct GNUNET_SENSOR_ModelFunctions *model_api; |
66 | |||
67 | /** | ||
68 | * Hashmap of loaded sensor definitions | ||
69 | */ | ||
70 | static struct GNUNET_CONTAINER_MultiHashMap *sensors; | ||
71 | 76 | ||
72 | /* | 77 | /* |
73 | * Handle to peerstore service | 78 | * Handle to peerstore service |
@@ -80,9 +85,14 @@ static struct GNUNET_PEERSTORE_Handle *peerstore; | |||
80 | static const char *analysis_datatypes[] = { "uint64", "double", NULL }; | 85 | static const char *analysis_datatypes[] = { "uint64", "double", NULL }; |
81 | 86 | ||
82 | /* | 87 | /* |
83 | * MultiHashmap of all sensor models | 88 | * Head of DLL of created models |
89 | */ | ||
90 | static struct SensorModel *models_head; | ||
91 | |||
92 | /* | ||
93 | * Tail of DLL of created models | ||
84 | */ | 94 | */ |
85 | static struct GNUNET_CONTAINER_MultiHashMap *sensor_models; | 95 | static struct SensorModel *models_tail; |
86 | 96 | ||
87 | /** | 97 | /** |
88 | * My peer id | 98 | * My peer id |
@@ -90,17 +100,12 @@ static struct GNUNET_CONTAINER_MultiHashMap *sensor_models; | |||
90 | struct GNUNET_PeerIdentity peerid; | 100 | struct GNUNET_PeerIdentity peerid; |
91 | 101 | ||
92 | /* | 102 | /* |
93 | * TODO: document | 103 | * Destroy a created model |
94 | */ | 104 | */ |
95 | static int | 105 | static void |
96 | destroy_sensor_model (void *cls, | 106 | destroy_sensor_model (struct SensorModel *sensor_model) |
97 | const struct GNUNET_HashCode *key, | ||
98 | void *value) | ||
99 | { | 107 | { |
100 | struct SensorModel *sensor_model = value; | 108 | GNUNET_assert (NULL != sensor_model); |
101 | |||
102 | if (NULL == sensor_model) | ||
103 | return GNUNET_YES; | ||
104 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 109 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
105 | "Destroying sensor model for `%s'.\n", | 110 | "Destroying sensor model for `%s'.\n", |
106 | sensor_model->sensor->name); | 111 | sensor_model->sensor->name); |
@@ -109,7 +114,7 @@ destroy_sensor_model (void *cls, | |||
109 | GNUNET_PEERSTORE_watch_cancel(sensor_model->wc); | 114 | GNUNET_PEERSTORE_watch_cancel(sensor_model->wc); |
110 | sensor_model->wc = NULL; | 115 | sensor_model->wc = NULL; |
111 | } | 116 | } |
112 | return GNUNET_YES; | 117 | sensor_model = NULL; |
113 | } | 118 | } |
114 | 119 | ||
115 | /* | 120 | /* |
@@ -117,19 +122,20 @@ destroy_sensor_model (void *cls, | |||
117 | */ | 122 | */ |
118 | void SENSOR_analysis_stop() | 123 | void SENSOR_analysis_stop() |
119 | { | 124 | { |
125 | struct SensorModel *sm; | ||
120 | 126 | ||
121 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor analysis module.\n"); | 127 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor analysis module.\n"); |
122 | if (NULL != model) | 128 | if (NULL != model_api) |
123 | { | 129 | { |
124 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model)); | 130 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model_api)); |
125 | GNUNET_free (model_lib_name); | 131 | GNUNET_free (model_lib_name); |
126 | model_lib_name = NULL; | 132 | model_lib_name = NULL; |
127 | } | 133 | } |
128 | if (NULL != sensor_models) | 134 | while (NULL != models_head) |
129 | { | 135 | { |
130 | GNUNET_CONTAINER_multihashmap_iterate(sensor_models, &destroy_sensor_model, NULL); | 136 | sm = models_head; |
131 | GNUNET_CONTAINER_multihashmap_destroy(sensor_models); | 137 | destroy_sensor_model(sm); |
132 | sensor_models = NULL; | 138 | GNUNET_CONTAINER_DLL_remove(models_head, models_tail, sm); |
133 | } | 139 | } |
134 | if (NULL != peerstore) | 140 | if (NULL != peerstore) |
135 | { | 141 | { |
@@ -139,7 +145,7 @@ void SENSOR_analysis_stop() | |||
139 | } | 145 | } |
140 | 146 | ||
141 | /* | 147 | /* |
142 | * TODO: document | 148 | * Sensor value watch callback |
143 | */ | 149 | */ |
144 | static int | 150 | static int |
145 | sensor_watcher (void *cls, | 151 | sensor_watcher (void *cls, |
@@ -152,7 +158,13 @@ sensor_watcher (void *cls, | |||
152 | } | 158 | } |
153 | 159 | ||
154 | /* | 160 | /* |
155 | * TODO: document | 161 | * Iterator for defined sensors |
162 | * Creates sensor model for numeric sensors | ||
163 | * | ||
164 | * @param cls unused | ||
165 | * @param key unused | ||
166 | * @param value a 'struct SensorInfo *' with sensor information | ||
167 | * @return #GNUNET_YES to continue iterations | ||
156 | */ | 168 | */ |
157 | static int | 169 | static int |
158 | init_sensor_model (void *cls, | 170 | init_sensor_model (void *cls, |
@@ -180,8 +192,7 @@ init_sensor_model (void *cls, | |||
180 | sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore, | 192 | sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore, |
181 | "sensor", &peerid, sensor->name, | 193 | "sensor", &peerid, sensor->name, |
182 | &sensor_watcher, sensor_model); | 194 | &sensor_watcher, sensor_model); |
183 | GNUNET_CONTAINER_multihashmap_put(sensor_models, key, | 195 | GNUNET_CONTAINER_DLL_insert(models_head, models_tail, sensor_model); |
184 | sensor_model, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
185 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 196 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
186 | "Created sensor model for `%s'.\n", sensor->name); | 197 | "Created sensor model for `%s'.\n", sensor->name); |
187 | return GNUNET_YES; | 198 | return GNUNET_YES; |
@@ -191,15 +202,16 @@ init_sensor_model (void *cls, | |||
191 | * Start the sensor analysis module | 202 | * Start the sensor analysis module |
192 | * | 203 | * |
193 | * @param c our service configuration | 204 | * @param c our service configuration |
194 | * @param sensors_mhm multihashmap of loaded sensors | 205 | * @param sensors multihashmap of loaded sensors |
195 | * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise | 206 | * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise |
196 | */ | 207 | */ |
197 | int | 208 | int |
198 | SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, | 209 | SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, |
199 | struct GNUNET_CONTAINER_MultiHashMap *sensors_mhm) | 210 | struct GNUNET_CONTAINER_MultiHashMap *sensors) |
200 | { | 211 | { |
201 | char *model_name; | 212 | char *model_name; |
202 | 213 | ||
214 | GNUNET_assert(NULL != sensors); | ||
203 | cfg = c; | 215 | cfg = c; |
204 | if (GNUNET_OK != | 216 | if (GNUNET_OK != |
205 | GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL", | 217 | GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL", |
@@ -209,20 +221,13 @@ SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, | |||
209 | return GNUNET_SYSERR; | 221 | return GNUNET_SYSERR; |
210 | } | 222 | } |
211 | GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name); | 223 | GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name); |
212 | model = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg); | 224 | model_api = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg); |
213 | GNUNET_free(model_name); | 225 | GNUNET_free(model_name); |
214 | if(NULL == model) | 226 | if(NULL == model_api) |
215 | { | 227 | { |
216 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name); | 228 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name); |
217 | return GNUNET_SYSERR; | 229 | return GNUNET_SYSERR; |
218 | } | 230 | } |
219 | sensors = sensors_mhm; | ||
220 | if (NULL == sensors) | ||
221 | { | ||
222 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Tried to start analysis before loading sensors.\n")); | ||
223 | SENSOR_analysis_stop(); | ||
224 | return GNUNET_SYSERR; | ||
225 | } | ||
226 | peerstore = GNUNET_PEERSTORE_connect(cfg); | 231 | peerstore = GNUNET_PEERSTORE_connect(cfg); |
227 | if (NULL == peerstore) | 232 | if (NULL == peerstore) |
228 | { | 233 | { |
@@ -231,7 +236,6 @@ SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, | |||
231 | return GNUNET_SYSERR; | 236 | return GNUNET_SYSERR; |
232 | } | 237 | } |
233 | GNUNET_CRYPTO_get_peer_identity(cfg, &peerid); | 238 | GNUNET_CRYPTO_get_peer_identity(cfg, &peerid); |
234 | sensor_models = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); | ||
235 | GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL); | 239 | GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL); |
236 | 240 | ||
237 | return GNUNET_OK; | 241 | return GNUNET_OK; |