blob: e678491636410600e10f9460dd46fca674ff776b [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/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07004 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyev5964fb72014-02-18 12:42:45 -08007#ifndef NDN_TRANSPORT_TRANSPORT_HPP
8#define NDN_TRANSPORT_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07009
Alexander Afanasyev19508852014-01-29 01:01:51 -080010#include "../common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080011#include "../encoding/block.hpp"
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070012
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070013namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070014
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015class Transport {
16public:
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060017 class Error : public std::runtime_error
18 {
19 public:
20 inline Error(const boost::system::error_code &code, const std::string &msg);
21 inline Error(const std::string& msg);
22 };
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080023
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080024 typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080025 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080026
27 inline
28 Transport();
29
30 inline virtual
31 ~Transport();
32
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070033 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080034 * Connect transport
35 *
36 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080037 */
38 inline virtual void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080039 connect(boost::asio::io_service& io_service,
40 const ReceiveCallback& receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080041
42 /**
43 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070044 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070045 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000046 close() = 0;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080047
Jeff Thompson432c8be2013-08-09 16:16:08 -070048 /**
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080049 * @brief Set data to the host
50 *
Jeff Thompson432c8be2013-08-09 16:16:08 -070051 * @param data A pointer to the buffer of data to send.
52 * @param dataLength The number of bytes in data.
53 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070054 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000055 send(const Block& wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070056
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080057 /**
58 * @brief Alternative version of sending data, applying scatter/gather I/O concept
59 *
60 * Two non-consecutive memory blocks will be send out together, e.g., as part of the
61 * same message in datagram-oriented transports.
62 */
63 virtual void
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000064 send(const Block& header, const Block& payload) = 0;
65
66 virtual void
67 pause() = 0;
68
69 virtual void
70 resume() = 0;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080071
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080072 inline bool
73 isConnected();
74
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000075 inline bool
76 isExpectingData();
77
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080078protected:
79 inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080080 receive(const Block& wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070081
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080082protected:
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080083 boost::asio::io_service* m_ioService;
84 bool m_isConnected;
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000085 bool m_isExpectingData;
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080086 ReceiveCallback m_receiveCallback;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070087};
88
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080089inline
90Transport::Transport()
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080091 : m_ioService(0)
92 , m_isConnected(false)
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +000093 , m_isExpectingData(false)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080094{
95}
96
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060097inline
98Transport::Error::Error(const boost::system::error_code& code, const std::string& msg)
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080099 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
100{
101}
102
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800103inline
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600104Transport::Error::Error(const std::string& msg)
105 : std::runtime_error(msg)
106{
107}
108
109inline
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800110Transport::~Transport()
111{
112}
113
114inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800115Transport::connect(boost::asio::io_service& ioService,
116 const ReceiveCallback& receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800117{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800118 m_ioService = &ioService;
119 m_receiveCallback = receiveCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800120}
121
122inline bool
123Transport::isConnected()
124{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800125 return m_isConnected;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800126}
127
Alexander Afanasyev52afb3f2014-03-07 09:05:35 +0000128inline bool
129Transport::isExpectingData()
130{
131 return m_isExpectingData;
132}
133
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800134inline void
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800135Transport::receive(const Block& wire)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800136{
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800137 m_receiveCallback(wire);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800138}
139
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800140} // namespace ndn
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700141
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800142#endif // NDN_TRANSPORT_TRANSPORT_HPP