Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #ifndef NDN_TRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_TRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 11 | namespace ndn { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 13 | class ElementListener; |
| 14 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 15 | class Transport { |
| 16 | public: |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 17 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 18 | * A Transport::ConnectionInfo is a base class for connection information used by subclasses of Transport. |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 19 | */ |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 20 | class ConnectionInfo { |
| 21 | public: |
| 22 | virtual ~ConnectionInfo(); |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener. |
| 27 | * @param connectionInfo A reference to an object of a subclass of ConnectionInfo. |
| 28 | * @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object. |
| 29 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 30 | virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 31 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Set data to the host |
| 34 | * @param data A pointer to the buffer of data to send. |
| 35 | * @param dataLength The number of bytes in data. |
| 36 | */ |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 37 | virtual void send(const unsigned char *data, unsigned int dataLength); |
| 38 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 39 | void send(const std::vector<unsigned char>& data) |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 40 | { |
| 41 | send(&data[0], data.size()); |
| 42 | } |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 45 | * Process any data to receive. For each element received, call elementListener.onReceivedElement. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 46 | * This is non-blocking and will silently time out after a brief period if there is no data to receive. |
| 47 | * You should repeatedly call this from an event loop. |
| 48 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 49 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 50 | */ |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 51 | virtual void processEvents() = 0; |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 53 | virtual bool getIsConnected(); |
| 54 | |
Jeff Thompson | 7098dd6 | 2013-08-06 14:42:02 -0700 | [diff] [blame] | 55 | /** |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 56 | * Close the connection. This base class implementation does nothing, but your derived class can override. |
| 57 | */ |
| 58 | virtual void close(); |
Jeff Thompson | 43d6c0a | 2013-08-12 12:35:48 -0700 | [diff] [blame] | 59 | |
| 60 | virtual ~Transport(); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | #endif |