blob: 072ce2e917f13a01d1a54fd6ac3ade210569f214 [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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -08007#include "tcp-factory.hpp"
Alexander Afanasyev7329e022014-02-27 14:47:22 -08008#include "core/global-io.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08009
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080010namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080011
12shared_ptr<TcpChannel>
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080013TcpFactory::create(const tcp::Endpoint& endpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080014{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080015 shared_ptr<TcpChannel> channel = find(endpoint);
16 if(static_cast<bool>(channel))
17 return channel;
18
Alexander Afanasyev7329e022014-02-27 14:47:22 -080019 channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080020 m_channels[endpoint] = channel;
21 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080022}
23
24shared_ptr<TcpChannel>
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080025TcpFactory::create(const std::string& localHost, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080026{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080027 using boost::asio::ip::tcp;
Alexander Afanasyev7329e022014-02-27 14:47:22 -080028
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080029 tcp::resolver::query query(localHost, localPort);
Alexander Afanasyev7329e022014-02-27 14:47:22 -080030 tcp::resolver resolver(getGlobalIoService());
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080031
32 tcp::resolver::iterator end;
33 tcp::resolver::iterator i = resolver.resolve(query);
34 if (i == end)
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035 return shared_ptr<TcpChannel>();
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080036
37 return create(*i);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080038}
39
40shared_ptr<TcpChannel>
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080041TcpFactory::find(const tcp::Endpoint& localEndpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080042{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080043 ChannelMap::iterator i = m_channels.find(localEndpoint);
44 if (i != m_channels.end())
45 return i->second;
46 else
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080047 return shared_ptr<TcpChannel>();
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080048}
49
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080050} // namespace nfd