blob: 0cde016abc9df47947decfbe1f25e1cc8eec2f15 [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 Thompson1656e6a2013-08-29 18:01:48 -070020void UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070021{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070022 const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070023
Jeff Thompsonbc53c522013-07-17 17:11:48 -070024 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070025 if ((error = ndn_UdpTransport_connect(&transport_, (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort())))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070026 throw std::runtime_error(ndn_getErrorString(error));
27
28 // TODO: This belongs in the socket listener.
29 const unsigned int initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070030 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070031 ndn_BinaryXmlElementReader_initialize
Jeff Thompson10e34382013-08-22 13:34:46 -070032 (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070033
Jeff Thompsona4056972013-08-22 11:52:21 -070034 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070035 elementListener_ = &elementListener;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070036}
37
Jeff Thompson60eaae32013-07-30 15:46:19 -070038void UdpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070039{
40 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070041 if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070042 throw std::runtime_error(ndn_getErrorString(error));
43}
44
Jeff Thompson432c8be2013-08-09 16:16:08 -070045void UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070046{
Jeff Thompson432c8be2013-08-09 16:16:08 -070047 int receiveIsReady;
48 ndn_Error error;
49 if ((error = ndn_UdpTransport_receiveIsReady(&transport_, &receiveIsReady)))
50 throw std::runtime_error(ndn_getErrorString(error));
51 if (!receiveIsReady)
52 return;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070053
Jeff Thompson432c8be2013-08-09 16:16:08 -070054 unsigned char buffer[8000];
55 unsigned int nBytes;
56 if ((error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
57 throw std::runtime_error(ndn_getErrorString(error));
58
59 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070060}
61
Jeff Thompsona4056972013-08-22 11:52:21 -070062bool UdpTransport::getIsConnected()
63{
64 return isConnected_;
65}
66
Jeff Thompson57963882013-08-05 16:01:25 -070067void UdpTransport::close()
68{
69 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070070 if ((error = ndn_UdpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070071 throw std::runtime_error(ndn_getErrorString(error));
72}
73
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070074UdpTransport::~UdpTransport()
75{
76 if (elementReader_.partialData.array)
77 // Free the memory allocated in connect.
78 free(elementReader_.partialData.array);
79}
80
Jeff Thompsonbc53c522013-07-17 17:11:48 -070081}