event_template.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @file event_template.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <misc/offset.h>
  31. #include <misc/debug.h>
  32. #include <misc/balloc.h>
  33. #include <ncd/modules/event_template.h>
  34. #define TemplateLog(o, ...) NCDModuleInst_Backend_Log((o)->i, (o)->blog_channel, __VA_ARGS__)
  35. static void enable_event (event_template *o)
  36. {
  37. ASSERT(!LinkedList1_IsEmpty(&o->events_list))
  38. ASSERT(!o->enabled)
  39. // get event
  40. struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->events_list), struct event_template_event, events_list_node);
  41. // remove from events list
  42. LinkedList1_Remove(&o->events_list, &e->events_list_node);
  43. // grab enabled map
  44. o->enabled_map = e->map;
  45. // append to free list
  46. LinkedList1_Append(&o->free_list, &e->events_list_node);
  47. // set enabled
  48. o->enabled = 1;
  49. // signal up
  50. NCDModuleInst_Backend_Up(o->i);
  51. }
  52. void event_template_new (event_template *o, NCDModuleInst *i, int blog_channel, int maxevents, void *user,
  53. event_template_func_free func_free)
  54. {
  55. ASSERT(maxevents > 0)
  56. // init arguments
  57. o->i = i;
  58. o->blog_channel = blog_channel;
  59. o->user = user;
  60. o->func_free = func_free;
  61. // allocate events array
  62. if (!(o->events = BAllocArray(maxevents, sizeof(o->events[0])))) {
  63. TemplateLog(o, BLOG_ERROR, "BAllocArray failed");
  64. goto fail0;
  65. }
  66. // init events lists
  67. LinkedList1_Init(&o->events_list);
  68. LinkedList1_Init(&o->free_list);
  69. for (int i = 0; i < maxevents; i++) {
  70. LinkedList1_Append(&o->free_list, &o->events[i].events_list_node);
  71. }
  72. // set not enabled
  73. o->enabled = 0;
  74. return;
  75. fail0:
  76. o->func_free(o->user, 1);
  77. return;
  78. }
  79. void event_template_die (event_template *o)
  80. {
  81. // free enabled map
  82. if (o->enabled) {
  83. BStringMap_Free(&o->enabled_map);
  84. }
  85. // free event maps
  86. LinkedList1Node *list_node = LinkedList1_GetFirst(&o->events_list);
  87. while (list_node) {
  88. struct event_template_event *e = UPPER_OBJECT(list_node, struct event_template_event, events_list_node);
  89. BStringMap_Free(&e->map);
  90. list_node = LinkedList1Node_Next(list_node);
  91. }
  92. // free events array
  93. BFree(o->events);
  94. o->func_free(o->user, 0);
  95. return;
  96. }
  97. int event_template_getvar (event_template *o, const char *name, NCDValMem *mem, NCDValRef *out)
  98. {
  99. ASSERT(o->enabled)
  100. ASSERT(name)
  101. const char *val = BStringMap_Get(&o->enabled_map, name);
  102. if (!val) {
  103. return 0;
  104. }
  105. *out = NCDVal_NewString(mem, val);
  106. return 1;
  107. }
  108. void event_template_queue (event_template *o, BStringMap map, int *out_was_empty)
  109. {
  110. ASSERT(!LinkedList1_IsEmpty(&o->free_list))
  111. // get event
  112. struct event_template_event *e = UPPER_OBJECT(LinkedList1_GetFirst(&o->free_list), struct event_template_event, events_list_node);
  113. // remove from free list
  114. LinkedList1_Remove(&o->free_list, &e->events_list_node);
  115. // set map
  116. e->map = map;
  117. // insert to events list
  118. LinkedList1_Append(&o->events_list, &e->events_list_node);
  119. // enable if not already
  120. if (!o->enabled) {
  121. enable_event(o);
  122. *out_was_empty = 1;
  123. } else {
  124. *out_was_empty = 0;
  125. }
  126. }
  127. void event_template_dequeue (event_template *o, int *out_is_empty)
  128. {
  129. ASSERT(o->enabled)
  130. // free enabled map
  131. BStringMap_Free(&o->enabled_map);
  132. // set not enabled
  133. o->enabled = 0;
  134. // signal down
  135. NCDModuleInst_Backend_Down(o->i);
  136. // enable if there are more events
  137. if (!LinkedList1_IsEmpty(&o->events_list)) {
  138. enable_event(o);
  139. *out_is_empty = 0;
  140. } else {
  141. *out_is_empty = 1;
  142. }
  143. }
  144. int event_template_is_enabled (event_template *o)
  145. {
  146. return o->enabled;
  147. }