blob: 34ccf8383d148a516cb44a5d6c2532e6555bab81 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi020123a2017-08-08 14:33:48 +00002/*
Junxiao Shi25467942017-06-30 02:53:14 +00003 * Copyright (c) 2013-2017 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 "unix-transport.hpp"
Junxiao Shi446de3c2016-07-25 22:38:16 +000023#include "stream-transport-impl.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080024
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025#include "../face.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000026#include "net/face-uri.hpp"
Junxiao Shi020123a2017-08-08 14:33:48 +000027#include "util/logger.hpp"
28
29NDN_LOG_INIT(ndn.UnixTransport);
30// DEBUG level: connect, close, pause, resume.
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080031
32namespace ndn {
33
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080034UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080035 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080036{
37}
38
Junxiao Shi020123a2017-08-08 14:33:48 +000039UnixTransport::~UnixTransport() = default;
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080040
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060041std::string
Alexander Afanasyev57e00362016-06-23 13:22:54 -070042UnixTransport::getSocketNameFromUri(const std::string& uriString)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060043{
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060044 // Assume the default nfd.sock location.
Alexander Afanasyev57e00362016-06-23 13:22:54 -070045 std::string path = "/var/run/nfd.sock";
46
47 if (uriString.empty()) {
48 return path;
49 }
50
51 try {
Junxiao Shi25467942017-06-30 02:53:14 +000052 const FaceUri uri(uriString);
Alexander Afanasyev57e00362016-06-23 13:22:54 -070053
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 Shi25467942017-06-30 02:53:14 +000063 catch (const FaceUri::Error& error) {
Alexander Afanasyev57e00362016-06-23 13:22:54 -070064 BOOST_THROW_EXCEPTION(Error(error.what()));
65 }
66
67 return path;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060068}
69
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070070shared_ptr<UnixTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070071UnixTransport::create(const std::string& uri)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070072{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070073 return make_shared<UnixTransport>(getSocketNameFromUri(uri));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070074}
75
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080076void
77UnixTransport::connect(boost::asio::io_service& ioService,
78 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080079{
Junxiao Shi020123a2017-08-08 14:33:48 +000080 NDN_LOG_DEBUG("connect path=" << m_unixSocket);
81
Junxiao Shi446de3c2016-07-25 22:38:16 +000082 if (m_impl == nullptr) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080083 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080084
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070085 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080086 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080087
88 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080089}
90
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080091void
92UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080093{
Junxiao Shi446de3c2016-07-25 22:38:16 +000094 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080095 m_impl->send(wire);
96}
97
98void
99UnixTransport::send(const Block& header, const Block& payload)
100{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000101 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800102 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800103}
104
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800105void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800106UnixTransport::close()
107{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000108 BOOST_ASSERT(m_impl != nullptr);
Junxiao Shi020123a2017-08-08 14:33:48 +0000109 NDN_LOG_DEBUG("close");
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800110 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700111 m_impl.reset();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800112}
113
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000114void
115UnixTransport::pause()
116{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000117 if (m_impl != nullptr) {
Junxiao Shi020123a2017-08-08 14:33:48 +0000118 NDN_LOG_DEBUG("pause");
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700119 m_impl->pause();
120 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000121}
122
123void
124UnixTransport::resume()
125{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000126 BOOST_ASSERT(m_impl != nullptr);
Junxiao Shi020123a2017-08-08 14:33:48 +0000127 NDN_LOG_DEBUG("resume");
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000128 m_impl->resume();
129}
130
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700131} // namespace ndn