1
0

Router.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "dht/dhtcore/Router.h"
  16. #include "dht/dhtcore/Router_new.h"
  17. #include "dht/dhtcore/NodeStore.h"
  18. #include "dht/dhtcore/RouterModule.h"
  19. #include "dht/dhtcore/SearchRunner.h"
  20. #include "util/Identity.h"
  21. struct Router_pvt
  22. {
  23. struct Router pub;
  24. struct NodeStore* nodeStore;
  25. struct SearchRunner* searchRunner;
  26. struct RouterModule* routerModule;
  27. Identity
  28. };
  29. struct Node_Link* Router_linkForPath(struct Router* r, uint64_t path)
  30. {
  31. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  32. return NodeStore_linkForPath(rr->nodeStore, path);
  33. }
  34. void Router_sendGetPeers(struct Router* r,
  35. struct Address* addr,
  36. uint64_t nearbyLabel,
  37. uint32_t timeoutMilliseconds,
  38. struct Allocator* alloc)
  39. {
  40. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  41. RouterModule_getPeers(addr, nearbyLabel, timeoutMilliseconds, rr->routerModule, alloc);
  42. }
  43. struct Node_Two* Router_lookup(struct Router* r, uint8_t targetAddr[16])
  44. {
  45. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  46. return NodeStore_getBest(rr->nodeStore, targetAddr);
  47. }
  48. void Router_brokenLink(struct Router* r, uint64_t path, uint64_t labelAtErrorHop)
  49. {
  50. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  51. NodeStore_brokenLink(rr->nodeStore, path, labelAtErrorHop);
  52. }
  53. void Router_disconnectedPeer(struct Router* r, uint64_t path)
  54. {
  55. struct Router_pvt* rr = Identity_check((struct Router_pvt*)r);
  56. NodeStore_disconnectedPeer(rr->nodeStore, path);
  57. }
  58. struct Router* Router_new(struct RouterModule* rm,
  59. struct NodeStore* ns,
  60. struct SearchRunner* sr,
  61. struct Allocator* alloc)
  62. {
  63. struct Router_pvt* rp = Allocator_clone(alloc, (&(struct Router_pvt) {
  64. .routerModule = rm,
  65. .nodeStore = ns,
  66. .searchRunner = sr
  67. }));
  68. Identity_set(rp);
  69. return &rp->pub;
  70. }