Janitor_admin.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "memory/Allocator.h"
  16. #include "admin/Admin.h"
  17. #include "dht/dhtcore/Janitor.h"
  18. #include "dht/dhtcore/Janitor_admin.h"
  19. struct Context {
  20. struct Admin* admin;
  21. struct Allocator* alloc;
  22. struct Janitor* janitor;
  23. Identity
  24. };
  25. static struct RumorMill* getRumorMill(struct Context* ctx, String* name)
  26. {
  27. if (String_equals(String_CONST("externalMill"), name)) {
  28. return ctx->janitor->externalMill;
  29. } else if (String_equals(String_CONST("linkMill"), name)) {
  30. return ctx->janitor->linkMill;
  31. } else if (String_equals(String_CONST("nodeMill"), name)) {
  32. return ctx->janitor->nodeMill;
  33. } else if (String_equals(String_CONST("dhtMill"), name)) {
  34. return ctx->janitor->dhtMill;
  35. } else if (String_equals(String_CONST("splitMill"), name)) {
  36. return ctx->janitor->splitMill;
  37. } else {
  38. return NULL;
  39. }
  40. }
  41. #define ENTRIES_PER_PAGE 4
  42. static void dumpRumorMill(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  43. {
  44. struct Context* ctx = Identity_check((struct Context*) vcontext);
  45. Dict* out = Dict_new(requestAlloc);
  46. struct RumorMill* rm = getRumorMill(ctx, Dict_getStringC(args, "mill"));
  47. if (!rm) {
  48. Dict_putStringC(out,
  49. "error",
  50. String_CONST("mill must be one of "
  51. "[externalMill,linkMill,nodeMill,dhtMill,splitMill]"),
  52. requestAlloc);
  53. Admin_sendMessage(out, txid, ctx->admin);
  54. return;
  55. }
  56. int64_t* page = Dict_getIntC(args, "page");
  57. int ctr = (page) ? *page * ENTRIES_PER_PAGE : 0;
  58. List* table = List_new(requestAlloc);
  59. for (int i = 0; i < ENTRIES_PER_PAGE && ctr < rm->count; i++) {
  60. String* addr = Address_toStringKey(&rm->addresses[ctr++], requestAlloc);
  61. List_addString(table, addr, requestAlloc);
  62. }
  63. Dict_putListC(out, "addresses", table, requestAlloc);
  64. Dict_putIntC(out, "total", rm->count, requestAlloc);
  65. Admin_sendMessage(out, txid, ctx->admin);
  66. }
  67. void Janitor_admin_register(struct Janitor* janitor, struct Admin* admin, struct Allocator* alloc)
  68. {
  69. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  70. .admin = admin,
  71. .alloc = alloc,
  72. .janitor = janitor
  73. }));
  74. Identity_set(ctx);
  75. Admin_registerFunction("Janitor_dumpRumorMill", dumpRumorMill, ctx, true,
  76. ((struct Admin_FunctionArg[]) {
  77. { .name = "page", .required = 1, .type = "Int" },
  78. { .name = "mill", .required = 1, .type = "String" },
  79. }), admin);
  80. }