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 | |
| 7 | #include "tcp-channel-factory.hpp" |
| 8 | |
| 9 | namespace ndn { |
| 10 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 11 | TcpChannelFactory::TcpChannelFactory(boost::asio::io_service& ioService) |
| 12 | : m_ioService(ioService) |
| 13 | { |
| 14 | } |
| 15 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 16 | shared_ptr<TcpChannel> |
| 17 | TcpChannelFactory::create(const tcp::Endpoint& endpoint) |
| 18 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 19 | shared_ptr<TcpChannel> channel = find(endpoint); |
| 20 | if(static_cast<bool>(channel)) |
| 21 | return channel; |
| 22 | |
| 23 | channel = make_shared<TcpChannel>(boost::ref(m_ioService), boost::cref(endpoint)); |
| 24 | m_channels[endpoint] = channel; |
| 25 | return channel; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | shared_ptr<TcpChannel> |
| 29 | TcpChannelFactory::create(const std::string& localHost, const std::string& localPort) |
| 30 | { |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 31 | using boost::asio::ip::tcp; |
| 32 | |
| 33 | tcp::resolver::query query(localHost, localPort); |
| 34 | // shared_ptr<tcp::resolver> resolver = |
| 35 | // make_shared<tcp::resolver>(boost::ref(m_ioService)); |
| 36 | tcp::resolver resolver(m_ioService); |
| 37 | |
| 38 | tcp::resolver::iterator end; |
| 39 | tcp::resolver::iterator i = resolver.resolve(query); |
| 40 | if (i == end) |
| 41 | return shared_ptr<ndn::TcpChannel>(); |
| 42 | |
| 43 | return create(*i); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | shared_ptr<TcpChannel> |
| 47 | TcpChannelFactory::find(const tcp::Endpoint& localEndpoint) |
| 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 |
| 53 | return shared_ptr<ndn::TcpChannel>(); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace ndn |