blob: 4be0144cd40f8184f405fc008c050499858d4274 [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:
Jeff Thompson10e34382013-08-22 13:34:46 -070022 /**
Jeff Thompsonc9671082013-08-22 13:54:05 -070023 * A TcpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the TCP connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070024 */
25 class ConnectionInfo : public Transport::ConnectionInfo {
26 public:
27 /**
28 * Create a ConnectionInfo with the given host and port.
29 * @param host The host for the connection.
Jeff Thompson39d241a2013-10-07 16:50:15 -070030 * @param port The port number for the connection. If omitted, use 6363.
Jeff Thompson10e34382013-08-22 13:34:46 -070031 */
Jeff Thompson39d241a2013-10-07 16:50:15 -070032 ConnectionInfo(const char *host, unsigned short port = 6363)
Jeff Thompson10e34382013-08-22 13:34:46 -070033 : host_(host), port_(port)
34 {
35 }
Jeff Thompsonb2c01a32013-08-23 19:42:18 -070036
37 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070038 * Get the host given to the constructor.
39 * @return A string reference for the host.
40 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 const std::string&
42 getHost() const { return host_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070043
44 /**
45 * Get the port given to the constructor.
46 * @return The port number.
47 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 unsigned short
49 getPort() const { return port_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070050
Jeff Thompson0050abe2013-09-17 12:50:25 -070051 virtual
52 ~ConnectionInfo();
Jeff Thompson10e34382013-08-22 13:34:46 -070053
54 private:
55 std::string host_;
56 unsigned short port_;
57 };
58
Jeff Thompson25b4e612013-10-10 16:03:24 -070059 TcpTransport();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070060
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070061 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070062 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
63 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
64 * @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 -070065 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070066 virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070067
Jeff Thompson432c8be2013-08-09 16:16:08 -070068 /**
69 * Set data to the host
70 * @param data A pointer to the buffer of data to send.
71 * @param dataLength The number of bytes in data.
72 */
Jeff Thompson97223af2013-09-24 17:01:27 -070073 virtual void send(const uint8_t *data, size_t dataLength);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070074
Jeff Thompson432c8be2013-08-09 16:16:08 -070075 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070076 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070077 * This is non-blocking and will return immediately if there is no data to receive.
78 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070079 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
80 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
81 */
82 virtual void processEvents();
Jeff Thompson57963882013-08-05 16:01:25 -070083
Jeff Thompsona4056972013-08-22 11:52:21 -070084 virtual bool getIsConnected();
85
Jeff Thompson432c8be2013-08-09 16:16:08 -070086 /**
87 * Close the connection to the host.
88 */
Jeff Thompson57963882013-08-05 16:01:25 -070089 virtual void close();
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070090
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070091 ~TcpTransport();
92
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070093private:
Jeff Thompson25b4e612013-10-10 16:03:24 -070094 ptr_lib::shared_ptr<struct ndn_TcpTransport> transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070095 bool isConnected_;
Jeff Thompsonb002f902013-07-16 18:07:18 -070096 // TODO: This belongs in the socket listener.
Jeff Thompson25b4e612013-10-10 16:03:24 -070097 ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070098};
99
100}
101
102#endif