Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 "unix-transport.hpp" |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 23 | #include "stream-transport-impl.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | #include "../face.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 26 | #include "net/face-uri.hpp" |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 27 | #include "util/logger.hpp" |
| 28 | |
| 29 | NDN_LOG_INIT(ndn.UnixTransport); |
| 30 | // DEBUG level: connect, close, pause, resume. |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 34 | UnixTransport::UnixTransport(const std::string& unixSocket) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 35 | : m_unixSocket(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 39 | UnixTransport::~UnixTransport() = default; |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 40 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 41 | std::string |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 42 | UnixTransport::getSocketNameFromUri(const std::string& uriString) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 43 | { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 44 | // Assume the default nfd.sock location. |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 45 | std::string path = "/var/run/nfd.sock"; |
| 46 | |
| 47 | if (uriString.empty()) { |
| 48 | return path; |
| 49 | } |
| 50 | |
| 51 | try { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 52 | const FaceUri uri(uriString); |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 53 | |
| 54 | if (uri.getScheme() != "unix") { |
| 55 | BOOST_THROW_EXCEPTION(Error("Cannot create UnixTransport from \"" + |
| 56 | uri.getScheme() + "\" URI")); |
| 57 | } |
| 58 | |
| 59 | if (!uri.getPath().empty()) { |
| 60 | path = uri.getPath(); |
| 61 | } |
| 62 | } |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 63 | catch (const FaceUri::Error& error) { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 64 | BOOST_THROW_EXCEPTION(Error(error.what())); |
| 65 | } |
| 66 | |
| 67 | return path; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 70 | shared_ptr<UnixTransport> |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 71 | UnixTransport::create(const std::string& uri) |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 73 | return make_shared<UnixTransport>(getSocketNameFromUri(uri)); |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 76 | void |
| 77 | UnixTransport::connect(boost::asio::io_service& ioService, |
| 78 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 79 | { |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 80 | NDN_LOG_DEBUG("connect path=" << m_unixSocket); |
| 81 | |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 82 | if (m_impl == nullptr) { |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 83 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 85 | m_impl = make_shared<Impl>(ref(*this), ref(ioService)); |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 86 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 87 | |
| 88 | m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket)); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 91 | void |
| 92 | UnixTransport::send(const Block& wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 93 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 94 | BOOST_ASSERT(m_impl != nullptr); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 95 | m_impl->send(wire); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | UnixTransport::send(const Block& header, const Block& payload) |
| 100 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 101 | BOOST_ASSERT(m_impl != nullptr); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 102 | m_impl->send(header, payload); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 105 | void |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 106 | UnixTransport::close() |
| 107 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 108 | BOOST_ASSERT(m_impl != nullptr); |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 109 | NDN_LOG_DEBUG("close"); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 110 | m_impl->close(); |
Alexander Afanasyev | bc5830a | 2014-07-11 15:02:38 -0700 | [diff] [blame] | 111 | m_impl.reset(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 114 | void |
| 115 | UnixTransport::pause() |
| 116 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 117 | if (m_impl != nullptr) { |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 118 | NDN_LOG_DEBUG("pause"); |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 119 | m_impl->pause(); |
| 120 | } |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
| 124 | UnixTransport::resume() |
| 125 | { |
Junxiao Shi | 446de3c | 2016-07-25 22:38:16 +0000 | [diff] [blame] | 126 | BOOST_ASSERT(m_impl != nullptr); |
Junxiao Shi | 020123a | 2017-08-08 14:33:48 +0000 | [diff] [blame] | 127 | NDN_LOG_DEBUG("resume"); |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 128 | m_impl->resume(); |
| 129 | } |
| 130 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 131 | } // namespace ndn |