SearchRunner.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef SearchRunner_H
  16. #define SearchRunner_H
  17. #ifdef SUBNODE
  18. #error "this file should not be included in subnode"
  19. #endif
  20. #include "dht/dhtcore/RouterModule.h"
  21. #include "dht/dhtcore/NodeStore.h"
  22. #include "dht/dhtcore/RumorMill.h"
  23. #include "util/log/Log.h"
  24. #include "util/events/EventBase.h"
  25. #include "util/Bits.h"
  26. #include "memory/Allocator.h"
  27. #include "util/Linker.h"
  28. Linker_require("dht/dhtcore/SearchRunner.c");
  29. struct SearchRunner_SearchData
  30. {
  31. /** What we're searching for */
  32. uint8_t target[16];
  33. /** Who we last asked about it. */
  34. struct Address lastNodeAsked;
  35. /** How many people we have asked. */
  36. int totalRequests;
  37. /** Number of searches which are currently active. */
  38. int activeSearches;
  39. };
  40. struct SearchRunner
  41. {
  42. int unused;
  43. };
  44. #define SearchRunner_DEFAULT_MAX_CONCURRENT_SEARCHES 30
  45. /** The maximum number of requests to make before calling a search failed. */
  46. #define SearchRunner_DEFAULT_MAX_REQUESTS 8
  47. /** If the search found something, the maximum number of requests to make before call it done. */
  48. #define SearchRunner_DEFAULT_MAX_REQUESTS_IF_FOUND 8
  49. /**
  50. * Start a search.
  51. * The returned promise will have it's callback called for each result of the search and
  52. * then it will be called with 0 milliseconds lag and NULL response indicating the search is over.
  53. *
  54. * @param searchTarget the address to search for.
  55. * @param maxRequests the number of requests to make before terminating the search.
  56. * @param maxRequestsIfFound maximum number of requests if a find has been made.
  57. * @param runner the search runner
  58. * @param alloc an allocator for the search, free this to cancel the search
  59. */
  60. struct RouterModule_Promise* SearchRunner_search(uint8_t target[16],
  61. int maxRequests,
  62. int maxRequestsIfFound,
  63. struct SearchRunner* searchRunner,
  64. struct Allocator* allocator);
  65. /**
  66. * Show an active search.
  67. *
  68. * @param runner
  69. * @param number the search number
  70. * @param alloc the allocator to use for the output
  71. * @return an active search or null if number exceeds the index of the
  72. * highest numbered active search.
  73. */
  74. struct SearchRunner_SearchData* SearchRunner_showActiveSearch(struct SearchRunner* runner,
  75. int number,
  76. struct Allocator* alloc);
  77. struct SearchRunner* SearchRunner_new(struct NodeStore* nodeStore,
  78. struct Log* logger,
  79. struct EventBase* base,
  80. struct RouterModule* module,
  81. uint8_t myAddress[16],
  82. struct RumorMill* rumorMill,
  83. struct Allocator* alloc);
  84. #endif