DHTModuleRegistry.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "dht/DHTModule.h"
  16. #include "dht/DHTModuleRegistry.h"
  17. #include "memory/Allocator.h"
  18. #include "util/Assert.h"
  19. #include <stddef.h> // NULL
  20. #define DEBUG2(x, y)
  21. /* #define DEBUG2(x, y) fprintf(stderr, x, y); fflush(stderr) */
  22. struct DHTModuleRegistry* DHTModuleRegistry_new(struct Allocator* allocator, struct Log* log)
  23. {
  24. struct DHTModuleRegistry* reg =
  25. Allocator_calloc(allocator, sizeof(struct DHTModuleRegistry), 1);
  26. reg->allocator = allocator;
  27. reg->members = Allocator_calloc(allocator, sizeof(char*), 1);
  28. reg->log = log;
  29. return reg;
  30. }
  31. int DHTModuleRegistry_register(struct DHTModule* module,
  32. struct DHTModuleRegistry* registry)
  33. {
  34. registry->members = Allocator_realloc(registry->allocator,
  35. registry->members,
  36. sizeof(char*) * (registry->memberCount + 2));
  37. registry->members[registry->memberCount] = module;
  38. registry->memberCount++;
  39. registry->members[registry->memberCount] = NULL;
  40. return 0;
  41. }
  42. void DHTModuleRegistry_handleIncoming(struct DHTMessage* message,
  43. const struct DHTModuleRegistry* registry)
  44. {
  45. if (!(message && registry && registry->members && registry->memberCount)) {
  46. return;
  47. }
  48. struct DHTModule** firstModulePtr = registry->members;
  49. struct DHTModule** modulePtr = registry->members + registry->memberCount - 1;
  50. struct DHTModule* module;
  51. while (modulePtr >= firstModulePtr) {
  52. module = *modulePtr;
  53. if (module && module->handleIncoming) {
  54. DEBUG2("<< calling: %s->handleIncoming\n", module->name);
  55. if (module->handleIncoming(message, module->context) != 0) {
  56. // TODO(cjd): Call a debugger with all unhandlable messages?
  57. return;
  58. }
  59. } else {
  60. DEBUG2("<< skipping %s->handleIncoming\n", module->name);
  61. }
  62. modulePtr--;
  63. }
  64. }
  65. static int dhtModuleHandleOutgoing(struct DHTModule* module, struct DHTMessage* message)
  66. {
  67. Assert_ifParanoid(module);
  68. if (module->handleOutgoing) {
  69. DEBUG2(">> calling: %s->handleOutgoing\n", module->name);
  70. return module->handleOutgoing(message, module->context);
  71. } else {
  72. DEBUG2(">> skipping: %s->handleOutgoing\n", module->name);
  73. }
  74. return 0;
  75. }
  76. /**
  77. * Do something to every module which is registered.
  78. * @param doThis the callback.
  79. * @param registry state.
  80. */
  81. static void forEachModule(int (*doThis)(struct DHTModule* module, struct DHTMessage* message),
  82. struct DHTMessage* message,
  83. const struct DHTModuleRegistry* registry)
  84. {
  85. struct DHTModule** modulePtr = registry->members;
  86. struct DHTModule* module = *modulePtr;
  87. while (module) {
  88. if (doThis(module, message) != 0) {
  89. return;
  90. }
  91. modulePtr++;
  92. module = *modulePtr;
  93. }
  94. }
  95. void DHTModuleRegistry_handleOutgoing(struct DHTMessage* message,
  96. const struct DHTModuleRegistry* registry)
  97. {
  98. forEachModule(dhtModuleHandleOutgoing, message, registry);
  99. }