SearchStore.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 SearchStore_H
  16. #define SearchStore_H
  17. #ifdef SUBNODE
  18. #error "this file should not be included in subnode"
  19. #endif
  20. #include "dht/Address.h"
  21. #include "memory/Allocator.h"
  22. #include "benc/Object.h"
  23. #include "util/log/Log.h"
  24. #include "util/Linker.h"
  25. Linker_require("dht/dhtcore/SearchStore.c");
  26. #include <stdint.h>
  27. /**
  28. * The number of nodes which will be held in a buffer when performing a search.
  29. * It is important that this number is large enough because when a search yields results, the
  30. * nodes which helped in get to those results have their reach number recalculated and if
  31. * they are prematurely evicted, they will not have their number recalculated.
  32. */
  33. #define SearchStore_SEARCH_NODES 256
  34. /*--------------------Structures--------------------*/
  35. struct SearchStore
  36. {
  37. /** The means of getting memory to store each search. */
  38. struct Allocator* const allocator;
  39. struct Log* const logger;
  40. };
  41. /** Represents a single search */
  42. struct SearchStore_Search
  43. {
  44. struct SearchStore* const store;
  45. struct Allocator* const alloc;
  46. void* callbackContext;
  47. };
  48. struct SearchStore_Node;
  49. struct SearchStore_Node
  50. {
  51. /** Number of milliseconds since the epoch when the search request was sent to this node. */
  52. uint64_t timeOfRequest;
  53. /** The search. */
  54. struct SearchStore_Search* search;
  55. /** The address of this node. */
  56. struct Address address;
  57. };
  58. /*--------------------Prototypes--------------------*/
  59. /**
  60. * Create a new SearchStore.
  61. *
  62. * @param allocator the means of aquiring memory for the new store.
  63. * @param logger
  64. */
  65. struct SearchStore* SearchStore_new(struct Allocator* allocator, struct Log* logger);
  66. /**
  67. * Create a new search.
  68. *
  69. * @param searchTarget the ID of the thing which we are searching for.
  70. * @param store the SearchStore to allocate the search in.
  71. * @param alloc the allocator to use for allocating this search.
  72. * @return the new search or NULL if MAX_SEARCHES are already running.
  73. */
  74. struct SearchStore_Search* SearchStore_newSearch(uint8_t searchTarget[16],
  75. struct SearchStore* store,
  76. struct Allocator* alloc);
  77. /**
  78. * Add a node to a search.
  79. *
  80. * @param address the address of the node to add.
  81. * @param search the search to add the node to.
  82. * @return -1 if this node has already been asked as part of this search, 0 otherwise.
  83. */
  84. int SearchStore_addNodeToSearch(struct Address* addr, struct SearchStore_Search* search);
  85. /**
  86. * Get the next node to ask in this search.
  87. * This will get the last node to be added to the search which has not been sent a request yet.
  88. *
  89. * @param search the search to get the node for.
  90. * @param allocator the allocator to use for allocating the memory to store the output.
  91. * @return the node which is copied from the storage to the allocated space.
  92. */
  93. struct SearchStore_Node* SearchStore_getNextNode(struct SearchStore_Search* search);
  94. #endif