blob: cce5f0ff498388072f75a888b0e11194411fe6a7 [file] [log] [blame]
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyevfe3b1502013-12-18 16:45:03 -080011 */
12
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080013#include "common.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080014
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "unix-transport.hpp"
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080016#include "stream-transport.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080017
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080018#include "../face.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080019
20namespace ndn {
21
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080022UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080023 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080024{
25}
26
27UnixTransport::~UnixTransport()
28{
29}
30
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060031std::string
32UnixTransport::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 Afanasyev2a7f7202014-04-23 14:25:29 -070039 catch (boost::property_tree::ptree_bad_path& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060040 {
41 // unix_socket not present, continue
42 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070043 catch (boost::property_tree::ptree_bad_data& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060044 {
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 Afanasyevf7ca3202014-02-14 22:28:31 -080073void
74UnixTransport::connect(boost::asio::io_service& ioService,
75 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080076{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080077 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080078 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080079
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080080 m_impl = make_shared<Impl> (boost::ref(*this),
81 boost::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{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080090 m_impl->send(wire);
91}
92
93void
94UnixTransport::send(const Block& header, const Block& payload)
95{
96 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080097}
98
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080099void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800100UnixTransport::close()
101{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800102 m_impl->close();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800103}
104
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000105void
106UnixTransport::pause()
107{
108 m_impl->pause();
109}
110
111void
112UnixTransport::resume()
113{
114 m_impl->resume();
115}
116
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800117}