Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fd188d31a97c9f920be536c287319df6599218a8
[simgrid.git] / src / gras / DataDesc / type_driver_rl.c
1 /* gs_rl_type_driver.c */
2
3 #include "DataDesc/gs_private.h"
4
5 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(NDR_tdriver_rl,NDR);
6 /*
7  * structs
8  */
9 struct s_gs_rl_type_driver {
10         /**/
11         int dummy;
12 };
13
14 struct s_gs_rl_type_bag {
15         gras_dict_t             *incoming_dict;
16
17         gras_dynar_t            *type_dynar;
18         gras_dict_t             *type_dict;
19
20         gras_dynar_t            *message_dynar;
21         gras_dict_t             *message_dict;
22
23         gras_dict_t             *outgoing_dict;
24 };
25
26 struct s_gs_rl_incoming_connection {
27         struct s_gs_connection  *p_connection;
28
29         gras_dynar_t            *type_dynar;
30         gras_dict_t             *type_dict;
31
32         gras_dynar_t            *message_dynar;
33         gras_dict_t             *message_dict;
34 };
35
36 struct s_gs_rl_outgoing_connection {
37         struct s_gs_connection  *p_connection;
38         gras_dict_t             *type_marker_dict;
39         gras_dict_t             *message_marker_dict;
40 };
41
42
43 /*
44  * static vars
45  */
46 static
47 struct s_gs_type_driver_ops *type_driver_ops = NULL;
48
49 static
50 struct s_gs_type_bag_ops    *type_bag_ops    = NULL;
51
52 /*
53  * exported functions, driver part
54  */
55 struct s_gs_type_driver_ops *
56 gs_rl_type_driver(void) {
57
58         if (!type_bag_ops) {
59                 type_bag_ops    = calloc(1, sizeof(struct s_gs_type_bag_ops));
60
61
62                 type_bag_ops->_init                             = gs_rl_bag__init;
63                 type_bag_ops->_exit                             = gs_rl_bag__exit;
64
65
66                 type_bag_ops->register_incoming_connection      = gs_rl_bag_register_incoming_connection;
67                 type_bag_ops->register_outgoing_connection      = gs_rl_bag_register_outgoing_connection;
68
69
70                 type_bag_ops->store_type                        = gs_rl_bag_store_type;
71                 type_bag_ops->store_incoming_type               = gs_rl_bag_store_incoming_type;
72                 type_bag_ops->get_type_by_name                  = gs_rl_bag_get_type_by_name;
73                 type_bag_ops->get_type_by_code                  = gs_rl_bag_get_type_by_code;
74                 type_bag_ops->mark_type                         = gs_rl_bag_mark_type;
75                 type_bag_ops->check_type_mark                   = gs_rl_bag_check_type_mark;
76
77
78                 type_bag_ops->store_message                     = gs_rl_bag_store_message;
79                 type_bag_ops->store_incoming_message            = gs_rl_bag_store_incoming_message;
80                 type_bag_ops->get_message_by_name               = gs_rl_bag_get_message_by_name;
81                 type_bag_ops->get_message_by_code               = gs_rl_bag_get_message_by_code;
82                 type_bag_ops->mark_message                      = gs_rl_bag_mark_message;
83                 type_bag_ops->check_message_mark                = gs_rl_bag_check_message_mark;
84         }
85
86         if (!type_driver_ops) {
87                 type_driver_ops = calloc(1, sizeof(struct s_gs_type_driver_ops));
88
89                 type_driver_ops->_init  = gs_rl__init;
90                 type_driver_ops->_exit  = gs_rl__exit;
91         }
92
93         return type_driver_ops;
94 }
95
96 void
97 gs_rl__init(struct s_gs_type_driver     *p_driver) {
98
99         struct s_gs_rl_type_driver *p_rl = NULL;
100
101         p_rl = calloc(1, sizeof(struct s_gs_rl_type_driver));
102         p_driver->specific      = p_rl;
103         p_driver->bag_ops       = type_bag_ops;
104 }
105
106 void
107 gs_rl__exit(struct s_gs_type_driver     *p_driver) {
108
109         struct s_gs_rl_type_driver *p_rl = p_driver->specific;
110
111         free(p_rl);
112         p_driver->specific = NULL;
113 }
114
115 /*
116  * exported functions, bag part
117  */
118
119 /* management */
120 void
121 gs_rl_bag__init(struct s_gs_type_bag    *p_bag) {
122
123         struct s_gs_rl_type_bag *p_rl = NULL;
124
125         p_rl = calloc(1, sizeof(struct s_gs_rl_type_bag));
126
127         gras_dict_new(&p_rl->incoming_dict);
128
129         gras_dynar_new(&p_rl->type_dynar, sizeof(struct s_gs_type *), NULL);
130         gras_dict_new (&p_rl->type_dict);
131
132         gras_dynar_new(&p_rl->message_dynar, sizeof(struct s_gs_type *), NULL);
133         gras_dict_new (&p_rl->message_dict);
134
135         gras_dict_new (&p_rl->outgoing_dict);
136
137         p_bag->specific         = p_rl;
138         p_bag->bag_ops          = type_bag_ops;
139
140         gs_bootstrap_type_bag(p_bag);
141 }
142
143 void
144 gs_rl_bag__exit(struct s_gs_type_bag    *p_bag) {
145
146         struct s_gs_rl_type_bag *p_rl = p_bag->specific;
147
148         gras_dict_free(&p_rl->type_dict);
149         gras_dynar_free(p_rl->type_dynar);
150
151         free(p_rl);
152         p_bag->specific = NULL;
153 }
154
155 /* connection */
156 void
157 gs_rl_bag_register_incoming_connection(struct s_gs_type_bag     *p_bag,
158                                        struct s_gs_connection   *p_cnx) {
159
160         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
161         struct s_gs_rl_incoming_connection      *p_in   = NULL;
162
163         if (p_cnx->direction != e_gs_connection_direction_incoming)
164                 GS_FAILURE("invalid operation");
165
166         p_in = malloc(sizeof (struct s_gs_rl_incoming_connection));
167
168         p_in->p_connection      = p_cnx;
169
170         gras_dynar_new(&p_in->type_dynar, sizeof(struct s_gs_type *), NULL);
171         gras_dict_new (&p_in->type_dict);
172
173         gras_dynar_new(&p_in->message_dynar, sizeof(struct s_gs_type *), NULL);
174         gras_dict_new (&p_in->message_dict);
175
176         gras_dict_insert_ext(p_rl->incoming_dict,
177                              (char *)&p_cnx,
178                              sizeof(p_cnx),
179                              p_in,
180                              NULL);
181
182         gs_bootstrap_incoming_connection(p_bag, p_cnx);
183 }
184
185
186 void
187 gs_rl_bag_register_outgoing_connection(struct s_gs_type_bag     *p_bag,
188                                        struct s_gs_connection   *p_cnx) {
189
190         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
191         struct s_gs_rl_outgoing_connection      *p_out  = NULL;
192
193         if (p_cnx->direction != e_gs_connection_direction_outgoing)
194                 GS_FAILURE("invalid operation");
195
196         p_out = malloc(sizeof (struct s_gs_rl_outgoing_connection));
197
198         p_out->p_connection     = p_cnx;
199         gras_dict_new(&p_out->type_marker_dict);
200         gras_dict_new(&p_out->message_marker_dict);
201
202         gras_dict_insert_ext(p_rl->outgoing_dict, (char *)&p_cnx, sizeof(p_cnx), p_out, NULL);
203
204         gs_bootstrap_outgoing_connection(p_bag, p_cnx);
205 }
206
207
208 /* types */
209 void
210 gs_rl_bag_store_type(struct s_gs_type_bag       *p_bag,
211                      struct s_gs_connection     *p_cnx,
212                      struct s_gs_type           *p_type) {
213
214         struct s_gs_rl_type_bag  *p_rl          = p_bag->specific;
215         struct s_gs_type        **pp_type       = NULL;
216
217         pp_type  = malloc(sizeof(struct s_gs_type *));
218         *pp_type = p_type;
219
220         if (p_cnx) {
221                 struct s_gs_rl_incoming_connection      *p_in   = NULL;
222
223                 if (p_cnx->direction != e_gs_connection_direction_incoming)
224                         GS_FAILURE("invalid operation");
225
226                 gras_dict_retrieve_ext(p_rl->incoming_dict,
227                                        (char *)&p_cnx,
228                                        sizeof(p_cnx),
229                                        (void **)&p_in);
230
231
232                 p_type->code = gras_dynar_length(p_in->type_dynar);
233                 gras_dynar_insert_at(p_in->type_dynar, p_type->code, pp_type);
234                 gras_dict_insert(p_in->type_dict, p_type->name, pp_type, NULL);
235         } else {
236                 p_type->code = gras_dynar_length(p_rl->type_dynar);
237                 gras_dynar_insert_at(p_rl->type_dynar, p_type->code, pp_type);
238                 gras_dict_insert(p_rl->type_dict, p_type->name, pp_type, NULL);
239         }
240 }
241
242 void
243 gs_rl_bag_store_incoming_type(struct s_gs_type_bag      *p_bag,
244                               struct s_gs_connection    *p_cnx,
245                               struct s_gs_type          *p_type) {
246
247         struct s_gs_rl_type_bag                  *p_rl          = p_bag->specific;
248         struct s_gs_rl_incoming_connection       *p_in          = NULL;
249         struct s_gs_type                        **pp_type       = NULL;
250
251         if (p_cnx->direction != e_gs_connection_direction_incoming)
252                 GS_FAILURE("invalid operation");
253
254         gras_dict_retrieve_ext(p_rl->incoming_dict,
255                                (char *)&p_cnx,
256                                sizeof(p_cnx),
257                                (void **)&p_in);
258
259         pp_type  = malloc(sizeof(struct s_gs_type *));
260         *pp_type = p_type;
261
262         gras_dynar_set(p_in->type_dynar, p_type->code, pp_type);
263         gras_dict_insert(p_in->type_dict, p_type->name, pp_type, NULL);
264 }
265
266 struct s_gs_type *
267 gs_rl_bag_get_type_by_name(struct s_gs_type_bag         *p_bag,
268                            struct s_gs_connection       *p_cnx,
269                            const char                   *name) {
270
271         struct s_gs_rl_type_bag  *p_rl          = p_bag->specific;
272         struct s_gs_type        **pp_type       = NULL;
273
274         if (p_cnx) {
275                 struct s_gs_rl_incoming_connection       *p_in          = NULL;
276
277                 if (p_cnx->direction != e_gs_connection_direction_incoming)
278                         GS_FAILURE("invalid operation");
279
280                 gras_dict_retrieve_ext(p_rl->incoming_dict,
281                                        (char *)&p_cnx,
282                                        sizeof(p_cnx),
283                                        (void **)&p_in);
284
285                 gras_dict_retrieve(p_in->type_dict, name, (void **)&pp_type);
286         } else {
287                 gras_dict_retrieve(p_rl->type_dict, name, (void **)&pp_type);
288         }
289
290         if (!pp_type) {
291                 DEBUG1("Get type by name '%s': not found",name);
292                 return NULL;
293         }
294
295         DEBUG1("Get type by name '%s': found",name);
296         return *pp_type;
297 }
298
299 struct s_gs_type *
300 gs_rl_bag_get_type_by_code(struct s_gs_type_bag         *p_bag,
301                            struct s_gs_connection       *p_cnx,
302                            int                           code) {
303
304         struct s_gs_rl_type_bag *p_rl   = p_bag->specific;
305         struct s_gs_type        *p_type = NULL;
306
307         if (p_cnx) {
308                 struct s_gs_rl_incoming_connection       *p_in          = NULL;
309
310                 if (p_cnx->direction != e_gs_connection_direction_incoming)
311                         GS_FAILURE("invalid operation");
312
313                 gras_dict_retrieve_ext(p_rl->incoming_dict,
314                                        (char *)&p_cnx,
315                                        sizeof(p_cnx),
316                                        (void **)&p_in);
317
318                 if ((unsigned int)code < gras_dynar_length(p_in->type_dynar)) {
319                         gras_dynar_get(p_in->type_dynar, code, (void **)&p_type);
320                 }
321         } else {
322                 if ((unsigned int)code < gras_dynar_length(p_rl->type_dynar)) {
323                         gras_dynar_get(p_rl->type_dynar, code, (void **)&p_type);
324                 }
325         }
326
327         return p_type;
328 }
329
330 void
331 gs_rl_bag_mark_type(struct s_gs_type_bag        *p_bag,
332                     struct s_gs_connection      *p_cnx,
333                     const char                  *name) {
334
335         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
336         struct s_gs_rl_outgoing_connection      *p_out  = NULL;
337
338         if (p_cnx->direction != e_gs_connection_direction_outgoing)
339                 GS_FAILURE("invalid operation");
340
341         gras_dict_retrieve_ext(p_rl->outgoing_dict, (char *)&p_cnx, sizeof(p_cnx), (void **)&p_out);
342
343         if (!p_out)
344                 GS_FAILURE("unregistered connection");
345
346         gras_dict_insert(p_out->type_marker_dict, name, strdup(name), NULL);
347 }
348
349 int
350 gs_rl_bag_check_type_mark(struct s_gs_type_bag          *p_bag,
351                           struct s_gs_connection        *p_cnx,
352                           const char                    *name) {
353
354         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
355         struct s_gs_rl_outgoing_connection      *p_out  = NULL;
356         char                                    *result = NULL;
357
358         if (p_cnx->direction != e_gs_connection_direction_outgoing)
359                 GS_FAILURE("invalid operation");
360
361         gras_dict_retrieve_ext(p_rl->outgoing_dict, (char *)&p_cnx, sizeof(p_cnx), (void **)&p_out);
362
363         if (!p_out)
364                 GS_FAILURE("unregistered connection");
365
366         gras_dict_retrieve(p_out->type_marker_dict, name, (void **)&result);
367
368         return !!result;
369 }
370
371 /* messages */
372 void
373 gs_rl_bag_store_message(struct s_gs_type_bag    *p_bag,
374                         struct s_gs_connection  *p_cnx,
375                         struct s_gs_message     *p_message) {
376
377         struct s_gs_rl_type_bag  *p_rl          = p_bag->specific;
378         struct s_gs_message     **pp_message    = NULL;
379
380         pp_message       = malloc(sizeof(struct s_gs_message *));
381         *pp_message = p_message;
382
383         if (p_cnx) {
384                 struct s_gs_rl_incoming_connection      *p_in   = NULL;
385
386                 if (p_cnx->direction != e_gs_connection_direction_incoming)
387                         GS_FAILURE("invalid operation");
388
389                 gras_dict_retrieve_ext(p_rl->incoming_dict,
390                                        (char *)&p_cnx,
391                                        sizeof(p_cnx),
392                                        (void **)&p_in);
393
394
395                 p_message->code = gras_dynar_length(p_in->message_dynar);
396                 gras_dynar_insert_at(p_in->message_dynar, p_message->code, pp_message);
397                 gras_dict_insert(p_in->message_dict, p_message->name, pp_message, NULL);
398         } else {
399                 p_message->code = gras_dynar_length(p_rl->message_dynar);
400                 gras_dynar_insert_at(p_rl->message_dynar, p_message->code, pp_message);
401                 gras_dict_insert(p_rl->message_dict, p_message->name, pp_message, NULL);
402         }
403 }
404
405 void
406 gs_rl_bag_store_incoming_message(struct s_gs_type_bag   *p_bag,
407                                  struct s_gs_connection *p_cnx,
408                                  struct s_gs_message    *p_message) {
409
410         struct s_gs_rl_type_bag                  *p_rl          = p_bag->specific;
411         struct s_gs_rl_incoming_connection       *p_in          = NULL;
412         struct s_gs_message                     **pp_message    = NULL;
413
414         if (p_cnx->direction != e_gs_connection_direction_incoming)
415                 GS_FAILURE("invalid operation");
416
417         gras_dict_retrieve_ext(p_rl->incoming_dict,
418                                (char *)&p_cnx,
419                                sizeof(p_cnx),
420                                (void **)&p_in);
421
422         pp_message       = malloc(sizeof(struct s_gs_message *));
423         *pp_message = p_message;
424
425         gras_dynar_set(p_in->message_dynar, p_message->code, pp_message);
426         gras_dict_insert(p_in->message_dict, p_message->name, pp_message, NULL);
427 }
428
429 struct s_gs_message *
430 gs_rl_bag_get_message_by_name(struct s_gs_type_bag      *p_bag,
431                               struct s_gs_connection    *p_cnx,
432                               const char                *name) {
433
434         struct s_gs_rl_type_bag  *p_rl          = p_bag->specific;
435         struct s_gs_message     **pp_message    = NULL;
436
437         if (p_cnx) {
438                 struct s_gs_rl_incoming_connection       *p_in          = NULL;
439
440                 if (p_cnx->direction != e_gs_connection_direction_incoming)
441                         GS_FAILURE("invalid operation");
442
443                 gras_dict_retrieve_ext(p_rl->incoming_dict,
444                                        (char *)&p_cnx,
445                                        sizeof(p_cnx),
446                                        (void **)&p_in);
447
448                 gras_dict_retrieve(p_in->message_dict, name, (void **)&pp_message);
449         } else {
450                 gras_dict_retrieve(p_rl->message_dict, name, (void **)&pp_message);
451         }
452
453         if (!pp_message) {
454                 return NULL;
455         }
456
457         return *pp_message;
458 }
459
460 struct s_gs_message *
461 gs_rl_bag_get_message_by_code(struct s_gs_type_bag      *p_bag,
462                               struct s_gs_connection    *p_cnx,
463                               int                        code) {
464
465         struct s_gs_rl_type_bag *p_rl   = p_bag->specific;
466         struct s_gs_message     *p_message      = NULL;
467
468         if (p_cnx) {
469                 struct s_gs_rl_incoming_connection       *p_in          = NULL;
470
471                 if (p_cnx->direction != e_gs_connection_direction_incoming)
472                         GS_FAILURE("invalid operation");
473
474                 gras_dict_retrieve_ext(p_rl->incoming_dict,
475                                        (char *)&p_cnx,
476                                        sizeof(p_cnx),
477                                        (void **)&p_in);
478
479                 if ((unsigned int)code < gras_dynar_length(p_in->message_dynar)) {
480                         gras_dynar_get(p_in->message_dynar, code, (void **)&p_message);
481                 }
482         } else {
483                 if ((unsigned int)code < gras_dynar_length(p_rl->message_dynar)) {
484                         gras_dynar_get(p_rl->message_dynar, code, (void **)&p_message);
485                 }
486         }
487
488         return p_message;
489 }
490
491 void
492 gs_rl_bag_mark_message(struct s_gs_type_bag     *p_bag,
493                        struct s_gs_connection   *p_cnx,
494                        const char               *name) {
495
496         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
497         struct s_gs_rl_outgoing_connection      *p_out  = NULL;
498
499         if (p_cnx->direction != e_gs_connection_direction_outgoing)
500                 GS_FAILURE("invalid operation");
501
502         gras_dict_retrieve_ext(p_rl->outgoing_dict, (char *)&p_cnx, sizeof(p_cnx), (void **)&p_out);
503
504         if (!p_out)
505                 GS_FAILURE("unregistered connection");
506
507         gras_dict_insert(p_out->message_marker_dict, name, strdup(name), NULL);
508 }
509
510 int
511 gs_rl_bag_check_message_mark(struct s_gs_type_bag       *p_bag,
512                              struct s_gs_connection     *p_cnx,
513                              const char                 *name) {
514
515         struct s_gs_rl_type_bag                 *p_rl   = p_bag->specific;
516         struct s_gs_rl_outgoing_connection      *p_out  = NULL;
517         char                                    *result = NULL;
518
519         if (p_cnx->direction != e_gs_connection_direction_outgoing)
520                 GS_FAILURE("invalid operation");
521
522         gras_dict_retrieve_ext(p_rl->outgoing_dict, (char *)&p_cnx, sizeof(p_cnx), (void **)&p_out);
523
524         if (!p_out)
525                 GS_FAILURE("unregistered connection");
526
527         gras_dict_retrieve(p_out->message_marker_dict, name, (void **)&result);
528
529         return !!result;
530 }
531
532