Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 7 | #include "tcp-factory.hpp" |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 8 | #include "core/global-io.hpp" |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 9 | #include "core/resolver.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 13 | TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/) |
| 14 | : m_defaultPort(defaultPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 15 | { |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 16 | } |
| 17 | |
| 18 | shared_ptr<TcpChannel> |
| 19 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 20 | { |
| 21 | shared_ptr<TcpChannel> channel = findChannel(endpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 22 | if(static_cast<bool>(channel)) |
| 23 | return channel; |
| 24 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 25 | channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 26 | m_channels[endpoint] = channel; |
| 27 | return channel; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 31 | TcpFactory::createChannel(const std::string& localHost, const std::string& localPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 33 | using boost::asio::ip::tcp; |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 35 | tcp::resolver::query query(localHost, localPort); |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 36 | tcp::resolver resolver(getGlobalIoService()); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 37 | |
| 38 | tcp::resolver::iterator end; |
| 39 | tcp::resolver::iterator i = resolver.resolve(query); |
| 40 | if (i == end) |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 41 | return shared_ptr<TcpChannel>(); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 43 | return createChannel(*i); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 47 | TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 49 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 50 | if (i != m_channels.end()) |
| 51 | return i->second; |
| 52 | else |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 53 | return shared_ptr<TcpChannel>(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 56 | void |
| 57 | TcpFactory::createFace(const FaceUri& uri, |
| 58 | const FaceCreatedCallback& onCreated, |
| 59 | const FaceConnectFailedCallback& onConnectFailed) |
| 60 | { |
| 61 | resolver::AddressSelector addressSelector = resolver::AnyAddress(); |
| 62 | if (uri.getScheme() == "tcp4") |
| 63 | addressSelector = resolver::Ipv4Address(); |
| 64 | else if (uri.getScheme() == "tcp6") |
| 65 | addressSelector = resolver::Ipv6Address(); |
| 66 | |
| 67 | using boost::asio::ip::tcp; |
| 68 | Resolver<tcp>::asyncResolve(uri.getDomain(), |
| 69 | uri.getPort().empty() ? m_defaultPort : uri.getPort(), |
| 70 | bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1, |
| 71 | onCreated, onConnectFailed), |
| 72 | onConnectFailed, |
| 73 | addressSelector); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint, |
| 78 | const FaceCreatedCallback& onCreated, |
| 79 | const FaceConnectFailedCallback& onConnectFailed) |
| 80 | { |
| 81 | // very simple logic for now |
| 82 | |
| 83 | for (ChannelMap::iterator channel = m_channels.begin(); |
| 84 | channel != m_channels.end(); |
| 85 | ++channel) |
| 86 | { |
| 87 | if ((channel->first.address().is_v4() && endpoint.address().is_v4()) || |
| 88 | (channel->first.address().is_v6() && endpoint.address().is_v6())) |
| 89 | { |
| 90 | channel->second->connect(endpoint, onCreated, onConnectFailed); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | onConnectFailed("No channels available to connect to " |
| 95 | + boost::lexical_cast<std::string>(endpoint)); |
| 96 | } |
| 97 | |
| 98 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 99 | } // namespace nfd |