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 | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame^] | 10 | #include "core/logger.hpp" |
| 11 | |
| 12 | NFD_LOG_INIT("TcpFactory"); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 13 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 14 | namespace nfd { |
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 | TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/) |
| 17 | : m_defaultPort(defaultPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 18 | { |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | shared_ptr<TcpChannel> |
| 22 | TcpFactory::createChannel(const tcp::Endpoint& endpoint) |
| 23 | { |
| 24 | shared_ptr<TcpChannel> channel = findChannel(endpoint); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 25 | if(static_cast<bool>(channel)) |
| 26 | return channel; |
| 27 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 28 | channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint)); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 29 | m_channels[endpoint] = channel; |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame^] | 30 | NFD_LOG_DEBUG("Channel [" << endpoint << "] created"); |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 31 | return channel; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 35 | TcpFactory::createChannel(const std::string& localHost, const std::string& localPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 36 | { |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame^] | 37 | return createChannel(TcpResolver::syncResolve(localHost, localPort)); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 41 | TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 43 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 44 | if (i != m_channels.end()) |
| 45 | return i->second; |
| 46 | else |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 47 | return shared_ptr<TcpChannel>(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 50 | void |
| 51 | TcpFactory::createFace(const FaceUri& uri, |
| 52 | const FaceCreatedCallback& onCreated, |
| 53 | const FaceConnectFailedCallback& onConnectFailed) |
| 54 | { |
| 55 | resolver::AddressSelector addressSelector = resolver::AnyAddress(); |
| 56 | if (uri.getScheme() == "tcp4") |
| 57 | addressSelector = resolver::Ipv4Address(); |
| 58 | else if (uri.getScheme() == "tcp6") |
| 59 | addressSelector = resolver::Ipv6Address(); |
| 60 | |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame^] | 61 | TcpResolver::asyncResolve(uri.getDomain(), |
| 62 | uri.getPort().empty() ? m_defaultPort : uri.getPort(), |
| 63 | bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1, |
| 64 | onCreated, onConnectFailed), |
| 65 | onConnectFailed, |
| 66 | addressSelector); |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint, |
| 71 | const FaceCreatedCallback& onCreated, |
| 72 | const FaceConnectFailedCallback& onConnectFailed) |
| 73 | { |
| 74 | // very simple logic for now |
| 75 | |
| 76 | for (ChannelMap::iterator channel = m_channels.begin(); |
| 77 | channel != m_channels.end(); |
| 78 | ++channel) |
| 79 | { |
| 80 | if ((channel->first.address().is_v4() && endpoint.address().is_v4()) || |
| 81 | (channel->first.address().is_v6() && endpoint.address().is_v6())) |
| 82 | { |
| 83 | channel->second->connect(endpoint, onCreated, onConnectFailed); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | onConnectFailed("No channels available to connect to " |
| 88 | + boost::lexical_cast<std::string>(endpoint)); |
| 89 | } |
| 90 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 91 | } // namespace nfd |