blob: fe697941a0888df652cdfa644a50bcfb57de841e [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_FACTORY_HPP
8#define NFD_FACE_UDP_FACTORY_HPP
9
10#include "protocol-factory.hpp"
11#include "udp-channel.hpp"
12#include "multicast-udp-face.hpp"
13
14
15namespace nfd {
16
17// @todo The multicast support for ipv6 must be implemented
18
19class UdpFactory : public ProtocolFactory
20{
21public:
22 /**
23 * \brief Exception of UdpFactory
24 */
25 struct Error : public ProtocolFactory::Error
26 {
27 Error(const std::string& what) : ProtocolFactory::Error(what) {}
28 };
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060029
Giulio Grassi624f6c62014-02-18 19:42:14 +010030 explicit
31 UdpFactory(const std::string& defaultPort = "6363");
32
33 /**
34 * \brief Create UDP-based channel using udp::Endpoint
35 *
36 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
37 *
38 * If this method called twice with the same endpoint, only one channel
39 * will be created. The second call will just retrieve the existing
40 * channel.
41 *
42 * If a multicast face is already active on the same local endpoint,
43 * the creation fails and an exception is thrown
44 *
45 * Once a face is created, if it doesn't send/receive anything for
46 * a period of time equal to timeout, it will be destroyed
47 * @todo this funcionality has to be implemented
48 *
49 * \returns always a valid pointer to a UdpChannel object, an exception
50 * is thrown if it cannot be created.
51 *
52 * \throws UdpFactory::Error
53 *
54 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
55 * for details on ways to create udp::Endpoint
56 */
57 shared_ptr<UdpChannel>
58 createChannel(const udp::Endpoint& localEndpoint,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070059 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010060
61 /**
62 * \brief Create UDP-based channel using specified host and port number
63 *
64 * This method will attempt to resolve the provided host and port numbers
65 * and will throw UdpFactory::Error when channel cannot be created.
66 *
67 * Note that this call will **BLOCK** until resolution is done or failed.
68 *
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060069 * If localHost is a IPv6 address of a specific device, it must be in the form:
Giulio Grassi624f6c62014-02-18 19:42:14 +010070 * ip address%interface name
71 * Example: fe80::5e96:9dff:fe7d:9c8d%en1
Steve DiBenedettoca53ac62014-03-27 19:58:40 -060072 * Otherwise, you can use ::
Giulio Grassi624f6c62014-02-18 19:42:14 +010073 *
74 * Once a face is created, if it doesn't send/receive anything for
75 * a period of time equal to timeout, it will be destroyed
76 * @todo this funcionality has to be implemented
77 *
78 * \throws UdpChannel::Error if the bind on the socket fails
79 * \throws UdpFactory::Error
80 */
81 shared_ptr<UdpChannel>
82 createChannel(const std::string& localHost,
83 const std::string& localPort,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070084 const time::seconds& timeout = time::seconds(600));
Giulio Grassi624f6c62014-02-18 19:42:14 +010085
86 /**
87 * \brief Create MulticastUdpFace using udp::Endpoint
88 *
89 * udp::Endpoint is really an alias for boost::asio::ip::udp::endpoint.
90 *
91 * The face will join the multicast group
92 *
93 * If this method called twice with the same endpoint and group, only one face
94 * will be created. The second call will just retrieve the existing
95 * channel.
96 *
97 * If an unicast face is already active on the same local NIC and port, the
98 * creation fails and an exception is thrown
99 *
100 * \returns always a valid pointer to a MulticastUdpFace object, an exception
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600101 * is thrown if it cannot be created.
Giulio Grassi624f6c62014-02-18 19:42:14 +0100102 *
103 * \throws UdpFactory::Error
104 *
105 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__udp/endpoint.html
106 * for details on ways to create udp::Endpoint
107 */
108 shared_ptr<MulticastUdpFace>
109 createMulticastFace(const udp::Endpoint& localEndpoint,
110 const udp::Endpoint& multicastEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600111
Giulio Grassi624f6c62014-02-18 19:42:14 +0100112 shared_ptr<MulticastUdpFace>
113 createMulticastFace(const std::string& localIp,
114 const std::string& multicastIp,
115 const std::string& multicastPort);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600116
Giulio Grassi624f6c62014-02-18 19:42:14 +0100117 // from Factory
118 virtual void
119 createFace(const FaceUri& uri,
120 const FaceCreatedCallback& onCreated,
121 const FaceConnectFailedCallback& onConnectFailed);
122
123protected:
124 typedef std::map< udp::Endpoint, shared_ptr<MulticastUdpFace> > MulticastFaceMap;
125
126 /**
127 * \brief Keeps tracking of the MulticastUdpFace created
128 */
129 MulticastFaceMap m_multicastFaces;
130
131private:
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600132
133 void
134 prohibitEndpoint(const udp::Endpoint& endpoint);
135
136 void
137 prohibitAllIpv4Endpoints(const uint16_t port);
138
139 void
140 prohibitAllIpv6Endpoints(const uint16_t port);
141
Giulio Grassi624f6c62014-02-18 19:42:14 +0100142 void
143 afterFaceFailed(udp::Endpoint& endpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600144
Giulio Grassi624f6c62014-02-18 19:42:14 +0100145 /**
146 * \brief Look up UdpChannel using specified local endpoint
147 *
148 * \returns shared pointer to the existing UdpChannel object
149 * or empty shared pointer when such channel does not exist
150 *
151 * \throws never
152 */
153 shared_ptr<UdpChannel>
154 findChannel(const udp::Endpoint& localEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600155
156
Giulio Grassi624f6c62014-02-18 19:42:14 +0100157 /**
158 * \brief Look up multicast UdpFace using specified local endpoint
159 *
160 * \returns shared pointer to the existing multicast MulticastUdpFace object
161 * or empty shared pointer when such face does not exist
162 *
163 * \throws never
164 */
165 shared_ptr<MulticastUdpFace>
166 findMulticastFace(const udp::Endpoint& localEndpoint);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600167
Giulio Grassi624f6c62014-02-18 19:42:14 +0100168 void
169 continueCreateFaceAfterResolve(const udp::Endpoint& endpoint,
170 const FaceCreatedCallback& onCreated,
171 const FaceConnectFailedCallback& onConnectFailed);
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600172
Giulio Grassi624f6c62014-02-18 19:42:14 +0100173 typedef std::map< udp::Endpoint, shared_ptr<UdpChannel> > ChannelMap;
174 ChannelMap m_channels;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600175
Giulio Grassi624f6c62014-02-18 19:42:14 +0100176 std::string m_defaultPort;
Steve DiBenedettoca53ac62014-03-27 19:58:40 -0600177
178 std::set<udp::Endpoint> m_prohibitedEndpoints;
Giulio Grassi624f6c62014-02-18 19:42:14 +0100179};
180
181} // namespace nfd
182
183#endif // NFD_FACE_UDP_FACTORY_HPP