Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #ifndef NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |
| 26 | #define NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 27 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | #include "channel.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | |
| 32 | namespace unix_stream { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 33 | typedef boost::asio::local::stream_protocol::endpoint Endpoint; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 34 | } // 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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 42 | class UnixStreamChannel : public Channel |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | /** |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 46 | * \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 Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 54 | * \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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 59 | explicit |
| 60 | UnixStreamChannel(const unix_stream::Endpoint& endpoint); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 61 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 62 | ~UnixStreamChannel() DECL_OVERRIDE; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 63 | |
| 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 Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 78 | bool |
| 79 | isListening() const; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 80 | |
| 81 | private: |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 82 | void |
| 83 | accept(const FaceCreatedCallback& onFaceCreated, |
| 84 | const ConnectFailedCallback& onAcceptFailed); |
| 85 | |
| 86 | void |
| 87 | handleAccept(const boost::system::error_code& error, |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 88 | const FaceCreatedCallback& onFaceCreated, |
| 89 | const ConnectFailedCallback& onAcceptFailed); |
| 90 | |
| 91 | private: |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 92 | unix_stream::Endpoint m_endpoint; |
Davide Pesavento | 292e5e1 | 2015-03-13 02:08:33 +0100 | [diff] [blame] | 93 | boost::asio::local::stream_protocol::acceptor m_acceptor; |
| 94 | boost::asio::local::stream_protocol::socket m_socket; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 97 | inline bool |
| 98 | UnixStreamChannel::isListening() const |
| 99 | { |
| 100 | return m_acceptor.is_open(); |
| 101 | } |
| 102 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 103 | } // namespace nfd |
| 104 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 105 | #endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |