blob: 4db6e439153023cde2be443c37af94f11ba5cfe4 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +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 **/
Giulio Grassi624f6c62014-02-18 19:42:14 +010024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP
26#define NFD_DAEMON_FACE_UDP_CHANNEL_HPP
Giulio Grassi624f6c62014-02-18 19:42:14 +010027
Junxiao Shi61e3cc52014-03-03 20:40:28 -070028#include "channel.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070029#include "core/global-io.hpp"
Giulio Grassi69871f02014-03-09 16:14:44 +010030#include "core/scheduler.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010031#include "udp-face.hpp"
32
33namespace nfd {
34
35namespace udp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070036typedef boost::asio::ip::udp::endpoint Endpoint;
Giulio Grassi624f6c62014-02-18 19:42:14 +010037} // namespace udp
38
39/**
40 * \brief Class implementing UDP-based channel to create faces
41 *
42 *
43 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070044class UdpChannel : public Channel
Giulio Grassi624f6c62014-02-18 19:42:14 +010045{
46public:
Giulio Grassi624f6c62014-02-18 19:42:14 +010047 /**
48 * \brief Exception of UdpChannel
49 */
50 struct Error : public std::runtime_error
51 {
52 Error(const std::string& what) : runtime_error(what) {}
53 };
Giulio Grassi624f6c62014-02-18 19:42:14 +010054
55 /**
56 * \brief Create UDP channel for the local endpoint
57 *
58 * To enable creation of faces upon incoming connections,
59 * one needs to explicitly call UdpChannel::listen method.
60 * The created socket is bound to the localEndpoint.
61 * reuse_address option is set
62 *
63 * \throw UdpChannel::Error if bind on the socket fails
64 */
65 UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070066 const time::seconds& timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010067
Junxiao Shi61e3cc52014-03-03 20:40:28 -070068 virtual
69 ~UdpChannel();
70
Giulio Grassi624f6c62014-02-18 19:42:14 +010071 /**
72 * \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
75 * \param onAcceptFailed Callback to notify when channel fails
76 *
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,
85 const ConnectFailedCallback& onAcceptFailed);
86
87 /**
88 * \brief Create a face by establishing connection to remote endpoint
89 *
90 * \throw UdpChannel::Error if bind or connect on the socket fail
91 */
92 void
93 connect(const udp::Endpoint& remoteEndpoint,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060094 const FaceCreatedCallback& onFaceCreated,
95 const ConnectFailedCallback& onConnectFailed);
Giulio Grassi624f6c62014-02-18 19:42:14 +010096 /**
97 * \brief Create a face by establishing connection to the specified
98 * remote host and remote port
99 *
100 * This method will never block and will return immediately. All
101 * necessary hostname and port resolution and connection will happen
102 * in asynchronous mode.
103 *
104 * If connection cannot be established within specified timeout, it
105 * will be aborted.
106 */
107 void
108 connect(const std::string& remoteHost, const std::string& remotePort,
109 const FaceCreatedCallback& onFaceCreated,
110 const ConnectFailedCallback& onConnectFailed);
111
112 /**
113 * \brief Get number of faces in the channel
114 */
115 size_t
116 size() const;
117
118private:
119 shared_ptr<UdpFace>
120 createFace(const shared_ptr<boost::asio::ip::udp::socket>& socket,
Giulio Grassi69871f02014-03-09 16:14:44 +0100121 const FaceCreatedCallback& onFaceCreated,
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700122 bool isOnDemand);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100123 void
124 afterFaceFailed(udp::Endpoint& endpoint);
125
126 /**
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700127 * \brief The UdpChannel has received a new pkt from a remote endpoint not yet
Giulio Grassi624f6c62014-02-18 19:42:14 +0100128 * associated with any UdpFace
129 */
130 void
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700131 newPeer(const boost::system::error_code& error, std::size_t nBytesReceived);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100132
133 void
134 handleEndpointResolution(const boost::system::error_code& error,
135 boost::asio::ip::udp::resolver::iterator remoteEndpoint,
136 const FaceCreatedCallback& onFaceCreated,
137 const ConnectFailedCallback& onConnectFailed,
138 const shared_ptr<boost::asio::ip::udp::resolver>& resolver);
139
Giulio Grassi69871f02014-03-09 16:14:44 +0100140 void
141 closeIdleFaces();
142
Giulio Grassi624f6c62014-02-18 19:42:14 +0100143private:
144 udp::Endpoint m_localEndpoint;
145
146 /**
147 * \brief Endpoint used to store the information about the last new remote endpoint
148 */
149 udp::Endpoint m_newRemoteEndpoint;
150
151 /**
152 * Callbacks for face creation.
153 * New communications are detected using async_receive_from.
154 * Its handler has a fixed signature. No space for the face callback
155 */
156 FaceCreatedCallback onFaceCreatedNewPeerCallback;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700157
Giulio Grassi624f6c62014-02-18 19:42:14 +0100158 // @todo remove the onConnectFailedNewPeerCallback if it remains unused
159 ConnectFailedCallback onConnectFailedNewPeerCallback;
160
161 /**
162 * \brief Socket used to "accept" new communication
163 **/
164 shared_ptr<boost::asio::ip::udp::socket> m_socket;
165
166 uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE];
167
168 typedef std::map< udp::Endpoint, shared_ptr<UdpFace> > ChannelFaceMap;
169 ChannelFaceMap m_channelFaces;
170
171 /**
172 * \brief If true, it means the function listen has already been called
173 */
174 bool m_isListening;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -0700175
Giulio Grassi69871f02014-03-09 16:14:44 +0100176 /**
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700177 * \brief every time m_idleFaceTimeout expires all the idle (and on-demand)
Giulio Grassi69871f02014-03-09 16:14:44 +0100178 * faces will be removed
179 */
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700180 time::seconds m_idleFaceTimeout;
Giulio Grassi69871f02014-03-09 16:14:44 +0100181
182 EventId m_closeIdleFaceEvent;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100183
184};
185
186} // namespace nfd
187
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700188#endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP