blob: fa3bd470c92374765e3e2d2862f7da3fecf837f6 [file] [log] [blame]
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#ifndef NDN_TCPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_TCPTRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07008
Jeff Thompson10e34382013-08-22 13:34:46 -07009#include <string>
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "../c/transport/tcp-transport.h"
11#include "../c/encoding/binary-xml-element-reader.h"
12#include "transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070013
14namespace ndn {
15
16class TcpTransport : public Transport {
17public:
Jeff Thompson10e34382013-08-22 13:34:46 -070018 /**
Jeff Thompsonc9671082013-08-22 13:54:05 -070019 * A TcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the TCP connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070020 */
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 Thompson6ac3c0c2013-08-24 10:26:00 -070026 * @param port The port number for the connection. If omitted, use 9695.
Jeff Thompson10e34382013-08-22 13:34:46 -070027 */
Jeff Thompson6ac3c0c2013-08-24 10:26:00 -070028 ConnectionInfo(const char *host, unsigned short port = 9695)
Jeff Thompson10e34382013-08-22 13:34:46 -070029 : host_(host), port_(port)
30 {
31 }
Jeff Thompsonb2c01a32013-08-23 19:42:18 -070032
33 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070034 * Get the host given to the constructor.
35 * @return A string reference for the host.
36 */
37 const std::string &getHost() const { return host_; }
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 Thompsonfcf347d2013-07-15 11:30:44 -070052 TcpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070053 : elementListener_(0), isConnected_(false)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070054 {
55 ndn_TcpTransport_init(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070056 elementReader_.partialData.array = 0;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070057 }
58
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070059 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070060 * 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 Thompson2ed62fb2013-07-16 18:10:30 -070063 */
Jeff Thompson10e34382013-08-22 13:34:46 -070064 virtual void connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070065
Jeff Thompson432c8be2013-08-09 16:16:08 -070066 /**
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 Thompsonb605b5d2013-07-30 15:12:56 -070071 virtual void send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070072
Jeff Thompson432c8be2013-08-09 16:16:08 -070073 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070074 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070075 * 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 Thompson432c8be2013-08-09 16:16:08 -070077 * @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 Thompson57963882013-08-05 16:01:25 -070081
Jeff Thompsona4056972013-08-22 11:52:21 -070082 virtual bool getIsConnected();
83
Jeff Thompson432c8be2013-08-09 16:16:08 -070084 /**
85 * Close the connection to the host.
86 */
Jeff Thompson57963882013-08-05 16:01:25 -070087 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070088
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070089 ~TcpTransport();
90
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070091private:
92 struct ndn_TcpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070093 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -070094 ElementListener *elementListener_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070095 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070096 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070097};
98
99}
100
101#endif