blob: ad6db2c6418cc7e483baf2f29704d599e0a324be [file] [log] [blame]
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyev5964fb72014-02-18 12:42:45 -08003 * Copyright (C) 2013-2014 Regents of the University of California.
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08007#include "common.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -08008
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009#include "unix-transport.hpp"
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080010#include "stream-transport.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../face.hpp"
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080013
14namespace ndn {
15
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080016UnixTransport::UnixTransport(const std::string& unixSocket)
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080017 : m_unixSocket(unixSocket)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080018{
19}
20
21UnixTransport::~UnixTransport()
22{
23}
24
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060025std::string
26UnixTransport::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 Afanasyevf7ca3202014-02-14 22:28:31 -080067void
68UnixTransport::connect(boost::asio::io_service& ioService,
69 const ReceiveCallback& receiveCallback)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080070{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080071 if (!static_cast<bool>(m_impl)) {
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080072 Transport::connect(ioService, receiveCallback);
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080073
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080074 m_impl = make_shared<Impl> (boost::ref(*this),
75 boost::ref(ioService));
Alexander Afanasyev50ca6272014-01-09 23:23:54 -080076 }
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080077
78 m_impl->connect(boost::asio::local::stream_protocol::endpoint(m_unixSocket));
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080079}
80
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080081void
82UnixTransport::send(const Block& wire)
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080083{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080084 m_impl->send(wire);
85}
86
87void
88UnixTransport::send(const Block& header, const Block& payload)
89{
90 m_impl->send(header, payload);
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080091}
92
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080093void
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080094UnixTransport::close()
95{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080096 m_impl->close();
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -080097}
98
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000099void
100UnixTransport::pause()
101{
102 m_impl->pause();
103}
104
105void
106UnixTransport::resume()
107{
108 m_impl->resume();
109}
110
Alexander Afanasyevfe3b1502013-12-18 16:45:03 -0800111}