1
0

DHTModuleRegistry.c 4.0 KB

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