blob: e54516a6f17c8ceda2ed2d586434254458d97ad0 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_TCPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_TCPTRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070010
Jeff Thompson10e34382013-08-22 13:34:46 -070011#include <string>
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include "../common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070014
Jeff Thompson25b4e612013-10-10 16:03:24 -070015struct ndn_TcpTransport;
16struct ndn_BinaryXmlElementReader;
17
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070018namespace ndn {
19
20class TcpTransport : public Transport {
21public:
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080022 TcpTransport(const char *host, unsigned short port = 6363);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070023
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070024 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070025 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
26 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
27 * @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 -070028 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080029 virtual void connect(ElementListener& elementListener);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070030
Jeff Thompson432c8be2013-08-09 16:16:08 -070031 /**
32 * Set data to the host
33 * @param data A pointer to the buffer of data to send.
34 * @param dataLength The number of bytes in data.
35 */
Jeff Thompson97223af2013-09-24 17:01:27 -070036 virtual void send(const uint8_t *data, size_t dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070037
Jeff Thompson432c8be2013-08-09 16:16:08 -070038 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070039 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070040 * This is non-blocking and will return immediately if there is no data to receive.
41 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070042 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
43 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
44 */
45 virtual void processEvents();
Jeff Thompson57963882013-08-05 16:01:25 -070046
Jeff Thompsona4056972013-08-22 11:52:21 -070047 virtual bool getIsConnected();
48
Jeff Thompson432c8be2013-08-09 16:16:08 -070049 /**
50 * Close the connection to the host.
51 */
Jeff Thompson57963882013-08-05 16:01:25 -070052 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070053
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070054 ~TcpTransport();
55
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070056private:
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080057 std::string host_;
58 unsigned short port_;
59
Jeff Thompsona4056972013-08-22 11:52:21 -070060 bool isConnected_;
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080061 ptr_lib::shared_ptr<struct ndn_TcpTransport> transport_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070062 // TODO: This belongs in the socket listener.
Jeff Thompson25b4e612013-10-10 16:03:24 -070063 ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070064};
65
66}
67
68#endif