blob: 0994719460cfc734880023717b6699ea90c3519a [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 Afanasyevd6655302014-02-28 08:41:28 -08009#include "core/resolver.hpp"
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080010#include "core/logger.hpp"
11
12NFD_LOG_INIT("TcpFactory");
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080013
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080014namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080015
Alexander Afanasyevd6655302014-02-28 08:41:28 -080016TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
17 : m_defaultPort(defaultPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080018{
Alexander Afanasyevd6655302014-02-28 08:41:28 -080019}
20
21shared_ptr<TcpChannel>
22TcpFactory::createChannel(const tcp::Endpoint& endpoint)
23{
24 shared_ptr<TcpChannel> channel = findChannel(endpoint);
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080025 if(static_cast<bool>(channel))
26 return channel;
27
Alexander Afanasyev7329e022014-02-27 14:47:22 -080028 channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint));
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080029 m_channels[endpoint] = channel;
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080030 NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080031 return channel;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032}
33
34shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080035TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080036{
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080037 return createChannel(TcpResolver::syncResolve(localHost, localPort));
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080038}
39
40shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080041TcpFactory::findChannel(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 Afanasyevd6655302014-02-28 08:41:28 -080050void
51TcpFactory::createFace(const FaceUri& uri,
52 const FaceCreatedCallback& onCreated,
53 const FaceConnectFailedCallback& onConnectFailed)
54{
55 resolver::AddressSelector addressSelector = resolver::AnyAddress();
56 if (uri.getScheme() == "tcp4")
57 addressSelector = resolver::Ipv4Address();
58 else if (uri.getScheme() == "tcp6")
59 addressSelector = resolver::Ipv6Address();
60
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080061 TcpResolver::asyncResolve(uri.getDomain(),
62 uri.getPort().empty() ? m_defaultPort : uri.getPort(),
63 bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
64 onCreated, onConnectFailed),
65 onConnectFailed,
66 addressSelector);
Alexander Afanasyevd6655302014-02-28 08:41:28 -080067}
68
69void
70TcpFactory::continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
71 const FaceCreatedCallback& onCreated,
72 const FaceConnectFailedCallback& onConnectFailed)
73{
74 // very simple logic for now
75
76 for (ChannelMap::iterator channel = m_channels.begin();
77 channel != m_channels.end();
78 ++channel)
79 {
80 if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
81 (channel->first.address().is_v6() && endpoint.address().is_v6()))
82 {
83 channel->second->connect(endpoint, onCreated, onConnectFailed);
84 return;
85 }
86 }
87 onConnectFailed("No channels available to connect to "
88 + boost::lexical_cast<std::string>(endpoint));
89}
90
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080091} // namespace nfd