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