ApiServer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 1883 Thomas Edison - All Rights Reserved
  2. * You may use, distribute and modify this code under the
  3. * terms of the GPLv3 license, which unfortunately won't be
  4. * written for another century.
  5. *
  6. * You should have received a copy of the LICENSE file with
  7. * this file.
  8. */
  9. #pragma once
  10. #include <regex>
  11. #include <boost/asio.hpp>
  12. #include <boost/bind/bind.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <json/json.h>
  15. #include <libeth/Farm.h>
  16. #include <libeth/Miner.h>
  17. #include <libpool/PoolManager.h>
  18. using namespace dev;
  19. using namespace dev::eth;
  20. using namespace std::chrono;
  21. using boost::asio::ip::tcp;
  22. using namespace boost::placeholders;
  23. class ApiConnection {
  24. public:
  25. ApiConnection(boost::asio::io_service::strand& _strand, int id, bool readonly, string password);
  26. ~ApiConnection() = default;
  27. void start();
  28. Json::Value getMinerStat1();
  29. using Disconnected = std::function<void(int const&)>;
  30. void onDisconnected(Disconnected const& _handler) { m_onDisconnected = _handler; }
  31. int getId() { return m_sessionId; }
  32. tcp::socket& socket() { return m_socket; }
  33. private:
  34. void disconnect();
  35. void processRequest(Json::Value& jRequest, Json::Value& jResponse);
  36. void recvSocketData();
  37. void onRecvSocketDataCompleted(const boost::system::error_code& ec, std::size_t bytes_transferred);
  38. void sendSocketData(Json::Value const& jReq, bool _disconnect = false);
  39. void sendSocketData(std::string const& _s, bool _disconnect = false);
  40. void onSendSocketDataCompleted(const boost::system::error_code& ec, bool _disconnect = false);
  41. Json::Value getMinerStatDetail();
  42. Json::Value getMinerStatDetailPerMiner(const TelemetryType& _t, std::shared_ptr<Miner> _miner);
  43. std::string getHttpMinerMetrics();
  44. std::string getHttpMinerStatDetail();
  45. Disconnected m_onDisconnected;
  46. int m_sessionId;
  47. tcp::socket m_socket;
  48. boost::asio::io_service::strand& m_io_strand;
  49. boost::asio::streambuf m_sendBuffer;
  50. boost::asio::streambuf m_recvBuffer;
  51. Json::StreamWriterBuilder m_jSwBuilder;
  52. std::string m_message; // The internal message string buffer
  53. bool m_readonly = false;
  54. std::string m_password = "";
  55. bool m_is_authenticated = true;
  56. };
  57. class ApiServer {
  58. public:
  59. ApiServer(string address, int portnum, string password);
  60. bool isRunning() { return m_running.load(std::memory_order_relaxed); };
  61. void start();
  62. void stop();
  63. private:
  64. void begin_accept();
  65. void handle_accept(std::shared_ptr<ApiConnection> session, boost::system::error_code ec);
  66. int lastSessionId = 0;
  67. std::thread m_workThread;
  68. std::atomic<bool> m_readonly = {false};
  69. std::string m_password = "";
  70. std::atomic<bool> m_running = {false};
  71. string m_address;
  72. uint16_t m_portnumber;
  73. tcp::acceptor m_acceptor;
  74. boost::asio::io_service::strand m_io_strand;
  75. std::vector<std::shared_ptr<ApiConnection>> m_sessions;
  76. };