// Copyright (c) 2011-2015 The Cryptonote developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #pragma once #include #include #include #include #include #include #include "BlockingQueue.h" #include "ConsoleTools.h" namespace Common { class AsyncConsoleReader { public: AsyncConsoleReader(); ~AsyncConsoleReader(); void start(); bool getline(std::string& line); void stop(); bool stopped() const; private: void consoleThread(); bool waitInput(); std::atomic m_stop; std::thread m_thread; BlockingQueue m_queue; }; class ConsoleHandler { public: ~ConsoleHandler(); typedef std::function &)> ConsoleCommandHandler; std::string getUsage() const; void setHandler(const std::string& command, const ConsoleCommandHandler& handler, const std::string& usage = ""); void requestStop(); bool runCommand(const std::vector& cmdAndArgs); void start(bool startThread = true, const std::string& prompt = "", Console::Color promptColor = Console::Color::Default); void stop(); void wait(); private: typedef std::map> CommandHandlersMap; virtual void handleCommand(const std::string& cmd); void handlerThread(); std::thread m_thread; std::string m_prompt; Console::Color m_promptColor = Console::Color::Default; CommandHandlersMap m_handlers; AsyncConsoleReader m_consoleReader; }; }