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 | 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> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 13 | TcpFactory::create(const tcp::Endpoint& endpoint) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 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> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 25 | TcpFactory::create(const std::string& localHost, const std::string& localPort) |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 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> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 41 | TcpFactory::find(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 | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 50 | } // namespace nfd |