blob: 4c6df871bec5eacf2f4c7158721eec73c6e48fd5 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#include "tcp-channel-factory.hpp"
namespace ndn {
TcpChannelFactory::TcpChannelFactory(boost::asio::io_service& ioService)
: m_ioService(ioService)
{
}
shared_ptr<TcpChannel>
TcpChannelFactory::create(const tcp::Endpoint& endpoint)
{
shared_ptr<TcpChannel> channel = find(endpoint);
if(static_cast<bool>(channel))
return channel;
channel = make_shared<TcpChannel>(boost::ref(m_ioService), boost::cref(endpoint));
m_channels[endpoint] = channel;
return channel;
}
shared_ptr<TcpChannel>
TcpChannelFactory::create(const std::string& localHost, const std::string& localPort)
{
using boost::asio::ip::tcp;
tcp::resolver::query query(localHost, localPort);
// shared_ptr<tcp::resolver> resolver =
// make_shared<tcp::resolver>(boost::ref(m_ioService));
tcp::resolver resolver(m_ioService);
tcp::resolver::iterator end;
tcp::resolver::iterator i = resolver.resolve(query);
if (i == end)
return shared_ptr<ndn::TcpChannel>();
return create(*i);
}
shared_ptr<TcpChannel>
TcpChannelFactory::find(const tcp::Endpoint& localEndpoint)
{
ChannelMap::iterator i = m_channels.find(localEndpoint);
if (i != m_channels.end())
return i->second;
else
return shared_ptr<ndn::TcpChannel>();
}
} // namespace ndn