Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 13 | #include "common.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 14 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 15 | #include "unix-transport.hpp" |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 16 | #include "stream-transport.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 18 | #include "../face.hpp" |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 19 | |
| 20 | namespace ndn { |
| 21 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 22 | UnixTransport::UnixTransport(const std::string& unixSocket) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 23 | : m_unixSocket(unixSocket) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
| 27 | UnixTransport::~UnixTransport() |
| 28 | { |
| 29 | } |
| 30 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 31 | std::string |
| 32 | UnixTransport::getDefaultSocketName(const ConfigFile& config) |
| 33 | { |
| 34 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 35 | try |
| 36 | { |
| 37 | return parsed.get<std::string>("unix_socket"); |
| 38 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 39 | catch (boost::property_tree::ptree_bad_path& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 40 | { |
| 41 | // unix_socket not present, continue |
| 42 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 43 | catch (boost::property_tree::ptree_bad_data& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 44 | { |
| 45 | throw ConfigFile::Error(error.what()); |
| 46 | } |
| 47 | |
| 48 | // no unix_socket specified so the default socket name |
| 49 | // depends on the protocol we're using |
| 50 | try |
| 51 | { |
| 52 | const std::string protocol = parsed.get<std::string>("protocol"); |
| 53 | if (protocol == "ndnd-tlv-0.7") |
| 54 | { |
| 55 | return "/tmp/.ndnd.sock"; |
| 56 | } |
| 57 | } |
| 58 | catch (boost::property_tree::ptree_bad_path& error) |
| 59 | { |
| 60 | return "/var/run/nfd.sock"; |
| 61 | } |
| 62 | catch (boost::property_tree::ptree_bad_data& error) |
| 63 | { |
| 64 | throw ConfigFile::Error(error.what()); |
| 65 | } |
| 66 | |
| 67 | // A we made here, then there's no unix_socket specified in the configuration |
| 68 | // file. A protocol is present, but it's not ndnd. |
| 69 | // Assume the default nfd.sock location. |
| 70 | return "/var/run/nfd.sock"; |
| 71 | } |
| 72 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 73 | void |
| 74 | UnixTransport::connect(boost::asio::io_service& ioService, |
| 75 | const ReceiveCallback& receiveCallback) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 77 | if (!static_cast<bool>(m_impl)) { |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 78 | Transport::connect(ioService, receiveCallback); |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 80 | m_impl = make_shared<Impl> (boost::ref(*this), |
| 81 | boost::ref(ioService)); |
Alexander Afanasyev | 50ca627 | 2014-01-09 23:23:54 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 83 | |
| 84 | m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket)); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 87 | void |
| 88 | UnixTransport::send(const Block& wire) |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 90 | m_impl->send(wire); |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | UnixTransport::send(const Block& header, const Block& payload) |
| 95 | { |
| 96 | m_impl->send(header, payload); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Alexander Afanasyev | f7ca320 | 2014-02-14 22:28:31 -0800 | [diff] [blame] | 99 | void |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 100 | UnixTransport::close() |
| 101 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 102 | m_impl->close(); |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alexander Afanasyev | 52afb3f | 2014-03-07 09:05:35 +0000 | [diff] [blame] | 105 | void |
| 106 | UnixTransport::pause() |
| 107 | { |
| 108 | m_impl->pause(); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | UnixTransport::resume() |
| 113 | { |
| 114 | m_impl->resume(); |
| 115 | } |
| 116 | |
Alexander Afanasyev | fe3b150 | 2013-12-18 16:45:03 -0800 | [diff] [blame] | 117 | } |