blob: f2e44aa226cf57bb7ddea43455e4970a500e78ce [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 "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"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070026#include "util/face-uri.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080027
28namespace ndn {
29
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080030UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080031 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080032{
33}
34
35UnixTransport::~UnixTransport()
36{
37}
38
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060039std::string
Alexander Afanasyev57e00362016-06-23 13:22:54 -070040UnixTransport::getSocketNameFromUri(const std::string& uriString)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060041{
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060042 // Assume the default nfd.sock location.
Alexander Afanasyev57e00362016-06-23 13:22:54 -070043 std::string path = "/var/run/nfd.sock";
44
45 if (uriString.empty()) {
46 return path;
47 }
48
49 try {
50 const util::FaceUri uri(uriString);
51
52 if (uri.getScheme() != "unix") {
53 BOOST_THROW_EXCEPTION(Error("Cannot create UnixTransport from \"" +
54 uri.getScheme() + "\" URI"));
55 }
56
57 if (!uri.getPath().empty()) {
58 path = uri.getPath();
59 }
60 }
61 catch (const util::FaceUri::Error& error) {
62 BOOST_THROW_EXCEPTION(Error(error.what()));
63 }
64
65 return path;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060066}
67
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070068shared_ptr<UnixTransport>
Alexander Afanasyev57e00362016-06-23 13:22:54 -070069UnixTransport::create(const std::string& uri)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070070{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070071 return make_shared<UnixTransport>(getSocketNameFromUri(uri));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070072}
73
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080074void
75UnixTransport::connect(boost::asio::io_service& ioService,
76 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080077{
Junxiao Shi446de3c2016-07-25 22:38:16 +000078 if (m_impl == nullptr) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080079 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080080
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070081 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080082 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080083
84 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080085}
86
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080087void
88UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080089{
Junxiao Shi446de3c2016-07-25 22:38:16 +000090 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091 m_impl->send(wire);
92}
93
94void
95UnixTransport::send(const Block& header, const Block& payload)
96{
Junxiao Shi446de3c2016-07-25 22:38:16 +000097 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080098 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080099}
100
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800101void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800102UnixTransport::close()
103{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000104 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800105 m_impl->close();
Alexander Afanasyevbc5830a2014-07-11 15:02:38 -0700106 m_impl.reset();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800107}
108
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000109void
110UnixTransport::pause()
111{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000112 if (m_impl != nullptr) {
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700113 m_impl->pause();
114 }
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000115}
116
117void
118UnixTransport::resume()
119{
Junxiao Shi446de3c2016-07-25 22:38:16 +0000120 BOOST_ASSERT(m_impl != nullptr);
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000121 m_impl->resume();
122}
123
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700124} // namespace ndn