blob: 3464d79034b1afc249a5dd9d6ed9aa39d9f70fcd [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080020 */
21
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "common.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080023
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "unix-transport.hpp"
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080025#include "stream-transport.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080026
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080027#include "../face.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070028#include "util/face-uri.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080029
30namespace ndn {
31
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080032UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080033 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080034{
35}
36
37UnixTransport::~UnixTransport()
38{
39}
40
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060041std::string
42UnixTransport::getDefaultSocketName(const ConfigFile& config)
43{
44 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060045
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060046 try
47 {
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070048 const util::FaceUri uri(parsed.get<std::string>("transport"));
49
50 if (uri.getScheme() != "unix")
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060051 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070052 BOOST_THROW_EXCEPTION(Transport::Error("Cannot create UnixTransport from \"" +
53 uri.getScheme() + "\" URI"));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070054 }
55
56 if (!uri.getPath().empty())
57 {
58 return uri.getPath();
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060059 }
60 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070061 catch (const boost::property_tree::ptree_bad_path& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060062 {
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070063 // no transport specified
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060064 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070065 catch (const boost::property_tree::ptree_bad_data& error)
66 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070067 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070068 }
69 catch (const util::FaceUri::Error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060070 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070071 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060072 }
73
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060074 // Assume the default nfd.sock location.
75 return "/var/run/nfd.sock";
76}
77
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070078shared_ptr<UnixTransport>
79UnixTransport::create(const ConfigFile& config)
80{
81 return make_shared<UnixTransport>(getDefaultSocketName(config));
82}
83
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080084void
85UnixTransport::connect(boost::asio::io_service& ioService,
86 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080087{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080088 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080089 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080090
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070091 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080092 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080093
94 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080095}
96
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080097void
98UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080099{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700100 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800101 m_impl->send(wire);
102}
103
104void
105UnixTransport::send(const Block& header, const Block& payload)
106{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700107 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800108 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800109}
110
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800111void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800112UnixTransport::close()
113{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700114 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800115 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700116 m_impl.reset();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800117}
118
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000119void
120UnixTransport::pause()
121{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700122 if (static_cast<bool>(m_impl)) {
123 m_impl->pause();
124 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000125}
126
127void
128UnixTransport::resume()
129{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700130 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000131 m_impl->resume();
132}
133
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800134}