blob: c87cdf432fa9f8f8a469ccbba200e9dfda72c213 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- 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 Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080014
Alexander Afanasyev7329e022014-02-27 14:47:22 -080015class TcpChannelFactory : public ChannelFactory
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080016{
17public:
18 /**
19 * \brief Exception of TcpChannelFactory
20 */
Alexander Afanasyev7329e022014-02-27 14:47:22 -080021 struct Error : public ChannelFactory::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080022 {
Alexander Afanasyev7329e022014-02-27 14:47:22 -080023 Error(const std::string& what) : ChannelFactory::Error(what) {}
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080024 };
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 *
38 * \throws TcpChannelFactory::Error
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
50 * and will throw TcpChannelFactory::Error when channel cannot be created.
51 *
52 * Note that this call will **BLOCK** until resolution is done or failed.
53 *
54 * \throws TcpChannelFactory::Error
55 */
56 shared_ptr<TcpChannel>
57 create(const std::string& localHost, const std::string& localPort);
58
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010059private:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080060 /**
61 * \brief Look up TcpChannel using specified local endpoint
62 *
63 * \returns shared pointer to the existing TcpChannel object
Alexander Afanasyev7329e022014-02-27 14:47:22 -080064 * or empty shared pointer when such channel does not exist
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080065 *
66 * \throws never
67 */
68 shared_ptr<TcpChannel>
69 find(const tcp::Endpoint& localEndpoint);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010070
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080071private:
Alexander Afanasyev7329e022014-02-27 14:47:22 -080072 typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap;
73 ChannelMap m_channels;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080074};
75
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080076} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080077
78#endif // NFD_FACE_TCP_CHANNEL_FACTORY_HPP