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