blob: 17410dc7c8a18dafb7d0ff5dfbd4000b00a9f72e [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5dd26c32014-07-20 23:15:14 -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 * 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"
Giulio Grassi624f6c62014-02-18 19:42:14 +010030
31namespace nfd {
32
33namespace udp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070034typedef boost::asio::ip::udp::endpoint Endpoint;
Giulio Grassi624f6c62014-02-18 19:42:14 +010035} // namespace udp
36
Davide Pesavento6ad890a2015-03-09 03:43:17 +010037class UdpFace;
38
Giulio Grassi624f6c62014-02-18 19:42:14 +010039/**
40 * \brief Class implementing UDP-based channel to create faces
Giulio Grassi624f6c62014-02-18 19:42:14 +010041 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070042class UdpChannel : public Channel
Giulio Grassi624f6c62014-02-18 19:42:14 +010043{
44public:
Giulio Grassi624f6c62014-02-18 19:42:14 +010045 /**
Giulio Grassi624f6c62014-02-18 19:42:14 +010046 * \brief Create UDP channel for the local endpoint
47 *
48 * To enable creation of faces upon incoming connections,
49 * one needs to explicitly call UdpChannel::listen method.
50 * The created socket is bound to the localEndpoint.
51 * reuse_address option is set
52 *
53 * \throw UdpChannel::Error if bind on the socket fails
54 */
55 UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070056 const time::seconds& timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010057
58 /**
59 * \brief Enable listening on the local endpoint, accept connections,
60 * and create faces when remote host makes a connection
61 * \param onFaceCreated Callback to notify successful creation of the face
62 * \param onAcceptFailed Callback to notify when channel fails
63 *
Alexander Afanasyev5959b012014-06-02 19:18:12 +030064 * Once a face is created, if it doesn't send/receive anything for
65 * a period of time equal to timeout, it will be destroyed
66 * \todo this functionality has to be implemented
67 *
Giulio Grassi624f6c62014-02-18 19:42:14 +010068 * \throws UdpChannel::Error if called multiple times
69 */
70 void
71 listen(const FaceCreatedCallback& onFaceCreated,
Davide Pesavento6ad890a2015-03-09 03:43:17 +010072 const ConnectFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010073
74 /**
75 * \brief Create a face by establishing connection to remote endpoint
76 *
77 * \throw UdpChannel::Error if bind or connect on the socket fail
78 */
79 void
80 connect(const udp::Endpoint& remoteEndpoint,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060081 const FaceCreatedCallback& onFaceCreated,
82 const ConnectFailedCallback& onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010083
84 /**
85 * \brief Get number of faces in the channel
86 */
87 size_t
88 size() const;
89
Davide Pesavento6ad890a2015-03-09 03:43:17 +010090 bool
91 isListening() const;
92
Giulio Grassi624f6c62014-02-18 19:42:14 +010093private:
Davide Pesavento292e5e12015-03-13 02:08:33 +010094 std::pair<bool, shared_ptr<UdpFace>>
95 createFace(const udp::Endpoint& remoteEndpoint, bool isOnDemand);
Giulio Grassi624f6c62014-02-18 19:42:14 +010096
97 /**
Davide Pesavento6ad890a2015-03-09 03:43:17 +010098 * \brief The channel has received a new packet from a remote
99 * endpoint that is not associated with any UdpFace yet
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100 */
101 void
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100102 handleNewPeer(const boost::system::error_code& error,
103 size_t nBytesReceived,
104 const FaceCreatedCallback& onFaceCreated,
105 const ConnectFailedCallback& onReceiveFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100106
Giulio Grassi624f6c62014-02-18 19:42:14 +0100107private:
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100108 std::map<udp::Endpoint, shared_ptr<UdpFace>> m_channelFaces;
109
Giulio Grassi624f6c62014-02-18 19:42:14 +0100110 udp::Endpoint m_localEndpoint;
111
112 /**
Davide Pesavento292e5e12015-03-13 02:08:33 +0100113 * \brief The latest peer that started communicating with us
Giulio Grassi624f6c62014-02-18 19:42:14 +0100114 */
Davide Pesavento292e5e12015-03-13 02:08:33 +0100115 udp::Endpoint m_remoteEndpoint;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100116
117 /**
Giulio Grassi624f6c62014-02-18 19:42:14 +0100118 * \brief Socket used to "accept" new communication
Giulio Grassi624f6c62014-02-18 19:42:14 +0100119 */
Davide Pesavento292e5e12015-03-13 02:08:33 +0100120 boost::asio::ip::udp::socket m_socket;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700121
Giulio Grassi69871f02014-03-09 16:14:44 +0100122 /**
Davide Pesavento292e5e12015-03-13 02:08:33 +0100123 * \brief When this timeout expires, all idle on-demand faces will be closed
Giulio Grassi69871f02014-03-09 16:14:44 +0100124 */
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700125 time::seconds m_idleFaceTimeout;
Davide Pesavento292e5e12015-03-13 02:08:33 +0100126
127 uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128};
129
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100130inline bool
131UdpChannel::isListening() const
132{
Davide Pesavento292e5e12015-03-13 02:08:33 +0100133 return m_socket.is_open();
Davide Pesavento6ad890a2015-03-09 03:43:17 +0100134}
135
Giulio Grassi624f6c62014-02-18 19:42:14 +0100136} // namespace nfd
137
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700138#endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP