blob: 171c24f82232ea7b754af574eaf8cfdad72caf98 [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
15class TcpChannelFactory : public ChannelFactory<tcp::Endpoint, TcpChannel>
16{
17public:
18 /**
19 * \brief Exception of TcpChannelFactory
20 */
Alexander Afanasyevd7228c02014-02-11 19:25:31 -080021 struct Error : public ChannelFactory<tcp::Endpoint, TcpChannel>::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080022 {
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010023 Error(const std::string& what)
24 : ChannelFactory<tcp::Endpoint, TcpChannel>::Error(what)
25 {
26 }
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080027 };
28
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010029 explicit
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080030 TcpChannelFactory(boost::asio::io_service& ioService);
31
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080032 /**
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 Pesaventobc4dd8c2014-02-14 20:01:01 +010065private:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080066 /**
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 Pesaventobc4dd8c2014-02-14 20:01:01 +010076
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080077private:
78 boost::asio::io_service& m_ioService;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080079};
80
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080081} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080082
83#endif // NFD_FACE_TCP_CHANNEL_FACTORY_HPP