Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | #include "common.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 24 | #include "unix-transport.hpp" |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 25 | #include "stream-transport.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 27 | #include "../face.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 31 | UnixTransport::UnixTransport(const std::string& unixSocket) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 32 | : m_unixSocket(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | UnixTransport::~UnixTransport() |
| 37 | { |
| 38 | } |
| 39 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 40 | std::string |
| 41 | UnixTransport::getDefaultSocketName(const ConfigFile& config) |
| 42 | { |
| 43 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 44 | try |
| 45 | { |
| 46 | return parsed.get<std::string>("unix_socket"); |
| 47 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 48 | catch (boost::property_tree::ptree_bad_path& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 49 | { |
| 50 | // unix_socket not present, continue |
| 51 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 52 | catch (boost::property_tree::ptree_bad_data& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 53 | { |
| 54 | throw ConfigFile::Error(error.what()); |
| 55 | } |
| 56 | |
| 57 | // no unix_socket specified so the default socket name |
| 58 | // depends on the protocol we're using |
| 59 | try |
| 60 | { |
| 61 | const std::string protocol = parsed.get<std::string>("protocol"); |
| 62 | if (protocol == "ndnd-tlv-0.7") |
| 63 | { |
| 64 | return "/tmp/.ndnd.sock"; |
| 65 | } |
| 66 | } |
| 67 | catch (boost::property_tree::ptree_bad_path& error) |
| 68 | { |
| 69 | return "/var/run/nfd.sock"; |
| 70 | } |
| 71 | catch (boost::property_tree::ptree_bad_data& error) |
| 72 | { |
| 73 | throw ConfigFile::Error(error.what()); |
| 74 | } |
| 75 | |
| 76 | // A we made here, then there's no unix_socket specified in the configuration |
| 77 | // file. A protocol is present, but it's not ndnd. |
| 78 | // Assume the default nfd.sock location. |
| 79 | return "/var/run/nfd.sock"; |
| 80 | } |
| 81 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 82 | void |
| 83 | UnixTransport::connect(boost::asio::io_service& ioService, |
| 84 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 85 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 86 | if (!static_cast<bool>(m_impl)) { |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 87 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 89 | m_impl = make_shared<Impl>(ref(*this), ref(ioService)); |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 91 | |
| 92 | m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket)); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 95 | void |
| 96 | UnixTransport::send(const Block& wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 97 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 98 | BOOST_ASSERT(static_cast<bool>(m_impl)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 99 | m_impl->send(wire); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | UnixTransport::send(const Block& header, const Block& payload) |
| 104 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 105 | BOOST_ASSERT(static_cast<bool>(m_impl)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 106 | m_impl->send(header, payload); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 109 | void |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 110 | UnixTransport::close() |
| 111 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 112 | BOOST_ASSERT(static_cast<bool>(m_impl)); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 113 | m_impl->close(); |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame^] | 114 | m_impl.reset(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 117 | void |
| 118 | UnixTransport::pause() |
| 119 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 120 | BOOST_ASSERT(static_cast<bool>(m_impl)); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 121 | m_impl->pause(); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | UnixTransport::resume() |
| 126 | { |
Alexander Afanasyev | 984ad19 | 2014-05-02 19:11:15 -0700 | [diff] [blame] | 127 | BOOST_ASSERT(static_cast<bool>(m_impl)); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 128 | m_impl->resume(); |
| 129 | } |
| 130 | |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 131 | } |