blob: 295632a6a438f22b6c15c5970409512c9bd085b9 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080028
29namespace ndn {
30
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080031UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080032 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080033{
34}
35
36UnixTransport::~UnixTransport()
37{
38}
39
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060040std::string
41UnixTransport::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 Afanasyev2a7f7202014-04-23 14:25:29 -070048 catch (boost::property_tree::ptree_bad_path& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060049 {
50 // unix_socket not present, continue
51 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070052 catch (boost::property_tree::ptree_bad_data& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060053 {
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 Afanasyevf7ca3202014-02-14 22:28:31 -080082void
83UnixTransport::connect(boost::asio::io_service& ioService,
84 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080085{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080087 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080088
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070089 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080090 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091
92 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080093}
94
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080095void
96UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080097{
Alexander Afanasyev984ad192014-05-02 19:11:15 -070098 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080099 m_impl->send(wire);
100}
101
102void
103UnixTransport::send(const Block& header, const Block& payload)
104{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700105 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800106 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800107}
108
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -0800109void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800110UnixTransport::close()
111{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700112 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800113 m_impl->close();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800114}
115
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000116void
117UnixTransport::pause()
118{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700119 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000120 m_impl->pause();
121}
122
123void
124UnixTransport::resume()
125{
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700126 BOOST_ASSERT(static_cast<bool>(m_impl));
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000127 m_impl->resume();
128}
129
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800130}