libamxs  0.6.0
Data Model Synchronization C API
amxs_sync_entry.c File Reference
#include <stdlib.h>
#include <string.h>
#include <amxc/amxc.h>
#include "amxs_sync_entry.h"
#include "amxs_priv.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static void amxs_llist_it_delete_sync_entry (amxc_llist_it_t *it)
 
static int amxs_sync_entry_compare_name (const char *const a, const char *const b)
 
amxs_sync_direction_t amxs_sync_entry_get_initial_direction (const amxs_sync_entry_t *const entry)
 
amxs_status_t amxs_sync_entry_new (amxs_sync_entry_t **entry, const char *a, const char *b, int attributes, amxs_translation_cb_t translation_cb, amxs_action_cb_t action_cb, amxs_sync_entry_type_t type, void *priv)
 
void amxs_sync_entry_delete (amxs_sync_entry_t **entry)
 
amxs_status_t amxs_sync_entry_init (amxs_sync_entry_t *entry, const char *a, const char *b, int attributes, amxs_translation_cb_t translation_cb, amxs_action_cb_t action_cb, amxs_sync_entry_type_t type, void *priv)
 
void amxs_sync_entry_clean (amxs_sync_entry_t *entry)
 
amxs_status_t amxs_sync_entry_copy (amxs_sync_entry_t **dest, amxs_sync_entry_t *src, void *priv)
 
amxs_status_t amxs_sync_entry_add_entry (amxs_sync_entry_t *parent, amxs_sync_entry_t *child)
 
int amxs_sync_entry_compare (amxs_sync_entry_t *entry1, amxs_sync_entry_t *entry2)
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxs_sync_entry.c.

Function Documentation

◆ amxs_llist_it_delete_sync_entry()

static void amxs_llist_it_delete_sync_entry ( amxc_llist_it_t *  it)
static

Definition at line 67 of file amxs_sync_entry.c.

67  {
68  amxs_sync_entry_t* entry = amxc_container_of(it, amxs_sync_entry_t, it);
69 
70  amxs_sync_entry_delete(&entry);
71 }
void amxs_sync_entry_delete(amxs_sync_entry_t **entry)
Definition: amxs_types.h:161

◆ amxs_sync_entry_add_entry()

amxs_status_t amxs_sync_entry_add_entry ( amxs_sync_entry_t parent,
amxs_sync_entry_t child 
)

Definition at line 285 of file amxs_sync_entry.c.

285  {
287  when_null(parent, exit);
288  when_null(child, exit);
289  when_true(parent == child, exit);
290 
291  if((parent->type == amxs_sync_type_invalid) ||
292  ( parent->type == amxs_sync_type_param) ||
293  ( child->type == amxs_sync_type_invalid) ||
294  ( child->type == amxs_sync_type_ctx)) {
295  status = amxs_status_invalid_type;
296  goto exit;
297  }
298 
299  status = amxs_validate_attributes(parent->attributes, child->attributes);
300  when_failed(status, exit);
301  status = amxs_update_child_attributes(parent->attributes, &child->attributes);
302  when_failed(status, exit);
303 
304  amxc_llist_iterate(it, &child->entries) {
305  amxs_sync_entry_t* entry = amxc_container_of(it, amxs_sync_entry_t, it);
306 
307  status = amxs_update_child_attributes(child->attributes, &entry->attributes);
308  when_failed(status, exit);
309  }
310 
311  amxc_llist_iterate(it, &parent->entries) {
312  amxs_sync_entry_t* entry = amxc_container_of(it, amxs_sync_entry_t, it);
313  if(amxs_sync_entry_compare(entry, child) == 0) {
314  status = amxs_status_duplicate;
315  goto exit;
316  }
317  }
318  amxc_llist_append(&parent->entries, &child->it);
319 
320  status = amxs_status_ok;
321 
322 exit:
323  return status;
324 }
amxs_status_t amxs_update_child_attributes(int parent_attr, int *child_attr)
Definition: amxs_common.c:124
amxs_status_t amxs_validate_attributes(int parent_attr, int child_attr)
Definition: amxs_common.c:111
int amxs_sync_entry_compare(amxs_sync_entry_t *entry1, amxs_sync_entry_t *entry2)
@ amxs_status_ok
Definition: amxs_types.h:86
@ amxs_status_unknown_error
Definition: amxs_types.h:90
@ amxs_status_invalid_type
Definition: amxs_types.h:92
@ amxs_status_duplicate
Definition: amxs_types.h:87
enum _amxs_status amxs_status_t
@ amxs_sync_type_param
Definition: amxs_types.h:102
@ amxs_sync_type_invalid
Definition: amxs_types.h:99
@ amxs_sync_type_ctx
Definition: amxs_types.h:100
amxs_sync_entry_type_t type
Definition: amxs_types.h:170
amxc_llist_it_t it
Definition: amxs_types.h:162
int attributes
Definition: amxs_types.h:165
amxc_llist_t entries
Definition: amxs_types.h:169

◆ amxs_sync_entry_clean()

void amxs_sync_entry_clean ( amxs_sync_entry_t entry)

Definition at line 220 of file amxs_sync_entry.c.

220  {
221  when_null(entry, exit);
222 
223  if(entry->type == amxs_sync_type_ctx) {
224  amxp_sigmngr_delete(&entry->sig_mngr);
225  }
226 
227  free(entry->a);
228  entry->a = NULL;
229  free(entry->b);
230  entry->b = NULL;
231  entry->attributes = 0;
232  entry->action_cb = NULL;
233  entry->translation_cb = NULL;
234  entry->type = amxs_sync_type_invalid;
235  entry->priv = NULL;
236  entry->bus_ctx_a = NULL;
237  entry->bus_ctx_b = NULL;
238  amxc_llist_it_take(&entry->it);
239  amxc_llist_clean(&entry->entries, amxs_llist_it_delete_sync_entry);
240  amxc_llist_clean(&entry->subscriptions, amxs_llist_it_delete_subscription);
241  amxc_var_clean(&entry->a_to_b);
242  amxc_var_clean(&entry->b_to_a);
243 
244 exit:
245  return;
246 }
void amxs_llist_it_delete_subscription(amxc_llist_it_t *it)
Definition: amxs_common.c:140
static void amxs_llist_it_delete_sync_entry(amxc_llist_it_t *it)
char * a
Definition: amxs_types.h:163
void * priv
Definition: amxs_types.h:168
amxb_bus_ctx_t * bus_ctx_a
Definition: amxs_types.h:171
amxs_translation_cb_t translation_cb
Definition: amxs_types.h:167
amxc_var_t b_to_a
Definition: amxs_types.h:176
amxc_var_t a_to_b
Definition: amxs_types.h:175
amxc_llist_t subscriptions
Definition: amxs_types.h:173
amxs_action_cb_t action_cb
Definition: amxs_types.h:166
amxp_signal_mngr_t * sig_mngr
Definition: amxs_types.h:174
char * b
Definition: amxs_types.h:164
amxb_bus_ctx_t * bus_ctx_b
Definition: amxs_types.h:172

◆ amxs_sync_entry_compare()

int amxs_sync_entry_compare ( amxs_sync_entry_t entry1,
amxs_sync_entry_t entry2 
)

Definition at line 326 of file amxs_sync_entry.c.

326  {
327  int ret = -1;
328 
329  if(entry1 == entry2) {
330  ret = 0;
331  goto exit;
332  }
333 
334  if((entry1 == NULL) || (entry2 == NULL)) {
335  goto exit;
336  }
337 
338  if(entry1->type != entry2->type) {
339  goto exit;
340  }
341 
342  if(amxs_sync_entry_compare_name(entry1->a, entry2->a) != 0) {
343  goto exit;
344  }
345 
346  if(amxs_sync_entry_compare_name(entry1->b, entry2->b) != 0) {
347  goto exit;
348  }
349 
350  if(((entry1->attributes | entry2->attributes) & (AMXS_SYNC_ONLY_B_TO_A | AMXS_SYNC_ONLY_A_TO_B ))
352  goto exit;
353  }
354 
355  ret = 0;
356 
357 exit:
358  return ret;
359 }
static int amxs_sync_entry_compare_name(const char *const a, const char *const b)
#define AMXS_SYNC_ONLY_B_TO_A
Only synchronize from object B to object A.
Definition: amxs_types.h:195
#define AMXS_SYNC_ONLY_A_TO_B
Only synchronize from object A to object B.
Definition: amxs_types.h:200

◆ amxs_sync_entry_compare_name()

static int amxs_sync_entry_compare_name ( const char *const  a,
const char *const  b 
)
static

Definition at line 73 of file amxs_sync_entry.c.

73  {
74  int ret = -1;
75 
76  if((a == NULL) && (b != NULL)) {
77  goto exit;
78  }
79 
80  if((a != NULL) && (b == NULL)) {
81  goto exit;
82  }
83 
84  if(((a != NULL) && (b != NULL)) && (strcmp(a, b) != 0)) {
85  goto exit;
86  }
87 
88  ret = 0;
89 
90 exit:
91  return ret;
92 }

◆ amxs_sync_entry_copy()

amxs_status_t amxs_sync_entry_copy ( amxs_sync_entry_t **  dest,
amxs_sync_entry_t src,
void *  priv 
)

Definition at line 248 of file amxs_sync_entry.c.

250  {
252  amxs_sync_entry_t* dst_child = NULL;
253  amxs_sync_entry_t* src_child = NULL;
254 
255  when_null(dest, exit);
256  when_null(src, exit);
257 
258  status = amxs_sync_entry_new(dest,
259  src->a,
260  src->b,
261  src->attributes,
262  src->translation_cb,
263  src->action_cb,
264  src->type,
265  priv);
266  when_failed(status, exit);
267 
268  (*dest)->local_dm_a = src->local_dm_a;
269  (*dest)->local_dm_b = src->local_dm_b;
270 
271  amxc_llist_for_each(it, &src->entries) {
272  src_child = amxc_container_of(it, amxs_sync_entry_t, it);
273  amxs_sync_entry_copy(&dst_child, src_child, NULL);
274  status = amxs_sync_entry_add_entry(*dest, dst_child);
275  when_failed(status, exit);
276  }
277 
278 exit:
279  if(status != 0) {
281  }
282  return status;
283 }
amxs_status_t amxs_sync_entry_add_entry(amxs_sync_entry_t *parent, amxs_sync_entry_t *child)
amxs_status_t amxs_sync_entry_new(amxs_sync_entry_t **entry, const char *a, const char *b, int attributes, amxs_translation_cb_t translation_cb, amxs_action_cb_t action_cb, amxs_sync_entry_type_t type, void *priv)
amxs_status_t amxs_sync_entry_copy(amxs_sync_entry_t **dest, amxs_sync_entry_t *src, void *priv)
amxd_dm_t * local_dm_b
Definition: amxs_types.h:178
amxd_dm_t * local_dm_a
Definition: amxs_types.h:177

◆ amxs_sync_entry_delete()

void amxs_sync_entry_delete ( amxs_sync_entry_t **  entry)

Definition at line 128 of file amxs_sync_entry.c.

128  {
129  when_null(entry, exit);
130  when_null(*entry, exit);
131 
132  amxs_sync_entry_clean(*entry);
133 
134  free(*entry);
135  *entry = NULL;
136 
137 exit:
138  return;
139 }
void amxs_sync_entry_clean(amxs_sync_entry_t *entry)

◆ amxs_sync_entry_get_initial_direction()

amxs_sync_direction_t amxs_sync_entry_get_initial_direction ( const amxs_sync_entry_t *const  entry)

Definition at line 94 of file amxs_sync_entry.c.

94  {
96  when_null(entry, exit);
97 
98  direction = (entry->attributes & AMXS_SYNC_INIT_B) == 0 ? amxs_sync_a_to_b : amxs_sync_b_to_a;
99 
100 exit:
101  return direction;
102 }
enum _amxs_sync_direction amxs_sync_direction_t
@ amxs_sync_a_to_b
Definition: amxs_types.h:80
@ amxs_sync_b_to_a
Definition: amxs_types.h:81
@ amxs_sync_invalid
Definition: amxs_types.h:82
#define AMXS_SYNC_INIT_B
Take the initial values from object B.
Definition: amxs_types.h:205

◆ amxs_sync_entry_init()

amxs_status_t amxs_sync_entry_init ( amxs_sync_entry_t entry,
const char *  a,
const char *  b,
int  attributes,
amxs_translation_cb_t  translation_cb,
amxs_action_cb_t  action_cb,
amxs_sync_entry_type_t  type,
void *  priv 
)

Definition at line 141 of file amxs_sync_entry.c.

148  {
150  when_null(entry, exit);
151 
152  status = amxs_validate_attributes(attributes, 0);
153  when_failed(status, exit);
154 
155  memset(entry, 0, sizeof(amxs_sync_entry_t));
156 
157  status = amxs_status_invalid_arg;
158  if((attributes & AMXS_SYNC_ONLY_B_TO_A) == 0) {
159  when_str_empty(a, exit);
160  }
161 
162  if((attributes & AMXS_SYNC_ONLY_A_TO_B) == 0) {
163  when_str_empty(b, exit);
164  }
165 
166  if(a != NULL) {
167  if((type != amxs_sync_type_param) && (a[strlen(a) - 1] != '.')) {
168  status = amxs_status_invalid_arg;
169  goto exit;
170  }
171  entry->a = strdup(a);
172  when_null(entry->a, exit);
173  }
174 
175  if(b != NULL) {
176  if((type != amxs_sync_type_param) && (b[strlen(b) - 1] != '.')) {
177  status = amxs_status_invalid_arg;
178  goto exit;
179  }
180  entry->b = strdup(b);
181  when_null(entry->b, exit);
182  }
183 
184  entry->attributes = attributes;
185  entry->action_cb = action_cb;
186  entry->translation_cb = translation_cb;
187  entry->type = type;
188  entry->priv = priv;
189  entry->bus_ctx_a = NULL;
190  entry->bus_ctx_b = NULL;
191  amxc_llist_it_init(&entry->it);
192  amxc_llist_init(&entry->entries);
193  amxc_llist_init(&entry->subscriptions);
194  amxc_var_init(&entry->a_to_b);
195  amxc_var_init(&entry->b_to_a);
196  entry->local_dm_a = NULL;
197  entry->local_dm_b = NULL;
198 
199  if(type == amxs_sync_type_ctx) {
200  amxc_var_set_type(&entry->a_to_b, AMXC_VAR_ID_HTABLE);
201  amxc_var_set_type(&entry->b_to_a, AMXC_VAR_ID_HTABLE);
202  when_failed(amxp_sigmngr_new(&entry->sig_mngr), exit);
203  amxp_sigmngr_add_signal(entry->sig_mngr, "sync:instance-added");
204  }
205 
206  status = amxs_status_ok;
207 
208 exit:
209  if((status != amxs_status_ok) && (entry != NULL)) {
210  free(entry->a);
211  entry->a = NULL;
212  free(entry->b);
213  entry->b = NULL;
214  amxc_var_clean(&entry->a_to_b);
215  amxc_var_clean(&entry->b_to_a);
216  }
217  return status;
218 }
@ amxs_status_invalid_arg
Definition: amxs_types.h:89

◆ amxs_sync_entry_new()

amxs_status_t amxs_sync_entry_new ( amxs_sync_entry_t **  entry,
const char *  a,
const char *  b,
int  attributes,
amxs_translation_cb_t  translation_cb,
amxs_action_cb_t  action_cb,
amxs_sync_entry_type_t  type,
void *  priv 
)

Definition at line 104 of file amxs_sync_entry.c.

111  {
113  when_null(entry, exit);
114 
115  *entry = (amxs_sync_entry_t*) calloc(1, sizeof(amxs_sync_entry_t));
116  when_null(*entry, exit);
117 
118  status = amxs_sync_entry_init(*entry, a, b, attributes, translation_cb, action_cb, type, priv);
119  if(status != amxs_status_ok) {
120  free(*entry);
121  *entry = NULL;
122  }
123 
124 exit:
125  return status;
126 }
amxs_status_t amxs_sync_entry_init(amxs_sync_entry_t *entry, const char *a, const char *b, int attributes, amxs_translation_cb_t translation_cb, amxs_action_cb_t action_cb, amxs_sync_entry_type_t type, void *priv)