blob: 06fbceb0895414ecec4f9e17d5e68834fe869841 [file] [log] [blame]
Jeff Thompsonbc53c522013-07-17 17:11:48 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson95a71b12013-07-31 10:49:50 -07006#ifndef NDN_UDPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_UDPTRANSPORT_HPP
Jeff Thompsonbc53c522013-07-17 17:11:48 -07008
Jeff Thompson10e34382013-08-22 13:34:46 -07009#include <string>
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "../c/transport/udp-transport.h"
11#include "../c/encoding/binary-xml-element-reader.h"
12#include "transport.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070013
14namespace ndn {
15
16class UdpTransport : public Transport {
17public:
Jeff Thompson10e34382013-08-22 13:34:46 -070018 /**
19 * A UdpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the UDP connection.
20 */
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.
26 * @param port The port number for the connection.
27 */
28 ConnectionInfo(const char *host, unsigned short port)
29 : host_(host), port_(port)
30 {
31 }
Jeff Thompsonb2c01a32013-08-23 19:42:18 -070032
33 /**
34 * Create a ConnectionInfo with the given host and default port 9695.
35 * @param host The host for the connection.
36 */
37 ConnectionInfo(const char *host)
38 : host_(host), port_(9695)
39 {
40 }
Jeff Thompson10e34382013-08-22 13:34:46 -070041
42 /**
43 * Get the host given to the constructor.
44 * @return A string reference for the host.
45 */
46 const std::string &getHost() const { return host_; }
47
48 /**
49 * Get the port given to the constructor.
50 * @return The port number.
51 */
52 unsigned short getPort() const { return port_; }
53
54 virtual ~ConnectionInfo();
55
56 private:
57 std::string host_;
58 unsigned short port_;
59 };
60
Jeff Thompsonbc53c522013-07-17 17:11:48 -070061 UdpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070062 : elementListener_(0), isConnected_(false)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070063 {
64 ndn_UdpTransport_init(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070065 elementReader_.partialData.array = 0;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070066 }
67
68 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070069 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
70 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
71 * @param elementListener Not a shared_ptr because we assume that it will remain valid during the life of this object.
Jeff Thompsonbc53c522013-07-17 17:11:48 -070072 */
Jeff Thompson10e34382013-08-22 13:34:46 -070073 virtual void connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070074
Jeff Thompson432c8be2013-08-09 16:16:08 -070075 /**
76 * Set data to the host
77 * @param data A pointer to the buffer of data to send.
78 * @param dataLength The number of bytes in data.
79 */
Jeff Thompson60eaae32013-07-30 15:46:19 -070080 virtual void send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070081
Jeff Thompson432c8be2013-08-09 16:16:08 -070082 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070083 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070084 * This is non-blocking and will return immediately if there is no data to receive.
85 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070086 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
87 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
88 */
89 virtual void processEvents();
Jeff Thompsonbc53c522013-07-17 17:11:48 -070090
Jeff Thompsona4056972013-08-22 11:52:21 -070091 virtual bool getIsConnected();
92
Jeff Thompson432c8be2013-08-09 16:16:08 -070093 /**
94 * Close the connection to the host.
95 */
Jeff Thompson57963882013-08-05 16:01:25 -070096 virtual void close();
97
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070098 ~UdpTransport();
99
Jeff Thompsonbc53c522013-07-17 17:11:48 -0700100private:
101 struct ndn_UdpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -0700102 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700103 ElementListener *elementListener_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -0700104 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700105 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -0700106};
107
108}
109
110#endif