blob: 0243dd69586248afff26c7b8b876d09171b9baa9 [file] [log] [blame]
Giulio Grassi624f6c62014-02-18 19:42:14 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_FACE_UDP_CHANNEL_HPP
8#define NFD_FACE_UDP_CHANNEL_HPP
9
Junxiao Shi61e3cc52014-03-03 20:40:28 -070010#include "channel.hpp"
Junxiao Shi61e3cc52014-03-03 20:40:28 -070011#include "core/global-io.hpp"
Giulio Grassi69871f02014-03-09 16:14:44 +010012#include "core/scheduler.hpp"
Giulio Grassi624f6c62014-02-18 19:42:14 +010013#include "udp-face.hpp"
14
15namespace nfd {
16
17namespace udp {
Junxiao Shi61e3cc52014-03-03 20:40:28 -070018typedef boost::asio::ip::udp::endpoint Endpoint;
Giulio Grassi624f6c62014-02-18 19:42:14 +010019} // namespace udp
20
21/**
22 * \brief Class implementing UDP-based channel to create faces
23 *
24 *
25 */
Junxiao Shi61e3cc52014-03-03 20:40:28 -070026class UdpChannel : public Channel
Giulio Grassi624f6c62014-02-18 19:42:14 +010027{
28public:
Giulio Grassi624f6c62014-02-18 19:42:14 +010029 /**
30 * \brief Exception of UdpChannel
31 */
32 struct Error : public std::runtime_error
33 {
34 Error(const std::string& what) : runtime_error(what) {}
35 };
Giulio Grassi624f6c62014-02-18 19:42:14 +010036
37 /**
38 * \brief Create UDP channel for the local endpoint
39 *
40 * To enable creation of faces upon incoming connections,
41 * one needs to explicitly call UdpChannel::listen method.
42 * The created socket is bound to the localEndpoint.
43 * reuse_address option is set
44 *
45 * \throw UdpChannel::Error if bind on the socket fails
46 */
47 UdpChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070048 const time::seconds& timeout);
Giulio Grassi624f6c62014-02-18 19:42:14 +010049
Junxiao Shi61e3cc52014-03-03 20:40:28 -070050 virtual
51 ~UdpChannel();
52
Giulio Grassi624f6c62014-02-18 19:42:14 +010053 /**
54 * \brief Enable listening on the local endpoint, accept connections,
55 * and create faces when remote host makes a connection
56 * \param onFaceCreated Callback to notify successful creation of the face
57 * \param onAcceptFailed Callback to notify when channel fails
58 *
59 * \throws UdpChannel::Error if called multiple times
60 */
61 void
62 listen(const FaceCreatedCallback& onFaceCreated,
63 const ConnectFailedCallback& onAcceptFailed);
64
65 /**
66 * \brief Create a face by establishing connection to remote endpoint
67 *
68 * \throw UdpChannel::Error if bind or connect on the socket fail
69 */
70 void
71 connect(const udp::Endpoint& remoteEndpoint,
72 const FaceCreatedCallback& onFaceCreated);
73 /**
74 * \brief Create a face by establishing connection to the specified
75 * remote host and remote port
76 *
77 * This method will never block and will return immediately. All
78 * necessary hostname and port resolution and connection will happen
79 * in asynchronous mode.
80 *
81 * If connection cannot be established within specified timeout, it
82 * will be aborted.
83 */
84 void
85 connect(const std::string& remoteHost, const std::string& remotePort,
86 const FaceCreatedCallback& onFaceCreated,
87 const ConnectFailedCallback& onConnectFailed);
88
89 /**
90 * \brief Get number of faces in the channel
91 */
92 size_t
93 size() const;
94
95private:
96 shared_ptr<UdpFace>
97 createFace(const shared_ptr<boost::asio::ip::udp::socket>& socket,
Giulio Grassi69871f02014-03-09 16:14:44 +010098 const FaceCreatedCallback& onFaceCreated,
Alexander Afanasyev355c0662014-03-20 18:08:17 -070099 bool isOnDemand);
Giulio Grassi624f6c62014-02-18 19:42:14 +0100100 void
101 afterFaceFailed(udp::Endpoint& endpoint);
102
103 /**
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700104 * \brief The UdpChannel has received a new pkt from a remote endpoint not yet
Giulio Grassi624f6c62014-02-18 19:42:14 +0100105 * associated with any UdpFace
106 */
107 void
108 newPeer(const boost::system::error_code& error,
109 std::size_t nBytesReceived);
110
111 void
112 handleEndpointResolution(const boost::system::error_code& error,
113 boost::asio::ip::udp::resolver::iterator remoteEndpoint,
114 const FaceCreatedCallback& onFaceCreated,
115 const ConnectFailedCallback& onConnectFailed,
116 const shared_ptr<boost::asio::ip::udp::resolver>& resolver);
117
Giulio Grassi69871f02014-03-09 16:14:44 +0100118 void
119 closeIdleFaces();
120
Giulio Grassi624f6c62014-02-18 19:42:14 +0100121private:
122 udp::Endpoint m_localEndpoint;
123
124 /**
125 * \brief Endpoint used to store the information about the last new remote endpoint
126 */
127 udp::Endpoint m_newRemoteEndpoint;
128
129 /**
130 * Callbacks for face creation.
131 * New communications are detected using async_receive_from.
132 * Its handler has a fixed signature. No space for the face callback
133 */
134 FaceCreatedCallback onFaceCreatedNewPeerCallback;
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700135
Giulio Grassi624f6c62014-02-18 19:42:14 +0100136 // @todo remove the onConnectFailedNewPeerCallback if it remains unused
137 ConnectFailedCallback onConnectFailedNewPeerCallback;
138
139 /**
140 * \brief Socket used to "accept" new communication
141 **/
142 shared_ptr<boost::asio::ip::udp::socket> m_socket;
143
144 uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE];
145
146 typedef std::map< udp::Endpoint, shared_ptr<UdpFace> > ChannelFaceMap;
147 ChannelFaceMap m_channelFaces;
148
149 /**
150 * \brief If true, it means the function listen has already been called
151 */
152 bool m_isListening;
Giulio Grassi69871f02014-03-09 16:14:44 +0100153
154 /**
Alexander Afanasyev355c0662014-03-20 18:08:17 -0700155 * \brief every time m_idleFaceTimeout expires all the idle (and on-demand)
Giulio Grassi69871f02014-03-09 16:14:44 +0100156 * faces will be removed
157 */
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700158 time::seconds m_idleFaceTimeout;
Giulio Grassi69871f02014-03-09 16:14:44 +0100159
160 EventId m_closeIdleFaceEvent;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100161
162};
163
164} // namespace nfd
165
166#endif // NFD_FACE_UDP_CHANNEL_HPP