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