blob: 6d833453eb7e56a1d11d7a91fb5dd06aee7cff83 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonbc53c522013-07-17 17:11:48 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbc53c522013-07-17 17:11:48 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson95a71b12013-07-31 10:49:50 -07008#ifndef NDN_UDPTRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_UDPTRANSPORT_HPP
Jeff Thompsonbc53c522013-07-17 17:11:48 -070010
Jeff Thompson10e34382013-08-22 13:34:46 -070011#include <string>
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include "../common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "transport.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070014
Jeff Thompson25b4e612013-10-10 16:03:24 -070015struct ndn_UdpTransport;
16struct ndn_BinaryXmlElementReader;
17
Jeff Thompsonbc53c522013-07-17 17:11:48 -070018namespace ndn {
19
20class UdpTransport : public Transport {
21public:
Jeff Thompson10e34382013-08-22 13:34:46 -070022 /**
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 Thompson39d241a2013-10-07 16:50:15 -070030 * @param port The port number for the connection. If omitted, use 6363.
Jeff Thompson10e34382013-08-22 13:34:46 -070031 */
Jeff Thompson39d241a2013-10-07 16:50:15 -070032 ConnectionInfo(const char *host, unsigned short port = 6363)
Jeff Thompson10e34382013-08-22 13:34:46 -070033 : host_(host), port_(port)
34 {
35 }
Jeff Thompsonb2c01a32013-08-23 19:42:18 -070036
37 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070038 * Get the host given to the constructor.
39 * @return A string reference for the host.
40 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 const std::string&
42 getHost() const { return host_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070043
44 /**
45 * Get the port given to the constructor.
46 * @return The port number.
47 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 unsigned short
49 getPort() const { return port_; }
Jeff Thompson10e34382013-08-22 13:34:46 -070050
Jeff Thompson0050abe2013-09-17 12:50:25 -070051 virtual
52 ~ConnectionInfo();
Jeff Thompson10e34382013-08-22 13:34:46 -070053
54 private:
55 std::string host_;
56 unsigned short port_;
57 };
58
Jeff Thompson25b4e612013-10-10 16:03:24 -070059 UdpTransport();
Jeff Thompsonbc53c522013-07-17 17:11:48 -070060
61 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070062 * 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 Thompsonbc53c522013-07-17 17:11:48 -070065 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070066 virtual void
67 connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070068
Jeff Thompson432c8be2013-08-09 16:16:08 -070069 /**
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 Thompson0050abe2013-09-17 12:50:25 -070074 virtual void
Jeff Thompson97223af2013-09-24 17:01:27 -070075 send(const uint8_t *data, size_t dataLength);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070076
Jeff Thompson432c8be2013-08-09 16:16:08 -070077 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070078 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070079 * 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 Thompson432c8be2013-08-09 16:16:08 -070081 * @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 Thompson0050abe2013-09-17 12:50:25 -070084 virtual void
85 processEvents();
Jeff Thompsonbc53c522013-07-17 17:11:48 -070086
Jeff Thompson0050abe2013-09-17 12:50:25 -070087 virtual bool
88 getIsConnected();
Jeff Thompsona4056972013-08-22 11:52:21 -070089
Jeff Thompson432c8be2013-08-09 16:16:08 -070090 /**
91 * Close the connection to the host.
92 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070093 virtual void
94 close();
Jeff Thompson57963882013-08-05 16:01:25 -070095
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070096 ~UdpTransport();
97
Jeff Thompsonbc53c522013-07-17 17:11:48 -070098private:
Jeff Thompson25b4e612013-10-10 16:03:24 -070099 ptr_lib::shared_ptr<struct ndn_UdpTransport> transport_;
Jeff Thompsona4056972013-08-22 11:52:21 -0700100 bool isConnected_;
Jeff Thompson10e34382013-08-22 13:34:46 -0700101 ElementListener *elementListener_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -0700102 // TODO: This belongs in the socket listener.
Jeff Thompson25b4e612013-10-10 16:03:24 -0700103 ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -0700104};
105
106}
107
108#endif