SearchStore.h 3.5 KB

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