Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- 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 | |
| 15 | namespace nfd { |
| 16 | |
| 17 | // @todo The multicast support for ipv6 must be implemented |
| 18 | |
| 19 | class UdpFactory : public ProtocolFactory |
| 20 | { |
| 21 | public: |
| 22 | /** |
| 23 | * \brief Exception of UdpFactory |
| 24 | */ |
| 25 | struct Error : public ProtocolFactory::Error |
| 26 | { |
| 27 | Error(const std::string& what) : ProtocolFactory::Error(what) {} |
| 28 | }; |
| 29 | |
| 30 | 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 Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 59 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 60 | |
| 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 | * |
| 69 | * If localHost is a IPv6 address of a specific device, it must be in the form: |
| 70 | * ip address%interface name |
| 71 | * Example: fe80::5e96:9dff:fe7d:9c8d%en1 |
| 72 | * Otherwise, you can use :: |
| 73 | * |
| 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 Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 84 | const time::seconds& timeout = time::seconds(600)); |
Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 85 | |
| 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 |
| 101 | * is thrown if it cannot be created. |
| 102 | * |
| 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); |
| 111 | |
| 112 | shared_ptr<MulticastUdpFace> |
| 113 | createMulticastFace(const std::string& localIp, |
| 114 | const std::string& multicastIp, |
| 115 | const std::string& multicastPort); |
| 116 | |
| 117 | // from Factory |
| 118 | virtual void |
| 119 | createFace(const FaceUri& uri, |
| 120 | const FaceCreatedCallback& onCreated, |
| 121 | const FaceConnectFailedCallback& onConnectFailed); |
| 122 | |
| 123 | protected: |
| 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 | |
| 131 | private: |
| 132 | void |
| 133 | afterFaceFailed(udp::Endpoint& endpoint); |
| 134 | |
| 135 | /** |
| 136 | * \brief Look up UdpChannel using specified local endpoint |
| 137 | * |
| 138 | * \returns shared pointer to the existing UdpChannel object |
| 139 | * or empty shared pointer when such channel does not exist |
| 140 | * |
| 141 | * \throws never |
| 142 | */ |
| 143 | shared_ptr<UdpChannel> |
| 144 | findChannel(const udp::Endpoint& localEndpoint); |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * \brief Look up multicast UdpFace using specified local endpoint |
| 149 | * |
| 150 | * \returns shared pointer to the existing multicast MulticastUdpFace object |
| 151 | * or empty shared pointer when such face does not exist |
| 152 | * |
| 153 | * \throws never |
| 154 | */ |
| 155 | shared_ptr<MulticastUdpFace> |
| 156 | findMulticastFace(const udp::Endpoint& localEndpoint); |
| 157 | |
| 158 | void |
| 159 | continueCreateFaceAfterResolve(const udp::Endpoint& endpoint, |
| 160 | const FaceCreatedCallback& onCreated, |
| 161 | const FaceConnectFailedCallback& onConnectFailed); |
| 162 | |
| 163 | typedef std::map< udp::Endpoint, shared_ptr<UdpChannel> > ChannelMap; |
| 164 | ChannelMap m_channels; |
| 165 | |
| 166 | std::string m_defaultPort; |
| 167 | }; |
| 168 | |
| 169 | } // namespace nfd |
| 170 | |
| 171 | #endif // NFD_FACE_UDP_FACTORY_HPP |