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 | #include "unix-stream-face.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | |
| 33 | namespace unix_stream { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 34 | typedef boost::asio::local::stream_protocol::endpoint Endpoint; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 35 | } // namespace unix_stream |
| 36 | |
| 37 | /** |
| 38 | * \brief Class implementing a local channel to create faces |
| 39 | * |
| 40 | * Channel can create faces as a response to incoming IPC connections |
| 41 | * (UnixStreamChannel::listen needs to be called for that to work). |
| 42 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 43 | class UnixStreamChannel : public Channel |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | /** |
Davide Pesavento | 9cec8ee | 2014-02-19 11:21:59 +0100 | [diff] [blame] | 47 | * \brief UnixStreamChannel-related error |
| 48 | */ |
| 49 | struct Error : public std::runtime_error |
| 50 | { |
| 51 | Error(const std::string& what) : std::runtime_error(what) {} |
| 52 | }; |
| 53 | |
| 54 | /** |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 55 | * \brief Create UnixStream channel for the specified endpoint |
| 56 | * |
| 57 | * To enable creation of faces upon incoming connections, one |
| 58 | * needs to explicitly call UnixStreamChannel::listen method. |
| 59 | */ |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 60 | explicit |
| 61 | UnixStreamChannel(const unix_stream::Endpoint& endpoint); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 62 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 63 | virtual |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 64 | ~UnixStreamChannel(); |
| 65 | |
| 66 | /** |
| 67 | * \brief Enable listening on the local endpoint, accept connections, |
| 68 | * and create faces when a connection is made |
| 69 | * \param onFaceCreated Callback to notify successful creation of the face |
| 70 | * \param onAcceptFailed Callback to notify when channel fails (accept call |
| 71 | * returns an error) |
| 72 | * \param backlog The maximum length of the queue of pending incoming |
| 73 | * connections |
| 74 | */ |
| 75 | void |
| 76 | listen(const FaceCreatedCallback& onFaceCreated, |
| 77 | const ConnectFailedCallback& onAcceptFailed, |
| 78 | int backlog = boost::asio::local::stream_protocol::acceptor::max_connections); |
| 79 | |
| 80 | private: |
| 81 | void |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 82 | handleSuccessfulAccept(const boost::system::error_code& error, |
| 83 | const shared_ptr<boost::asio::local::stream_protocol::socket>& socket, |
| 84 | const FaceCreatedCallback& onFaceCreated, |
| 85 | const ConnectFailedCallback& onConnectFailed); |
| 86 | |
| 87 | private: |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 88 | unix_stream::Endpoint m_endpoint; |
| 89 | shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor; |
Davide Pesavento | e22d8c8 | 2014-04-14 04:01:52 +0200 | [diff] [blame] | 90 | bool m_isListening; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } // namespace nfd |
| 94 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 95 | #endif // NFD_DAEMON_FACE_UNIX_STREAM_CHANNEL_HPP |