node.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright © 2017 Simon Désaulniers
  3. * Author: Simon Désaulniers <sim.desaulniers@gmail.com>
  4. * Milisarge <milisarge@gmail.com>
  5. *
  6. * Bu program özgür yazılımdır:
  7. * Özgür Yazılım Vakfı(Free Software Foundation) tarafından yayımlanan
  8. * GNU Genel Kamu Lisansı’nın sürüm 3 veya
  9. * isteğinize bağlı olarak daha sonraki sürümlerinin hükümleri altında
  10. * yeniden dağıtabilir ve/veya değiştirebilirsiniz.
  11. * Bu program, yararlı olması umuduyla dağıtılmış olup, programın BİR TEMİNATI YOKTUR;
  12. * TİCARETİNİN YAPILABİLİRLİĞİNE VE ÖZEL BİR AMAÇ İÇİN UYGUNLUĞUNA dair bir teminat da vermez.
  13. * Ayrıntılar için GNU Genel Kamu Lisansı’na göz atınız.
  14. * Bu programla birlikte GNU Genel Kamu Lisansı’nın bir kopyasını elde etmiş olmanız gerekir.
  15. * Eğer elinize ulaşmadıysa <http://www.gnu.org/licenses/> adresine bakınız.
  16. */
  17. #pragma once
  18. #include <cstdint>
  19. #include <string>
  20. #include <memory>
  21. #include <opendht/dhtrunner.h>
  22. #include <opendht/value.h>
  23. #include <opendht/infohash.h>
  24. #include <opendht/rng.h>
  25. #include <opendht/callbacks.h>
  26. namespace Milis {
  27. class Node {
  28. static const constexpr char* DEFAULT_BOOTSTRAP_NODE = "bootstrap.ring.cx";
  29. static const constexpr char* DEFAULT_BOOTSTRAP_PORT = "4222";
  30. static const constexpr char* CONNECTION_FAILURE_MSG = "err.. Failed to connect to the DHT.";
  31. static const constexpr char* OPERATION_FAILURE_MSG = "err.. DHT operation failed.";
  32. public:
  33. using PastedCallback = std::function<void(std::vector<dht::Blob>)>;
  34. Node() {}
  35. virtual ~Node () {}
  36. void run(uint16_t port = 0, std::string bootstrap_hostname = DEFAULT_BOOTSTRAP_NODE, std::string bootstrap_port = DEFAULT_BOOTSTRAP_PORT) {
  37. if (running_)
  38. return;
  39. node_.run(port, dht::crypto::generateIdentity(), true);
  40. node_.bootstrap(bootstrap_hostname, bootstrap_port);
  41. running_ = true;
  42. };
  43. void stop() {
  44. std::condition_variable cv;
  45. std::mutex m;
  46. std::atomic_bool done {false};
  47. node_.shutdown([&]()
  48. {
  49. std::lock_guard<std::mutex> lk(m);
  50. done = true;
  51. cv.notify_all();
  52. });
  53. // wait for shutdown
  54. std::unique_lock<std::mutex> lk(m);
  55. cv.wait(lk, [&](){ return done.load(); });
  56. node_.join();
  57. }
  58. /**
  59. * Pastes a blob on the DHT under a given code. If no callback, the function
  60. * blocks until pasting on the DHT is done.
  61. *
  62. * @param blob The blob to paste.
  63. * @param cb A function to execute when paste is done. If empty, the
  64. * function will block until done.
  65. *
  66. * @return true if success, else false.
  67. */
  68. bool put(const std::string& code, dht::Blob&& blob, dht::DoneCallbackSimple&& cb = {});
  69. /**
  70. * Recover a blob under a given code.
  71. *
  72. * @param code The code to lookup.
  73. * @param cb A function to execute when the pasted blob is retrieved.
  74. */
  75. void get(const std::string& code, PastedCallback&& cb);
  76. /**
  77. * Recover blob values under a given code. This function blocks until the
  78. * DHT has satisfied the request.
  79. *
  80. * @param code The code to lookup.
  81. *
  82. * @return the blobs.
  83. */
  84. std::vector<dht::Blob> get(const std::string& code);
  85. private:
  86. dht::DhtRunner node_;
  87. bool running_ {false};
  88. std::uniform_int_distribution<uint32_t> codeDist_;
  89. std::mt19937_64 rand_;
  90. };
  91. } /* dpaste */