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 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 11 | #include <ndn-cpp/common.hpp> |
| 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> |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 15 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 16 | namespace ndn { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 17 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 18 | class Transport { |
| 19 | public: |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 20 | typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback; |
| 21 | |
| 22 | inline |
| 23 | Transport(); |
| 24 | |
| 25 | inline virtual |
| 26 | ~Transport(); |
| 27 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 28 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 29 | * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener. |
| 30 | * @param connectionInfo A reference to an object of a subclass of ConnectionInfo. |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 31 | */ |
| 32 | inline virtual void |
| 33 | connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback); |
| 34 | |
| 35 | /** |
| 36 | * Close the connection. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 37 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 38 | virtual void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 39 | close() = 0; |
| 40 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Set data to the host |
| 43 | * @param data A pointer to the buffer of data to send. |
| 44 | * @param dataLength The number of bytes in data. |
| 45 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 46 | virtual void |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 47 | send(const Block &wire) = 0; |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 49 | inline bool |
| 50 | isConnected(); |
| 51 | |
| 52 | protected: |
| 53 | inline void |
| 54 | receive(const Block &wire); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 56 | protected: |
| 57 | boost::asio::io_service *ioService_; |
| 58 | bool isConnected_; |
| 59 | ReceiveCallback receiveCallback_; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame^] | 62 | inline |
| 63 | Transport::Transport() |
| 64 | : ioService_(0) |
| 65 | , isConnected_(false) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | inline |
| 70 | Transport::~Transport() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | inline void |
| 75 | Transport::connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback) |
| 76 | { |
| 77 | ioService_ = &ioService; |
| 78 | receiveCallback_ = receiveCallback; |
| 79 | } |
| 80 | |
| 81 | inline bool |
| 82 | Transport::isConnected() |
| 83 | { |
| 84 | return isConnected_; |
| 85 | } |
| 86 | |
| 87 | inline void |
| 88 | Transport::receive(const Block &wire) |
| 89 | { |
| 90 | receiveCallback_(wire); |
| 91 | } |
| 92 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #endif |