blob: 0b2f3978a00a4a7133a287e7f275684ddafb906f [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
7#include <stdexcept>
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07008#include "../face.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -07009#include "../c/util/ndn_realloc.h"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "udp-transport.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070011
12using namespace std;
13
14namespace ndn {
15
Jeff Thompson10e34382013-08-22 13:34:46 -070016UdpTransport::ConnectionInfo::~ConnectionInfo()
17{
18}
19
Jeff Thompson0050abe2013-09-17 12:50:25 -070020void
21UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070022{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070023 const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070024
Jeff Thompsonbc53c522013-07-17 17:11:48 -070025 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070026 if ((error = ndn_UdpTransport_connect(&transport_, (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort())))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070027 throw std::runtime_error(ndn_getErrorString(error));
28
29 // TODO: This belongs in the socket listener.
30 const unsigned int initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070031 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070032 ndn_BinaryXmlElementReader_initialize
Jeff Thompson10ad12a2013-09-24 16:19:11 -070033 (&elementReader_, &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070034
Jeff Thompsona4056972013-08-22 11:52:21 -070035 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070036 elementListener_ = &elementListener;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070037}
38
Jeff Thompson0050abe2013-09-17 12:50:25 -070039void
Jeff Thompson10ad12a2013-09-24 16:19:11 -070040UdpTransport::send(const uint8_t *data, unsigned int dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070041{
42 ndn_Error error;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070043 if ((error = ndn_UdpTransport_send(&transport_, (uint8_t *)data, dataLength)))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070044 throw std::runtime_error(ndn_getErrorString(error));
45}
46
Jeff Thompson0050abe2013-09-17 12:50:25 -070047void
48UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070049{
Jeff Thompson432c8be2013-08-09 16:16:08 -070050 int receiveIsReady;
51 ndn_Error error;
52 if ((error = ndn_UdpTransport_receiveIsReady(&transport_, &receiveIsReady)))
53 throw std::runtime_error(ndn_getErrorString(error));
54 if (!receiveIsReady)
55 return;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070056
Jeff Thompson10ad12a2013-09-24 16:19:11 -070057 uint8_t buffer[8000];
Jeff Thompson432c8be2013-08-09 16:16:08 -070058 unsigned int nBytes;
59 if ((error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
60 throw std::runtime_error(ndn_getErrorString(error));
61
62 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070063}
64
Jeff Thompson0050abe2013-09-17 12:50:25 -070065bool
66UdpTransport::getIsConnected()
Jeff Thompsona4056972013-08-22 11:52:21 -070067{
68 return isConnected_;
69}
70
Jeff Thompson0050abe2013-09-17 12:50:25 -070071void
72UdpTransport::close()
Jeff Thompson57963882013-08-05 16:01:25 -070073{
74 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070075 if ((error = ndn_UdpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070076 throw std::runtime_error(ndn_getErrorString(error));
77}
78
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070079UdpTransport::~UdpTransport()
80{
81 if (elementReader_.partialData.array)
82 // Free the memory allocated in connect.
83 free(elementReader_.partialData.array);
84}
85
Jeff Thompsonbc53c522013-07-17 17:11:48 -070086}