blob: a7acd44066ad2eb54ada673e6f537d438c2d130e [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 Afanasyevb67090a2014-04-29 22:31:01 -070080 m_impl = make_shared<Impl>(ref(*this), ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080081 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080082
83 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080084}
85
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080086void
87UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080088{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080089 m_impl->send(wire);
90}
91
92void
93UnixTransport::send(const Block& header, const Block& payload)
94{
95 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080096}
97
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080098void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080099UnixTransport::close()
100{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800101 m_impl->close();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800102}
103
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000104void
105UnixTransport::pause()
106{
107 m_impl->pause();
108}
109
110void
111UnixTransport::resume()
112{
113 m_impl->resume();
114}
115
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800116}