blob: d301350ec6bd04ef2f501f02a19d1acd3b6a0406 [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 Afanasyevd6655302014-02-28 08:41:28 -08008#include "core/resolver.hpp"
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -08009#include "core/logger.hpp"
10
11NFD_LOG_INIT("TcpFactory");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080012
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080014
Alexander Afanasyevd6655302014-02-28 08:41:28 -080015TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
16 : m_defaultPort(defaultPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080017{
Alexander Afanasyevd6655302014-02-28 08:41:28 -080018}
19
20shared_ptr<TcpChannel>
21TcpFactory::createChannel(const tcp::Endpoint& endpoint)
22{
23 shared_ptr<TcpChannel> channel = findChannel(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080024 if(static_cast<bool>(channel))
25 return channel;
26
Junxiao Shi61e3cc52014-03-03 20:40:28 -070027 channel = make_shared<TcpChannel>(boost::cref(endpoint));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080028 m_channels[endpoint] = channel;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080029 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080030 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080031}
32
33shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080034TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080035{
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080036 return createChannel(TcpResolver::syncResolve(localHost, localPort));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080037}
38
39shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080040TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080041{
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080042 ChannelMap::iterator i = m_channels.find(localEndpoint);
43 if (i != m_channels.end())
44 return i->second;
45 else
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080046 return shared_ptr<TcpChannel>();
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080047}
48
Alexander Afanasyevd6655302014-02-28 08:41:28 -080049void
50TcpFactory::createFace(const FaceUri& uri,
51 const FaceCreatedCallback& onCreated,
52 const FaceConnectFailedCallback& onConnectFailed)
53{
54 resolver::AddressSelector addressSelector = resolver::AnyAddress();
55 if (uri.getScheme() == "tcp4")
56 addressSelector = resolver::Ipv4Address();
57 else if (uri.getScheme() == "tcp6")
58 addressSelector = resolver::Ipv6Address();
59
Junxiao Shi61e3cc52014-03-03 20:40:28 -070060 TcpResolver::asyncResolve(uri.getHost(),
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080061 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
62 bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
63 onCreated, onConnectFailed),
64 onConnectFailed,
65 addressSelector);
Alexander Afanasyevd6655302014-02-28 08:41:28 -080066}
67
68void
69TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
70 const FaceCreatedCallback& onCreated,
71 const FaceConnectFailedCallback& onConnectFailed)
72{
73 // very simple logic for now
74
75 for (ChannelMap::iterator channel = m_channels.begin();
76 channel != m_channels.end();
77 ++channel)
78 {
79 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
80 (channel->first.address().is_v6() && endpoint.address().is_v6()))
81 {
82 channel->second->connect(endpoint, onCreated, onConnectFailed);
83 return;
84 }
85 }
86 onConnectFailed("No channels available to connect to "
87 + boost::lexical_cast<std::string>(endpoint));
88}
89
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080090} // namespace nfd