blob: 4c6df871bec5eacf2f4c7158721eec73c6e48fd5 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- 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
9namespace ndn {
10
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080011TcpChannelFactory::TcpChannelFactory(boost::asio::io_service& ioService)
12 : m_ioService(ioService)
13{
14}
15
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080016shared_ptr<TcpChannel>
17TcpChannelFactory::create(const tcp::Endpoint& endpoint)
18{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080019 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 Afanasyeva9034b02014-01-26 18:32:02 -080026}
27
28shared_ptr<TcpChannel>
29TcpChannelFactory::create(const std::string& localHost, const std::string& localPort)
30{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080031 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 Afanasyeva9034b02014-01-26 18:32:02 -080044}
45
46shared_ptr<TcpChannel>
47TcpChannelFactory::find(const tcp::Endpoint& localEndpoint)
48{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080049 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 Afanasyeva9034b02014-01-26 18:32:02 -080054}
55
56} // namespace ndn