blob: 9e12ef64295c4e42d4abb3a1e9082b086c305706 [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 Thompson6ac3c0c2013-08-24 10:26:00 -070027 * @param port The port number for the connection. If omitted, use 9695.
Jeff Thompson10e34382013-08-22 13:34:46 -070028 */
Jeff Thompson6ac3c0c2013-08-24 10:26:00 -070029 ConnectionInfo(const char *host, unsigned short port = 9695)
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 Thompson1656e6a2013-08-29 18:01:48 -070038 const std::string& getHost() const { return host_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070039
40 /**
41 * Get the port given to the constructor.
42 * @return The port number.
43 */
44 unsigned short getPort() const { return port_; }
45
46 virtual ~ConnectionInfo();
47
48 private:
49 std::string host_;
50 unsigned short port_;
51 };
52
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070053 TcpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070054 : elementListener_(0), isConnected_(false)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070055 {
Jeff Thompsond1427fb2013-08-29 17:20:32 -070056 ndn_TcpTransport_initialize(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070057 elementReader_.partialData.array = 0;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070058 }
59
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070060 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070061 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
62 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
63 * @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 -070064 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070065 virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070066
Jeff Thompson432c8be2013-08-09 16:16:08 -070067 /**
68 * Set data to the host
69 * @param data A pointer to the buffer of data to send.
70 * @param dataLength The number of bytes in data.
71 */
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070072 virtual void send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070073
Jeff Thompson432c8be2013-08-09 16:16:08 -070074 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070075 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070076 * This is non-blocking and will return immediately if there is no data to receive.
77 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070078 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
79 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
80 */
81 virtual void processEvents();
Jeff Thompson57963882013-08-05 16:01:25 -070082
Jeff Thompsona4056972013-08-22 11:52:21 -070083 virtual bool getIsConnected();
84
Jeff Thompson432c8be2013-08-09 16:16:08 -070085 /**
86 * Close the connection to the host.
87 */
Jeff Thompson57963882013-08-05 16:01:25 -070088 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070089
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070090 ~TcpTransport();
91
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070092private:
93 struct ndn_TcpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070094 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -070095 ElementListener *elementListener_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070096 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070097 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070098};
99
100}
101
102#endif