blob: 0073eeb1e94493db5edae29430965af6ba25dbb6 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070020 */
21
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080022#ifndef NDN_TRANSPORT_TRANSPORT_HPP
23#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070024
Alexander Afanasyev19508852014-01-29 01:01:51 -080025#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080026#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070027
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include <boost/asio.hpp>
29
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070030namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070031
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040032class Transport : noncopyable
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070034public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060035 class Error : public std::runtime_error
36 {
37 public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 inline Error(const boost::system::error_code& code, const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060039 inline Error(const std::string& msg);
40 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070042 typedef function<void (const Block& wire)> ReceiveCallback;
43 typedef function<void ()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080045 inline
46 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080048 inline virtual
49 ~Transport();
50
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070051 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070052 * @brief Connect transport
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080053 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070054 * @throws boost::system::system_error if connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080055 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080057 connect(boost::asio::io_service& io_service,
58 const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080060 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070061 * @brief Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070062 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000064 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080065
Jeff Thompson432c8be2013-08-09 16:16:08 -070066 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070067 * @brief Send block of data from @param wire through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080068 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070069 * @param wire A block of data to send
Jeff Thompson432c8be2013-08-09 16:16:08 -070070 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000072 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070073
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080074 /**
75 * @brief Alternative version of sending data, applying scatter/gather I/O concept
76 *
77 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
78 * same message in datagram-oriented transports.
79 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000081 send(const Block& header, const Block& payload) = 0;
82
83 virtual void
84 pause() = 0;
85
86 virtual void
87 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088
89 inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080090 isConnected();
91
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000092 inline bool
93 isExpectingData();
94
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080095protected:
96 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080097 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080099protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800100 boost::asio::io_service* m_ioService;
101 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000102 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800103 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700104};
105
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800106inline
107Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800108 : m_ioService(0)
109 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000110 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800111{
112}
113
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600114inline
115Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800116 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
117{
118}
119
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800120inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600121Transport::Error::Error(const std::string& msg)
122 : std::runtime_error(msg)
123{
124}
125
126inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800127Transport::~Transport()
128{
129}
130
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800132Transport::connect(boost::asio::io_service& ioService,
133 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800135 m_ioService = &ioService;
136 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800137}
138
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800140Transport::isConnected()
141{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800143}
144
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000145inline bool
146Transport::isExpectingData()
147{
148 return m_isExpectingData;
149}
150
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800151inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800152Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800153{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800154 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800155}
156
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800157} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700158
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800159#endif // NDN_TRANSPORT_TRANSPORT_HPP