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