blob: e383f4ea450721a4f13f42289cec707f6e5e6c86 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070011 */
12
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080013#ifndef NDN_TRANSPORT_TRANSPORT_HPP
14#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015
Alexander Afanasyev19508852014-01-29 01:01:51 -080016#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080017#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070018
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070019#include <boost/asio.hpp>
20
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070021namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070022
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070023class Transport
24{
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070025public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060026 class Error : public std::runtime_error
27 {
28 public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029 inline Error(const boost::system::error_code& code, const std::string& msg);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060030 inline Error(const std::string& msg);
31 };
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070033 typedef function<void (const Block& wire)> ReceiveCallback;
34 typedef function<void ()> ErrorCallback;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080036 inline
37 Transport();
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080039 inline virtual
40 ~Transport();
41
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070042 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070043 * @brief Connect transport
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080044 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070045 * @throws boost::system::system_error if connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080046 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070047 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080048 connect(boost::asio::io_service& io_service,
49 const ReceiveCallback& receiveCallback);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080051 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070052 * @brief Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070053 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000055 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080056
Jeff Thompson432c8be2013-08-09 16:16:08 -070057 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070058 * @brief Send block of data from @param wire through the transport
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080059 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070060 * @param wire A block of data to send
Jeff Thompson432c8be2013-08-09 16:16:08 -070061 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000063 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070064
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080065 /**
66 * @brief Alternative version of sending data, applying scatter/gather I/O concept
67 *
68 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
69 * same message in datagram-oriented transports.
70 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000072 send(const Block& header, const Block& payload) = 0;
73
74 virtual void
75 pause() = 0;
76
77 virtual void
78 resume() = 0;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079
80 inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080081 isConnected();
82
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000083 inline bool
84 isExpectingData();
85
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080086protected:
87 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080088 receive(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080090protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091 boost::asio::io_service* m_ioService;
92 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000093 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080094 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070095};
96
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080097inline
98Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080099 : m_ioService(0)
100 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000101 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800102{
103}
104
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600105inline
106Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800107 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
108{
109}
110
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800111inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600112Transport::Error::Error(const std::string& msg)
113 : std::runtime_error(msg)
114{
115}
116
117inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800118Transport::~Transport()
119{
120}
121
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800123Transport::connect(boost::asio::io_service& ioService,
124 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800125{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800126 m_ioService = &ioService;
127 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800128}
129
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700130inline bool
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800131Transport::isConnected()
132{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800133 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134}
135
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000136inline bool
137Transport::isExpectingData()
138{
139 return m_isExpectingData;
140}
141
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800142inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800143Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800144{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800145 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800146}
147
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800148} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700149
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800150#endif // NDN_TRANSPORT_TRANSPORT_HPP