blob: 3ea17036cecb7de143e28a2dca49c211a92dfdec [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 Thompson0050abe2013-09-17 12:50:25 -070038 const std::string&
39 getHost() const { return host_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070040
41 /**
42 * Get the port given to the constructor.
43 * @return The port number.
44 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070045 unsigned short
46 getPort() const { return port_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070047
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 virtual
49 ~ConnectionInfo();
Jeff Thompson10e34382013-08-22 13:34:46 -070050
51 private:
52 std::string host_;
53 unsigned short port_;
54 };
55
Jeff Thompsonbc53c522013-07-17 17:11:48 -070056 UdpTransport()
Jeff Thompson10e34382013-08-22 13:34:46 -070057 : elementListener_(0), isConnected_(false)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070058 {
Jeff Thompsond1427fb2013-08-29 17:20:32 -070059 ndn_UdpTransport_initialize(&transport_);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070060 elementReader_.partialData.array = 0;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070061 }
62
63 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070064 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
65 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
66 * @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 -070067 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070068 virtual void
69 connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070070
Jeff Thompson432c8be2013-08-09 16:16:08 -070071 /**
72 * Set data to the host
73 * @param data A pointer to the buffer of data to send.
74 * @param dataLength The number of bytes in data.
75 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 virtual void
77 send(const unsigned char *data, unsigned int dataLength);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070078
Jeff Thompson432c8be2013-08-09 16:16:08 -070079 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070080 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070081 * This is non-blocking and will return immediately if there is no data to receive.
82 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070083 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
84 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
85 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070086 virtual void
87 processEvents();
Jeff Thompsonbc53c522013-07-17 17:11:48 -070088
Jeff Thompson0050abe2013-09-17 12:50:25 -070089 virtual bool
90 getIsConnected();
Jeff Thompsona4056972013-08-22 11:52:21 -070091
Jeff Thompson432c8be2013-08-09 16:16:08 -070092 /**
93 * Close the connection to the host.
94 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070095 virtual void
96 close();
Jeff Thompson57963882013-08-05 16:01:25 -070097
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