DHTModuleRegistry.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "memory/BufferAllocator.h"
  19. #include "io/Reader.h"
  20. #include "io/Writer.h"
  21. #include "benc/Object.h"
  22. #include "benc/serialization/BencSerializer.h"
  23. #include "benc/serialization/standard/StandardBencSerializer.h"
  24. #include "util/Assert.h"
  25. #include "util/platform/libc/string.h"
  26. #define DEBUG2(x, y)
  27. /* #define DEBUG2(x, y) fprintf(stderr, x, y); fflush(stderr) */
  28. /** This defines what format the registry will be serialized in. */
  29. #define SERIALIZER StandardBencSerializer_get()
  30. struct DHTModuleRegistry* DHTModuleRegistry_new(struct Allocator* allocator)
  31. {
  32. struct DHTModuleRegistry* reg =
  33. Allocator_calloc(allocator, sizeof(struct DHTModuleRegistry), 1);
  34. reg->allocator = allocator;
  35. reg->members = Allocator_calloc(allocator, sizeof(char*), 1);
  36. return reg;
  37. }
  38. int DHTModuleRegistry_register(struct DHTModule* module,
  39. struct DHTModuleRegistry* registry)
  40. {
  41. registry->members = Allocator_realloc(registry->allocator,
  42. registry->members,
  43. sizeof(char*) * (registry->memberCount + 2));
  44. registry->members[registry->memberCount] = module;
  45. registry->memberCount++;
  46. registry->members[registry->memberCount] = NULL;
  47. return 0;
  48. }
  49. void DHTModuleRegistry_handleIncoming(struct DHTMessage* message,
  50. const struct DHTModuleRegistry* registry)
  51. {
  52. if (!(message && registry && registry->members && registry->memberCount)) {
  53. return;
  54. }
  55. struct DHTModule** firstModulePtr = registry->members;
  56. struct DHTModule** modulePtr = registry->members + registry->memberCount - 1;
  57. struct DHTModule* module;
  58. while (modulePtr >= firstModulePtr) {
  59. module = *modulePtr;
  60. if (module && module->handleIncoming) {
  61. DEBUG2("<< calling: %s->handleIncoming\n", module->name);
  62. if (module->handleIncoming(message, module->context) != 0) {
  63. // TODO: Call a debugger with all unhandlable messages?
  64. return;
  65. }
  66. } else {
  67. DEBUG2("<< skipping %s->handleIncoming\n", module->name);
  68. }
  69. modulePtr--;
  70. }
  71. }
  72. static int dhtModuleHandleOutgoing(struct DHTModule* module, struct DHTMessage* message)
  73. {
  74. Assert_true(module != NULL);
  75. if (module->handleOutgoing) {
  76. DEBUG2(">> calling: %s->handleOutgoing\n", module->name);
  77. return module->handleOutgoing(message, module->context);
  78. } else {
  79. DEBUG2(">> skipping: %s->handleOutgoing\n", module->name);
  80. }
  81. return 0;
  82. }
  83. /**
  84. * Do something to every module which is registered.
  85. * @param doThis the callback.
  86. * @param registry state.
  87. */
  88. static void forEachModule(int (*doThis)(struct DHTModule* module, struct DHTMessage* message),
  89. struct DHTMessage* message,
  90. const struct DHTModuleRegistry* registry)
  91. {
  92. struct DHTModule** modulePtr = registry->members;
  93. struct DHTModule* module = *modulePtr;
  94. while (module) {
  95. if (doThis(module, message) != 0) {
  96. return;
  97. }
  98. modulePtr++;
  99. module = *modulePtr;
  100. }
  101. }
  102. void DHTModuleRegistry_handleOutgoing(struct DHTMessage* message,
  103. const struct DHTModuleRegistry* registry)
  104. {
  105. forEachModule(dhtModuleHandleOutgoing, message, registry);
  106. }