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_TCPTRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_TCPTRANSPORT_HPP |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 9 | #include <string> |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "../c/transport/tcp-transport.h" |
| 11 | #include "../c/encoding/binary-xml-element-reader.h" |
| 12 | #include "transport.hpp" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | class TcpTransport : public Transport { |
| 17 | public: |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 18 | /** |
Jeff Thompson | c967108 | 2013-08-22 13:54:05 -0700 | [diff] [blame] | 19 | * A TcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the TCP connection. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 20 | */ |
| 21 | class ConnectionInfo : public Transport::ConnectionInfo { |
| 22 | public: |
| 23 | /** |
| 24 | * Create a ConnectionInfo with the given host and port. |
| 25 | * @param host The host for the connection. |
Jeff Thompson | 6ac3c0c | 2013-08-24 10:26:00 -0700 | [diff] [blame] | 26 | * @param port The port number for the connection. If omitted, use 9695. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 27 | */ |
Jeff Thompson | 6ac3c0c | 2013-08-24 10:26:00 -0700 | [diff] [blame] | 28 | ConnectionInfo(const char *host, unsigned short port = 9695) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 29 | : host_(host), port_(port) |
| 30 | { |
| 31 | } |
Jeff Thompson | b2c01a3 | 2013-08-23 19:42:18 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 34 | * Get the host given to the constructor. |
| 35 | * @return A string reference for the host. |
| 36 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 37 | const std::string& getHost() const { return host_; } |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Get the port given to the constructor. |
| 41 | * @return The port number. |
| 42 | */ |
| 43 | unsigned short getPort() const { return port_; } |
| 44 | |
| 45 | virtual ~ConnectionInfo(); |
| 46 | |
| 47 | private: |
| 48 | std::string host_; |
| 49 | unsigned short port_; |
| 50 | }; |
| 51 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 52 | TcpTransport() |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 53 | : elementListener_(0), isConnected_(false) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 55 | ndn_TcpTransport_initialize(&transport_); |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 56 | elementReader_.partialData.array = 0; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 59 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 60 | * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener. |
| 61 | * @param connectionInfo A reference to a TcpTransport::ConnectionInfo. |
| 62 | * @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object. |
Jeff Thompson | 2ed62fb | 2013-07-16 18:10:30 -0700 | [diff] [blame] | 63 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 64 | virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 65 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Set data to the host |
| 68 | * @param data A pointer to the buffer of data to send. |
| 69 | * @param dataLength The number of bytes in data. |
| 70 | */ |
Jeff Thompson | b605b5d | 2013-07-30 15:12:56 -0700 | [diff] [blame] | 71 | virtual void send(const unsigned char *data, unsigned int dataLength); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 72 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 73 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 74 | * Process any data to receive. For each element received, call elementListener.onReceivedElement. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 75 | * This is non-blocking and will return immediately if there is no data to receive. |
| 76 | * You should normally not call this directly since it is called by Face.processEvents. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 77 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 78 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 79 | */ |
| 80 | virtual void processEvents(); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 82 | virtual bool getIsConnected(); |
| 83 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Close the connection to the host. |
| 86 | */ |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 87 | virtual void close(); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 88 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 89 | ~TcpTransport(); |
| 90 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 91 | private: |
| 92 | struct ndn_TcpTransport transport_; |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 93 | bool isConnected_; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 94 | ElementListener *elementListener_; |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 95 | // TODO: This belongs in the socket listener. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 96 | ndn_BinaryXmlElementReader elementReader_; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | #endif |