blob: fbda795d2c0cd8450c9505cb9104801fa356d58b [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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -08007#ifndef NFD_FACE_TCP_FACTORY_HPP
8#define NFD_FACE_TCP_FACTORY_HPP
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08009
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080010#include "protocol-factory.hpp"
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080011#include "tcp-channel.hpp"
12
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080014
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080015class TcpFactory : public ProtocolFactory
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080016{
17public:
18 /**
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080019 * \brief Exception of TcpFactory
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080020 */
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080021 struct Error : public ProtocolFactory::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080022 {
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080023 Error(const std::string& what) : ProtocolFactory::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 *
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080038 * \throws TcpFactory::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080039 *
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 Afanasyev0eb70652014-02-27 18:35:07 -080050 * and will throw TcpFactory::Error when channel cannot be created.
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080051 *
52 * Note that this call will **BLOCK** until resolution is done or failed.
53 *
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080054 * \throws TcpFactory::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080055 */
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
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080078#endif // NFD_FACE_TCP_FACTORY_HPP