blob: 39dfbe2015770fecaf8aaf9d5fc69b3fe0a42324 [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 /**
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080023 * Create a UdpTransport with the given host and port.
24 * @param host The host for the connection.
25 * @param port The port number for the connection. If omitted, use 6363.
Jeff Thompson10e34382013-08-22 13:34:46 -070026 */
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080027 UdpTransport(const char *host, unsigned short port = 6363);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070028
29 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070030 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
31 * @param connectionInfo A reference to a TcpTransport::ConnectionInfo.
32 * @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 -070033 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070034 virtual void
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080035 connect(ElementListener& elementListener);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070036
Jeff Thompson432c8be2013-08-09 16:16:08 -070037 /**
38 * Set data to the host
39 * @param data A pointer to the buffer of data to send.
40 * @param dataLength The number of bytes in data.
41 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 virtual void
Jeff Thompson97223af2013-09-24 17:01:27 -070043 send(const uint8_t *data, size_t dataLength);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070044
Jeff Thompson432c8be2013-08-09 16:16:08 -070045 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070046 * Process any data to receive. For each element received, call elementListener.onReceivedElement.
Jeff Thompsonc7e07442013-08-19 15:25:43 -070047 * This is non-blocking and will return immediately if there is no data to receive.
48 * You should normally not call this directly since it is called by Face.processEvents.
Jeff Thompson432c8be2013-08-09 16:16:08 -070049 * @throw This may throw an exception for reading data or in the callback for processing the data. If you
50 * call this from an main event loop, you may want to catch and log/disregard all exceptions.
51 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070052 virtual void
53 processEvents();
Jeff Thompsonbc53c522013-07-17 17:11:48 -070054
Jeff Thompson0050abe2013-09-17 12:50:25 -070055 virtual bool
56 getIsConnected();
Jeff Thompsona4056972013-08-22 11:52:21 -070057
Jeff Thompson432c8be2013-08-09 16:16:08 -070058 /**
59 * Close the connection to the host.
60 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070061 virtual void
62 close();
Jeff Thompson57963882013-08-05 16:01:25 -070063
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070064 ~UdpTransport();
65
Jeff Thompsonbc53c522013-07-17 17:11:48 -070066private:
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080067 std::string host_;
68 unsigned short port_;
69
Jeff Thompsona4056972013-08-22 11:52:21 -070070 bool isConnected_;
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080071 ptr_lib::shared_ptr<struct ndn_UdpTransport> transport_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070072 // TODO: This belongs in the socket listener.
Jeff Thompson25b4e612013-10-10 16:03:24 -070073 ptr_lib::shared_ptr<struct ndn_BinaryXmlElementReader> elementReader_;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070074};
75
76}
77
78#endif