Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_TRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_TRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 10 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 11 | #include <ndn-cpp-dev/common.hpp> |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 12 | |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 13 | #include <vector> |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 14 | #include <boost/asio.hpp> |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 15 | #include <boost/lexical_cast.hpp> |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 17 | namespace ndn { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 18 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 19 | class Transport { |
| 20 | public: |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 21 | struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); }; |
| 22 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 23 | typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback; |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 24 | typedef ptr_lib::function<void ()> ErrorCallback; |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 25 | |
| 26 | inline |
| 27 | Transport(); |
| 28 | |
| 29 | inline virtual |
| 30 | ~Transport(); |
| 31 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 32 | /** |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 33 | * Connect transport |
| 34 | * |
| 35 | * @throws If connection cannot be established |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 36 | */ |
| 37 | inline virtual void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 38 | connect(boost::asio::io_service &io_service, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 39 | const ReceiveCallback &receiveCallback); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Close the connection. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 43 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 44 | virtual void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 45 | close() = 0; |
| 46 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Set data to the host |
| 49 | * @param data A pointer to the buffer of data to send. |
| 50 | * @param dataLength The number of bytes in data. |
| 51 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 52 | virtual void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 53 | send(const Block &wire) = 0; |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 55 | inline bool |
| 56 | isConnected(); |
| 57 | |
| 58 | protected: |
| 59 | inline void |
| 60 | receive(const Block &wire); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 62 | protected: |
| 63 | boost::asio::io_service *ioService_; |
| 64 | bool isConnected_; |
| 65 | ReceiveCallback receiveCallback_; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 68 | inline |
| 69 | Transport::Transport() |
| 70 | : ioService_(0) |
| 71 | , isConnected_(false) |
| 72 | { |
| 73 | } |
| 74 | |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 75 | inline Transport::Error::Error(const boost::system::error_code &code, const std::string &msg) |
| 76 | : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : "")) |
| 77 | { |
| 78 | } |
| 79 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 80 | inline |
| 81 | Transport::~Transport() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | inline void |
Alexander Afanasyev | a557d5a | 2013-12-28 21:59:03 -0800 | [diff] [blame] | 86 | Transport::connect(boost::asio::io_service &ioService, |
Alexander Afanasyev | 3ae2da2 | 2013-12-29 15:50:04 -0800 | [diff] [blame] | 87 | const ReceiveCallback &receiveCallback) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 88 | { |
| 89 | ioService_ = &ioService; |
| 90 | receiveCallback_ = receiveCallback; |
| 91 | } |
| 92 | |
| 93 | inline bool |
| 94 | Transport::isConnected() |
| 95 | { |
| 96 | return isConnected_; |
| 97 | } |
| 98 | |
| 99 | inline void |
| 100 | Transport::receive(const Block &wire) |
| 101 | { |
| 102 | receiveCallback_(wire); |
| 103 | } |
| 104 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #endif |