Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 95a71b1 | 2013-07-31 10:49:50 -0700 | [diff] [blame] | 8 | #ifndef NDN_UDPTRANSPORT_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_UDPTRANSPORT_HPP |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 11 | #include <string> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 12 | #include "../common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 13 | #include "transport.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 15 | struct ndn_UdpTransport; |
| 16 | struct ndn_BinaryXmlElementReader; |
| 17 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | class UdpTransport : public Transport { |
| 21 | public: |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 22 | /** |
| 23 | * A UdpTransport::ConnectionInfo extends Transport::ConnectionInfo to hold the host and port info for the UDP connection. |
| 24 | */ |
| 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 Thompson | 39d241a | 2013-10-07 16:50:15 -0700 | [diff] [blame] | 30 | * @param port The port number for the connection. If omitted, use 6363. |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 31 | */ |
Jeff Thompson | 39d241a | 2013-10-07 16:50:15 -0700 | [diff] [blame] | 32 | ConnectionInfo(const char *host, unsigned short port = 6363) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 33 | : host_(host), port_(port) |
| 34 | { |
| 35 | } |
Jeff Thompson | b2c01a3 | 2013-08-23 19:42:18 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 38 | * Get the host given to the constructor. |
| 39 | * @return A string reference for the host. |
| 40 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 41 | const std::string& |
| 42 | getHost() const { return host_; } |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Get the port given to the constructor. |
| 46 | * @return The port number. |
| 47 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 48 | unsigned short |
| 49 | getPort() const { return port_; } |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 51 | virtual |
| 52 | ~ConnectionInfo(); |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | std::string host_; |
| 56 | unsigned short port_; |
| 57 | }; |
| 58 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 59 | UdpTransport(); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 62 | * 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 65 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 66 | virtual void |
| 67 | connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Set data to the host |
| 71 | * @param data A pointer to the buffer of data to send. |
| 72 | * @param dataLength The number of bytes in data. |
| 73 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 74 | virtual void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 75 | send(const uint8_t *data, size_t dataLength); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 77 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 78 | * Process any data to receive. For each element received, call elementListener.onReceivedElement. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 79 | * This is non-blocking and will return immediately if there is no data to receive. |
| 80 | * 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] | 81 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 82 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 83 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 84 | virtual void |
| 85 | processEvents(); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 87 | virtual bool |
| 88 | getIsConnected(); |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 89 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Close the connection to the host. |
| 92 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 93 | virtual void |
| 94 | close(); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 95 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 96 | ~UdpTransport(); |
| 97 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 98 | private: |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 99 | ptr_lib::shared_ptr<struct ndn_UdpTransport> transport_; |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 100 | bool isConnected_; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 101 | ElementListener *elementListener_; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 102 | // TODO: This belongs in the socket listener. |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 103 | ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | #endif |