blob: 0c63960e201b80b86b135872dbfc4ce3a0f55bb3 [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 Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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
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 {
52 const util::FaceUri uri(uriString);
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 }
63 catch (const util::FaceUri::Error& error) {
64 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{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080080 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080081 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080082
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070083 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080084 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080085
86 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080087}
88
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080089void
90UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080091{
Alexander Afanasyev984ad192014-05-02 19:11:15 -070092 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080093 m_impl->send(wire);
94}
95
96void
97UnixTransport::send(const Block& header, const Block& payload)
98{
Alexander Afanasyev984ad192014-05-02 19:11:15 -070099 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800100 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800101}
102
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800103void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800104UnixTransport::close()
105{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700106 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800107 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700108 m_impl.reset();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800109}
110
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000111void
112UnixTransport::pause()
113{
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700114 if (static_cast<bool>(m_impl)) {
115 m_impl->pause();
116 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000117}
118
119void
120UnixTransport::resume()
121{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700122 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000123 m_impl->resume();
124}
125
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700126} // namespace ndn