blob: b9b71a8ae53780de74a9de38764e751e87a7a7ac [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi64d99f22017-01-21 23:06:36 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Yukai Tu0a49d342015-09-13 12:54:22 +08004 * 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/>.
Junxiao Shi5dd26c32014-07-20 23:15:14 -070024 */
Giulio Grassi624f6c62014-02-18 19:42:14 +010025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP
27#define NFD_DAEMON_FACE_UDP_CHANNEL_HPP
Giulio Grassi624f6c62014-02-18 19:42:14 +010028
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "channel.hpp"
Junxiao Shi64d99f22017-01-21 23:06:36 +000030#include "udp-protocol.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010031
32namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040033namespace face {
Giulio Grassi624f6c62014-02-18 19:42:14 +010034
Giulio Grassi624f6c62014-02-18 19:42:14 +010035/**
36 * \brief Class implementing UDP-based channel to create faces
Giulio Grassi624f6c62014-02-18 19:42:14 +010037 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070038class UdpChannel : public Channel
Giulio Grassi624f6c62014-02-18 19:42:14 +010039{
40public:
Giulio Grassi624f6c62014-02-18 19:42:14 +010041 /**
Giulio Grassi624f6c62014-02-18 19:42:14 +010042 * \brief Create UDP channel for the local endpoint
43 *
44 * To enable creation of faces upon incoming connections,
45 * one needs to explicitly call UdpChannel::listen method.
46 * The created socket is bound to the localEndpoint.
47 * reuse_address option is set
48 *
49 * \throw UdpChannel::Error if bind on the socket fails
50 */
51 UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 const time::seconds& timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010053
54 /**
Junxiao Shic5401492015-12-23 16:33:21 -070055 * \brief Get number of faces in the channel
56 */
57 size_t
58 size() const;
59
60 /**
61 * \brief Create a face by establishing connection to remote endpoint
62 *
63 * \throw UdpChannel::Error if bind or connect on the socket fail
64 */
65 void
66 connect(const udp::Endpoint& remoteEndpoint,
67 ndn::nfd::FacePersistency persistency,
68 const FaceCreatedCallback& onFaceCreated,
69 const FaceCreationFailedCallback& onConnectFailed);
70
71 /**
Giulio Grassi624f6c62014-02-18 19:42:14 +010072 * \brief Enable listening on the local endpoint, accept connections,
73 * and create faces when remote host makes a connection
74 * \param onFaceCreated Callback to notify successful creation of the face
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050075 * \param onReceiveFailed Callback to notify when channel fails
Giulio Grassi624f6c62014-02-18 19:42:14 +010076 *
Alexander Afanasyev5959b012014-06-02 19:18:12 +030077 * Once a face is created, if it doesn't send/receive anything for
78 * a period of time equal to timeout, it will be destroyed
79 * \todo this functionality has to be implemented
80 *
Giulio Grassi624f6c62014-02-18 19:42:14 +010081 * \throws UdpChannel::Error if called multiple times
82 */
83 void
84 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +010085 const FaceCreationFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010086
Davide Pesavento6ad890a2015-03-09 03:43:17 +010087 bool
88 isListening() const;
89
Giulio Grassi624f6c62014-02-18 19:42:14 +010090private:
Junxiao Shic5401492015-12-23 16:33:21 -070091 void
92 waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
93 const FaceCreationFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010094
95 /**
Davide Pesavento6ad890a2015-03-09 03:43:17 +010096 * \brief The channel has received a new packet from a remote
97 * endpoint that is not associated with any UdpFace yet
Giulio Grassi624f6c62014-02-18 19:42:14 +010098 */
99 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100100 handleNewPeer(const boost::system::error_code& error,
101 size_t nBytesReceived,
102 const FaceCreatedCallback& onFaceCreated,
Davide Pesavento47ab0292015-11-02 18:45:39 +0100103 const FaceCreationFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100104
Junxiao Shicde37ad2015-12-24 01:02:05 -0700105 std::pair<bool, shared_ptr<Face>>
Junxiao Shic5401492015-12-23 16:33:21 -0700106 createFace(const udp::Endpoint& remoteEndpoint, ndn::nfd::FacePersistency persistency);
107
Giulio Grassi624f6c62014-02-18 19:42:14 +0100108private:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700109 std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100110
Giulio Grassi624f6c62014-02-18 19:42:14 +0100111 udp::Endpoint m_localEndpoint;
112
113 /**
Davide Pesavento292e5e12015-03-13 02:08:33 +0100114 * \brief The latest peer that started communicating with us
Giulio Grassi624f6c62014-02-18 19:42:14 +0100115 */
Davide Pesavento292e5e12015-03-13 02:08:33 +0100116 udp::Endpoint m_remoteEndpoint;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100117
118 /**
Giulio Grassi624f6c62014-02-18 19:42:14 +0100119 * \brief Socket used to "accept" new communication
Giulio Grassi624f6c62014-02-18 19:42:14 +0100120 */
Davide Pesavento292e5e12015-03-13 02:08:33 +0100121 boost::asio::ip::udp::socket m_socket;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700122
Giulio Grassi69871f02014-03-09 16:14:44 +0100123 /**
Davide Pesavento292e5e12015-03-13 02:08:33 +0100124 * \brief When this timeout expires, all idle on-demand faces will be closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100125 */
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700126 time::seconds m_idleFaceTimeout;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100127
128 uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
Giulio Grassi624f6c62014-02-18 19:42:14 +0100129};
130
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100131inline bool
132UdpChannel::isListening() const
133{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100134 return m_socket.is_open();
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100135}
136
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400137} // namespace face
Giulio Grassi624f6c62014-02-18 19:42:14 +0100138} // namespace nfd
139
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700140#endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP