Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [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 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 7 | #ifndef NFD_FACE_TCP_FACTORY_HPP |
| 8 | #define NFD_FACE_TCP_FACTORY_HPP |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 10 | #include "protocol-factory.hpp" |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 11 | #include "tcp-channel.hpp" |
| 12 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 13 | namespace nfd { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 14 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 15 | class TcpFactory : public ProtocolFactory |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 16 | { |
| 17 | public: |
| 18 | /** |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 19 | * \brief Exception of TcpFactory |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 20 | */ |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 21 | struct Error : public ProtocolFactory::Error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 22 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 23 | Error(const std::string& what) : ProtocolFactory::Error(what) {} |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 26 | explicit |
| 27 | TcpFactory(const std::string& defaultPort = "6363"); |
| 28 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 29 | /** |
| 30 | * \brief Create TCP-based channel using tcp::Endpoint |
| 31 | * |
| 32 | * tcp::Endpoint is really an alias for boost::asio::ip::tcp::endpoint. |
| 33 | * |
| 34 | * If this method called twice with the same endpoint, only one channel |
| 35 | * will be created. The second call will just retrieve the existing |
| 36 | * channel. |
| 37 | * |
| 38 | * \returns always a valid pointer to a TcpChannel object, an exception |
| 39 | * is thrown if it cannot be created. |
| 40 | * |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 41 | * \throws TcpFactory::Error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 42 | * |
| 43 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__tcp/endpoint.html |
| 44 | * for details on ways to create tcp::Endpoint |
| 45 | */ |
| 46 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 47 | createChannel(const tcp::Endpoint& localEndpoint); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * \brief Create TCP-based channel using specified host and port number |
| 51 | * |
| 52 | * This method will attempt to resolve the provided host and port numbers |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 53 | * and will throw TcpFactory::Error when channel cannot be created. |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 54 | * |
| 55 | * Note that this call will **BLOCK** until resolution is done or failed. |
| 56 | * |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame^] | 57 | * \throws TcpFactory::Error or std::runtime_error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 58 | */ |
| 59 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 60 | createChannel(const std::string& localHost, const std::string& localPort); |
| 61 | |
| 62 | // from Factory |
| 63 | |
| 64 | virtual void |
| 65 | createFace(const FaceUri& uri, |
| 66 | const FaceCreatedCallback& onCreated, |
| 67 | const FaceConnectFailedCallback& onConnectFailed); |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 68 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 69 | private: |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 70 | /** |
| 71 | * \brief Look up TcpChannel using specified local endpoint |
| 72 | * |
| 73 | * \returns shared pointer to the existing TcpChannel object |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 74 | * or empty shared pointer when such channel does not exist |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 75 | * |
| 76 | * \throws never |
| 77 | */ |
| 78 | shared_ptr<TcpChannel> |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 79 | findChannel(const tcp::Endpoint& localEndpoint); |
| 80 | |
| 81 | void |
| 82 | continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint, |
| 83 | const FaceCreatedCallback& onCreated, |
| 84 | const FaceConnectFailedCallback& onConnectFailed); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 85 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 86 | private: |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 87 | typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap; |
| 88 | ChannelMap m_channels; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 89 | |
| 90 | std::string m_defaultPort; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 93 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 95 | #endif // NFD_FACE_TCP_FACTORY_HPP |