blob: 06be8123bf82bac11d0e19bbe3c940f983c8826f [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
26#define NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010027
Junxiao Shi61e3cc52014-03-03 20:40:28 -070028#include "channel.hpp"
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010029
30namespace nfd {
31
32namespace unix_stream {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070033typedef boost::asio::local::stream_protocol::endpoint Endpoint;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010034} // namespace unix_stream
35
36/**
37 * \brief Class implementing a local channel to create faces
38 *
39 * Channel can create faces as a response to incoming IPC connections
40 * (UnixStreamChannel::listen needs to be called for that to work).
41 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070042class UnixStreamChannel : public Channel
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010043{
44public:
45 /**
Davide Pesavento9cec8ee2014-02-19 11:21:59 +010046 * \brief UnixStreamChannel-related error
47 */
48 struct Error : public std::runtime_error
49 {
50 Error(const std::string& what) : std::runtime_error(what) {}
51 };
52
53 /**
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010054 * \brief Create UnixStream channel for the specified endpoint
55 *
56 * To enable creation of faces upon incoming connections, one
57 * needs to explicitly call UnixStreamChannel::listen method.
58 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070059 explicit
60 UnixStreamChannel(const unix_stream::Endpoint& endpoint);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010061
Davide Pesavento6ad890a2015-03-09 03:43:17 +010062 ~UnixStreamChannel() DECL_OVERRIDE;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010063
64 /**
65 * \brief Enable listening on the local endpoint, accept connections,
66 * and create faces when a connection is made
67 * \param onFaceCreated Callback to notify successful creation of the face
68 * \param onAcceptFailed Callback to notify when channel fails (accept call
69 * returns an error)
70 * \param backlog The maximum length of the queue of pending incoming
71 * connections
72 */
73 void
74 listen(const FaceCreatedCallback& onFaceCreated,
75 const ConnectFailedCallback& onAcceptFailed,
76 int backlog = boost::asio::local::stream_protocol::acceptor::max_connections);
77
Davide Pesavento6ad890a2015-03-09 03:43:17 +010078 bool
79 isListening() const;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010080
81private:
Davide Pesavento6ad890a2015-03-09 03:43:17 +010082 void
83 accept(const FaceCreatedCallback& onFaceCreated,
84 const ConnectFailedCallback& onAcceptFailed);
85
86 void
87 handleAccept(const boost::system::error_code& error,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010088 const FaceCreatedCallback& onFaceCreated,
89 const ConnectFailedCallback& onAcceptFailed);
90
91private:
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010092 unix_stream::Endpoint m_endpoint;
Davide Pesavento292e5e12015-03-13 02:08:33 +010093 boost::asio::local::stream_protocol::acceptor m_acceptor;
94 boost::asio::local::stream_protocol::socket m_socket;
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010095};
96
Davide Pesavento6ad890a2015-03-09 03:43:17 +010097inline bool
98UnixStreamChannel::isListening() const
99{
100 return m_acceptor.is_open();
101}
102
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +0100103} // namespace nfd
104
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700105#endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP