blob: deb7dc5b34e6da85984b1b04acaedfa17d874453 [file] [log] [blame]
Jeff Thompsonbc53c522013-07-17 17:11:48 -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 Thompsonbc53c522013-07-17 17:11:48 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson95a71b12013-07-31 10:49:50 -07007#ifndef NDN_UDPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_UDPTRANSPORT_HPP
Jeff Thompsonbc53c522013-07-17 17:11:48 -07009
Jeff Thompson10e34382013-08-22 13:34:46 -070010#include <string>
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "../c/transport/udp-transport.h"
12#include "../c/encoding/binary-xml-element-reader.h"
13#include "transport.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070014
15namespace ndn {
16
17class UdpTransport : public Transport {
18public:
Jeff Thompson10e34382013-08-22 13:34:46 -070019 /**
20 * A UdpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the UDP connection.
21 */
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 Thompsonbc53c522013-07-17 17:11:48 -070053 UdpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070054 : elementListener_(0), isConnected_(false)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070055 {
Jeff Thompsond1427fb2013-08-29 17:20:32 -070056 ndn_UdpTransport_initialize(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070057 elementReader_.partialData.array = 0;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070058 }
59
60 /**
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 Thompsonbc53c522013-07-17 17:11:48 -070064 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070065 virtual void connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonbc53c522013-07-17 17:11:48 -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 Thompson60eaae32013-07-30 15:46:19 -070072 virtual void send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonbc53c522013-07-17 17:11:48 -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 Thompsonbc53c522013-07-17 17:11:48 -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();
89
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070090 ~UdpTransport();
91
Jeff Thompsonbc53c522013-07-17 17:11:48 -070092private:
93 struct ndn_UdpTransport transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -070094 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -070095 ElementListener *elementListener_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070096 // TODO: This belongs in the socket listener.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070097 ndn_BinaryXmlElementReader elementReader_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070098};
99
100}
101
102#endif