blob: 6ba2e37947ae2ca888dc0cf24043d57b0f4042a3 [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
Alexander Afanasyevd6655302014-02-28 08:41:28 -080026 explicit
27 TcpFactory(const std::string& defaultPort = "6363");
28
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080029 /**
30 * \brief Create TCP-based channel using tcp::Endpoint
31 *
32 * tcp::Endpoint is really an alias for boost::asio::ip::tcp::endpoint.
33 *
34 * If this method called twice with the same endpoint, only one channel
35 * will be created. The second call will just retrieve the existing
36 * channel.
37 *
38 * \returns always a valid pointer to a TcpChannel object, an exception
39 * is thrown if it cannot be created.
40 *
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080041 * \throws TcpFactory::Error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080042 *
43 * \see http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio/reference/ip__tcp/endpoint.html
44 * for details on ways to create tcp::Endpoint
45 */
46 shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080047 createChannel(const tcp::Endpoint& localEndpoint);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080048
49 /**
50 * \brief Create TCP-based channel using specified host and port number
51 *
52 * This method will attempt to resolve the provided host and port numbers
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080053 * and will throw TcpFactory::Error when channel cannot be created.
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080054 *
55 * Note that this call will **BLOCK** until resolution is done or failed.
56 *
Alexander Afanasyev5f1ec252014-02-28 10:59:17 -080057 * \throws TcpFactory::Error or std::runtime_error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080058 */
59 shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080060 createChannel(const std::string& localHost, const std::string& localPort);
61
62 // from Factory
63
64 virtual void
65 createFace(const FaceUri& uri,
66 const FaceCreatedCallback& onCreated,
67 const FaceConnectFailedCallback& onConnectFailed);
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080068
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010069private:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080070 /**
71 * \brief Look up TcpChannel using specified local endpoint
72 *
73 * \returns shared pointer to the existing TcpChannel object
Alexander Afanasyev7329e022014-02-27 14:47:22 -080074 * or empty shared pointer when such channel does not exist
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080075 *
76 * \throws never
77 */
78 shared_ptr<TcpChannel>
Alexander Afanasyevd6655302014-02-28 08:41:28 -080079 findChannel(const tcp::Endpoint& localEndpoint);
80
81 void
82 continueCreateFaceAfterResolve(const tcp::Endpoint& endpoint,
83 const FaceCreatedCallback& onCreated,
84 const FaceConnectFailedCallback& onConnectFailed);
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010085
Alexander Afanasyev8ad71ba2014-01-27 00:07:14 -080086private:
Alexander Afanasyev7329e022014-02-27 14:47:22 -080087 typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap;
88 ChannelMap m_channels;
Alexander Afanasyevd6655302014-02-28 08:41:28 -080089
90 std::string m_defaultPort;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080091};
92
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080093} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080094
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080095#endif // NFD_FACE_TCP_FACTORY_HPP