Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 95a71b1 | 2013-07-31 10:49:50 -0700 | [diff] [blame] | 7 | #ifndef NDN_UDPTRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_UDPTRANSPORT_HPP |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 10 | #include <string> |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "../c/transport/udp-transport.h" |
| 12 | #include "../c/encoding/binary-xml-element-reader.h" |
| 13 | #include "transport.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | class UdpTransport : public Transport { |
| 18 | public: |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 19 | /** |
| 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 Thompson | 6ac3c0c | 2013-08-24 10:26:00 -0700 | [diff] [blame] | 27 | * @param port The port number for the connection. If omitted, use 9695. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 28 | */ |
Jeff Thompson | 6ac3c0c | 2013-08-24 10:26:00 -0700 | [diff] [blame] | 29 | ConnectionInfo(const char *host, unsigned short port = 9695) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 30 | : host_(host), port_(port) |
| 31 | { |
| 32 | } |
Jeff Thompson | b2c01a3 | 2013-08-23 19:42:18 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 35 | * Get the host given to the constructor. |
| 36 | * @return A string reference for the host. |
| 37 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 38 | const std::string& |
| 39 | getHost() const { return host_; } |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Get the port given to the constructor. |
| 43 | * @return The port number. |
| 44 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 45 | unsigned short |
| 46 | getPort() const { return port_; } |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 48 | virtual |
| 49 | ~ConnectionInfo(); |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | std::string host_; |
| 53 | unsigned short port_; |
| 54 | }; |
| 55 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 56 | UdpTransport() |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 57 | : elementListener_(0), isConnected_(false) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 58 | { |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 59 | ndn_UdpTransport_initialize(&transport_); |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 60 | elementReader_.partialData.array = 0; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 64 | * 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 67 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 68 | virtual void |
| 69 | connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 70 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 71 | /** |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 76 | virtual void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 77 | send(const uint8_t *data, size_t dataLength); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 79 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 80 | * Process any data to receive. For each element received, call elementListener.onReceivedElement. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 81 | * 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 Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 83 | * @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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 86 | virtual void |
| 87 | processEvents(); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 88 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 89 | virtual bool |
| 90 | getIsConnected(); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Close the connection to the host. |
| 94 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 95 | virtual void |
| 96 | close(); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 97 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 98 | ~UdpTransport(); |
| 99 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 100 | private: |
| 101 | struct ndn_UdpTransport transport_; |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 102 | bool isConnected_; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 103 | ElementListener *elementListener_; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 104 | // TODO: This belongs in the socket listener. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 105 | ndn_BinaryXmlElementReader elementReader_; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | #endif |