DHTModuleRegistry.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <http://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)
  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. return reg;
  29. }
  30. int DHTModuleRegistry_register(struct DHTModule* module,
  31. struct DHTModuleRegistry* registry)
  32. {
  33. registry->members = Allocator_realloc(registry->allocator,
  34. registry->members,
  35. sizeof(char*) * (registry->memberCount + 2));
  36. registry->members[registry->memberCount] = module;
  37. registry->memberCount++;
  38. registry->members[registry->memberCount] = NULL;
  39. return 0;
  40. }
  41. void DHTModuleRegistry_handleIncoming(struct DHTMessage* message,
  42. const struct DHTModuleRegistry* registry)
  43. {
  44. if (!(message && registry && registry->members && registry->memberCount)) {
  45. return;
  46. }
  47. struct DHTModule** firstModulePtr = registry->members;
  48. struct DHTModule** modulePtr = registry->members + registry->memberCount - 1;
  49. struct DHTModule* module;
  50. while (modulePtr >= firstModulePtr) {
  51. module = *modulePtr;
  52. if (module && module->handleIncoming) {
  53. DEBUG2("<< calling: %s->handleIncoming\n", module->name);
  54. if (module->handleIncoming(message, module->context) != 0) {
  55. // TODO(cjd): Call a debugger with all unhandlable messages?
  56. return;
  57. }
  58. } else {
  59. DEBUG2("<< skipping %s->handleIncoming\n", module->name);
  60. }
  61. modulePtr--;
  62. }
  63. }
  64. static int dhtModuleHandleOutgoing(struct DHTModule* module, struct DHTMessage* message)
  65. {
  66. Assert_ifParanoid(module);
  67. if (module->handleOutgoing) {
  68. DEBUG2(">> calling: %s->handleOutgoing\n", module->name);
  69. return module->handleOutgoing(message, module->context);
  70. } else {
  71. DEBUG2(">> skipping: %s->handleOutgoing\n", module->name);
  72. }
  73. return 0;
  74. }
  75. /**
  76. * Do something to every module which is registered.
  77. * @param doThis the callback.
  78. * @param registry state.
  79. */
  80. static void forEachModule(int (*doThis)(struct DHTModule* module, struct DHTMessage* message),
  81. struct DHTMessage* message,
  82. const struct DHTModuleRegistry* registry)
  83. {
  84. struct DHTModule** modulePtr = registry->members;
  85. struct DHTModule* module = *modulePtr;
  86. while (module) {
  87. if (doThis(module, message) != 0) {
  88. return;
  89. }
  90. modulePtr++;
  91. module = *modulePtr;
  92. }
  93. }
  94. void DHTModuleRegistry_handleOutgoing(struct DHTMessage* message,
  95. const struct DHTModuleRegistry* registry)
  96. {
  97. forEachModule(dhtModuleHandleOutgoing, message, registry);
  98. }