Swarm.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum 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. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Swarm.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #pragma once
  19. #include <unordered_map>
  20. #include <libdevcore/Common.h>
  21. #include <libdevcore/FixedHash.h>
  22. #include <libdevcrypto/Common.h>
  23. namespace dev
  24. {
  25. class WebThreeDirect;
  26. class IPFS;
  27. namespace bzz
  28. {
  29. DEV_SIMPLE_EXCEPTION(ResourceNotAvailable);
  30. /// Pinned, read-only, content.
  31. class Pinned
  32. {
  33. friend class Client;
  34. public:
  35. Pinned() = default;
  36. explicit operator bool() const { return !!m_content; }
  37. operator bytes const&() const { return *m_content; }
  38. operator bytesConstRef() const { return bytesConstRef(&*m_content); }
  39. private:
  40. Pinned(std::shared_ptr<bytes> const& _d): m_content(_d) {}
  41. std::shared_ptr<bytes> m_content;
  42. };
  43. using Pinneds = std::vector<Pinned>;
  44. /// Basic interface for Swarm.
  45. class Interface
  46. {
  47. public:
  48. virtual ~Interface();
  49. virtual Pinned put(bytes const& _data) = 0;
  50. virtual Pinned get(h256 const& _hash) = 0;
  51. virtual Pinneds insertBundle(bytesConstRef _bundle) = 0;
  52. };
  53. class Client;
  54. /// Placeholder for Swarm.
  55. class Client: public Interface
  56. {
  57. public:
  58. Client(WebThreeDirect* _web3);
  59. void setPutAccount(Secret const& _s) { m_putAccount = _s; }
  60. Pinned put(bytes const& _data) override;
  61. Pinned get(h256 const& _hash) override;
  62. Pinneds insertBundle(bytesConstRef _bundle) override;
  63. private:
  64. std::unordered_map<h256, std::shared_ptr<bytes>> m_cache;
  65. WebThreeDirect* m_owner;
  66. std::shared_ptr<IPFS> m_ipfs;
  67. Secret m_putAccount;
  68. };
  69. }
  70. }