blob: e6c2a7379fb69e2e1befdc362117912bda40b5f9 [file] [log] [blame]
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_TCPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_TCPTRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07009
Jeff Thompson10e34382013-08-22 13:34:46 -070010#include <string>
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "../c/transport/tcp-transport.h"
12#include "../c/encoding/binary-xml-element-reader.h"
13#include "transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070014
15namespace ndn {
16
17class TcpTransport : public Transport {
18public:
Jeff Thompson10e34382013-08-22 13:34:46 -070019 /**
Jeff Thompsonc9671082013-08-22 13:54:05 -070020 * A TcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the TCP connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070021 */
22 class ConnectionInfo : public Transport::ConnectionInfo {
23 public:
24 /**
25 * Create a ConnectionInfo with the given host and port.
26 * @param host The host for the connection.
Jeff Thompson39d241a2013-10-07 16:50:15 -070027 * @param port The port number for the connection. If omitted, use 6363.
Jeff Thompson10e34382013-08-22 13:34:46 -070028 */
Jeff Thompson39d241a2013-10-07 16:50:15 -070029 ConnectionInfo(const char *host, unsigned short port = 6363)
Jeff Thompson10e34382013-08-22 13:34:46 -070030 : host_(host), port_(port)
31 {
32 }
Jeff Thompsonb2c01a32013-08-23 19:42:18 -070033
34 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070035 * Get the host given to the constructor.
36 * @return A string reference for the host.
37 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070038 const std::string&
39 getHost() const { return host_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070040
41 /**
42 * Get the port given to the constructor.
43 * @return The port number.
44 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070045 unsigned short
46 getPort() const { return port_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070047
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 virtual
49 ~ConnectionInfo();
Jeff Thompson10e34382013-08-22 13:34:46 -070050
51 private:
52 std::string host_;
53 unsigned short port_;
54 };
55
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070056 TcpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070057 : elementListener_(0), isConnected_(false)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070058 {
Jeff Thompsond1427fb2013-08-29 17:20:32 -070059 ndn_TcpTransport_initialize(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070060 elementReader_.partialData.array = 0;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070061 }
62
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070063 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070064 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
65 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
66 * @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 -070067 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070068 virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070069
Jeff Thompson432c8be2013-08-09 16:16:08 -070070 /**
71 * Set data to the host
72 * @param data A pointer to the buffer of data to send.
73 * @param dataLength The number of bytes in data.
74 */
Jeff Thompson97223af2013-09-24 17:01:27 -070075 virtual void send(const uint8_t *data, size_t dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070076
Jeff Thompson432c8be2013-08-09 16:16:08 -070077 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070078 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070079 * This is non-blocking and will return immediately if there is no data to receive.
80 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070081 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
82 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
83 */
84 virtual void processEvents();
Jeff Thompson57963882013-08-05 16:01:25 -070085
Jeff Thompsona4056972013-08-22 11:52:21 -070086 virtual bool getIsConnected();
87
Jeff Thompson432c8be2013-08-09 16:16:08 -070088 /**
89 * Close the connection to the host.
90 */
Jeff Thompson57963882013-08-05 16:01:25 -070091 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070092
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070093 ~TcpTransport();
94
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070095private:
96 struct ndn_TcpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070097 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -070098 ElementListener *elementListener_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070099 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700100 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700101};
102
103}
104
105#endif