blob: e396d8e66a4f317ac4dc4a0d1624d90bb59f639b [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento8fd15e62017-04-06 19:58:54 -04003 * Copyright (c) 2014-2017, Regents of the University of California,
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020024 */
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
27#define NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010030
31namespace nfd {
32
33namespace unix_stream {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070034typedef boost::asio::local::stream_protocol::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010035} // namespace unix_stream
36
Davide Pesavento8fd15e62017-04-06 19:58:54 -040037namespace face {
38
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010039/**
40 * \brief Class implementing a local channel to create faces
41 *
42 * Channel can create faces as a response to incoming IPC connections
43 * (UnixStreamChannel::listen needs to be called for that to work).
44 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070045class UnixStreamChannel : public Channel
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010046{
47public:
48 /**
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010049 * \brief UnixStreamChannel-related error
50 */
Davide Pesaventoc19408d2017-04-08 00:42:55 -040051 class Error : public std::runtime_error
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010052 {
Davide Pesaventoc19408d2017-04-08 00:42:55 -040053 public:
54 explicit
55 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
58 }
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010059 };
60
61 /**
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010062 * \brief Create UnixStream channel for the specified endpoint
63 *
64 * To enable creation of faces upon incoming connections, one
65 * needs to explicitly call UnixStreamChannel::listen method.
66 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070067 explicit
68 UnixStreamChannel(const unix_stream::Endpoint& endpoint);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010069
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020070 ~UnixStreamChannel() override;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010071
Davide Pesaventoc19408d2017-04-08 00:42:55 -040072 bool
73 isListening() const override
74 {
75 return m_acceptor.is_open();
76 }
77
78 size_t
79 size() const override
80 {
81 return m_size;
82 }
83
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010084 /**
Davide Pesavento43ff2a92017-05-18 19:50:57 -040085 * \brief Start listening
86 *
87 * Enable listening on the Unix socket, waiting for incoming connections,
88 * and creating a face when a connection is made.
89 *
90 * Faces created in this way will have on-demand persistency.
91 *
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010092 * \param onFaceCreated Callback to notify successful creation of the face
93 * \param onAcceptFailed Callback to notify when channel fails (accept call
94 * returns an error)
95 * \param backlog The maximum length of the queue of pending incoming
96 * connections
Davide Pesavento43ff2a92017-05-18 19:50:57 -040097 * \throw Error
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010098 */
99 void
100 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100101 const FaceCreationFailedCallback& onAcceptFailed,
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100102 int backlog = boost::asio::local::stream_protocol::acceptor::max_connections);
103
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100104private:
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100105 void
106 accept(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100107 const FaceCreationFailedCallback& onAcceptFailed);
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108
109 void
110 handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100111 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100112 const FaceCreationFailedCallback& onAcceptFailed);
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100113
114private:
Davide Pesavento77911cc2017-04-08 22:12:30 -0400115 const unix_stream::Endpoint m_endpoint;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100116 boost::asio::local::stream_protocol::acceptor m_acceptor;
117 boost::asio::local::stream_protocol::socket m_socket;
Davide Pesaventoc19408d2017-04-08 00:42:55 -0400118 size_t m_size;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100119};
120
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400121} // namespace face
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100122} // namespace nfd
123
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700124#endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP