//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #if 0 namespace ripple { namespace PeerFinder { class ResolverImp : public Resolver , private Thread , private LeakChecked { private: class Request; struct State { List list; }; typedef SharedData SharedState; SharedState m_state; boost::asio::io_service m_io_service; boost::optional m_work; //-------------------------------------------------------------------------- static boost::asio::ip::tcp::endpoint fromIPAddress ( IPAddress const& ipEndpoint) { if (ipEndpoint.is_v4 ()) { return boost::asio::ip::tcp::endpoint ( boost::asio::ip::address_v4 ( ipEndpoint.to_v4().value), ipEndpoint.port ()); } bassertfalse; return boost::asio::ip::tcp::endpoint (); } //-------------------------------------------------------------------------- class Request : public SharedObject , public List ::Node , private LeakChecked { public: typedef SharedPtr Ptr; typedef boost::asio::ip::tcp Protocol; typedef boost::system::error_code error_code; typedef Protocol::socket socket_type; typedef Protocol::endpoint endpoint_type; ResolverImp& m_owner; boost::asio::io_service& m_io_service; IPAddress m_address; AbstractHandler m_handler; socket_type m_socket; boost::system::error_code m_error; bool m_canAccept; Request (ResolverImp& owner, boost::asio::io_service& io_service, IPAddress const& address, AbstractHandler handler) : m_owner (owner) , m_io_service (io_service) , m_address (address) , m_handler (handler) , m_socket (m_io_service) , m_canAccept (false) { m_owner.add (*this); m_socket.async_connect (fromIPAddress (m_address), wrapHandler (boost::bind (&Request::handle_connect, Ptr(this), boost::asio::placeholders::error), m_handler)); } ~Request () { Result result; result.address = m_address; result.error = m_error; m_io_service.wrap (m_handler) (result); m_owner.remove (*this); } void cancel () { m_socket.cancel(); } void handle_connect (boost::system::error_code ec) { m_error = ec; if (ec) return; m_canAccept = true; } }; //-------------------------------------------------------------------------- void add (Request& request) { SharedState::Access state (m_state); state->list.push_back (request); } void remove (Request& request) { SharedState::Access state (m_state); state->list.erase (state->list.iterator_to (request)); } void run () { m_io_service.run (); } public: ResolverImp () : Thread ("PeerFinder::Resolver") , m_work (boost::in_place (boost::ref (m_io_service))) { startThread (); } ~ResolverImp () { // cancel pending i/o cancel(); // destroy the io_service::work object m_work = boost::none; // signal and wait for the thread to exit gracefully stopThread (); } void cancel () { SharedState::Access state (m_state); for (List ::iterator iter (state->list.begin()); iter != state->list.end(); ++iter) iter->cancel(); } void async_test (IPAddress const& endpoint, AbstractHandler handler) { new Request (*this, m_io_service, endpoint, handler); } }; //------------------------------------------------------------------------------ Resolver* Resolver::New () { return new ResolverImp; } } } #endif