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 | |
| 26 | /** |
| 27 | * \brief Create TCP-based channel using tcp::Endpoint |
| 28 | * |
| 29 | * tcp::Endpoint is really an alias for boost::asio::ip::tcp::endpoint. |
| 30 | * |
| 31 | * If this method called twice with the same endpoint, only one channel |
| 32 | * will be created. The second call will just retrieve the existing |
| 33 | * channel. |
| 34 | * |
| 35 | * \returns always a valid pointer to a TcpChannel object, an exception |
| 36 | * is thrown if it cannot be created. |
| 37 | * |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 38 | * \throws TcpFactory::Error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 39 | * |
| 40 | * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__tcp/endpoint.html |
| 41 | * for details on ways to create tcp::Endpoint |
| 42 | */ |
| 43 | shared_ptr<TcpChannel> |
| 44 | create(const tcp::Endpoint& localEndpoint); |
| 45 | |
| 46 | /** |
| 47 | * \brief Create TCP-based channel using specified host and port number |
| 48 | * |
| 49 | * This method will attempt to resolve the provided host and port numbers |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 50 | * and will throw TcpFactory::Error when channel cannot be created. |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 51 | * |
| 52 | * Note that this call will **BLOCK** until resolution is done or failed. |
| 53 | * |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 54 | * \throws TcpFactory::Error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 55 | */ |
| 56 | shared_ptr<TcpChannel> |
| 57 | create(const std::string& localHost, const std::string& localPort); |
| 58 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 59 | private: |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 60 | /** |
| 61 | * \brief Look up TcpChannel using specified local endpoint |
| 62 | * |
| 63 | * \returns shared pointer to the existing TcpChannel object |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 64 | * or empty shared pointer when such channel does not exist |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 65 | * |
| 66 | * \throws never |
| 67 | */ |
| 68 | shared_ptr<TcpChannel> |
| 69 | find(const tcp::Endpoint& localEndpoint); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 70 | |
Alexander Afanasyev | 8ad71ba | 2014-01-27 00:07:14 -0800 | [diff] [blame] | 71 | private: |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 72 | typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap; |
| 73 | ChannelMap m_channels; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 76 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 78 | #endif // NFD_FACE_TCP_FACTORY_HPP |