blob: 2ac010adb2f44768f812865f62a65a0f724721bb [file] [log] [blame]
Jeff Thompsonbc53c522013-07-17 17:11:48 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#include <stdexcept>
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -07007#include "../face.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -07008#include "../c/util/ndn_realloc.h"
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "udp-transport.hpp"
Jeff Thompsonbc53c522013-07-17 17:11:48 -070010
11using namespace std;
12
13namespace ndn {
14
Jeff Thompson10e34382013-08-22 13:34:46 -070015UdpTransport::ConnectionInfo::~ConnectionInfo()
16{
17}
18
Jeff Thompson1656e6a2013-08-29 18:01:48 -070019void UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070020{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070021 const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070022
Jeff Thompsonbc53c522013-07-17 17:11:48 -070023 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070024 if ((error = ndn_UdpTransport_connect(&transport_, (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort())))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070025 throw std::runtime_error(ndn_getErrorString(error));
26
27 // TODO: This belongs in the socket listener.
28 const unsigned int initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070029 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070030 ndn_BinaryXmlElementReader_initialize
Jeff Thompson10e34382013-08-22 13:34:46 -070031 (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070032
Jeff Thompsona4056972013-08-22 11:52:21 -070033 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070034 elementListener_ = &elementListener;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070035}
36
Jeff Thompson60eaae32013-07-30 15:46:19 -070037void UdpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070038{
39 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070040 if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070041 throw std::runtime_error(ndn_getErrorString(error));
42}
43
Jeff Thompson432c8be2013-08-09 16:16:08 -070044void UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070045{
Jeff Thompson432c8be2013-08-09 16:16:08 -070046 int receiveIsReady;
47 ndn_Error error;
48 if ((error = ndn_UdpTransport_receiveIsReady(&transport_, &receiveIsReady)))
49 throw std::runtime_error(ndn_getErrorString(error));
50 if (!receiveIsReady)
51 return;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070052
Jeff Thompson432c8be2013-08-09 16:16:08 -070053 unsigned char buffer[8000];
54 unsigned int nBytes;
55 if ((error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
56 throw std::runtime_error(ndn_getErrorString(error));
57
58 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070059}
60
Jeff Thompsona4056972013-08-22 11:52:21 -070061bool UdpTransport::getIsConnected()
62{
63 return isConnected_;
64}
65
Jeff Thompson57963882013-08-05 16:01:25 -070066void UdpTransport::close()
67{
68 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070069 if ((error = ndn_UdpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070070 throw std::runtime_error(ndn_getErrorString(error));
71}
72
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070073UdpTransport::~UdpTransport()
74{
75 if (elementReader_.partialData.array)
76 // Free the memory allocated in connect.
77 free(elementReader_.partialData.array);
78}
79
Jeff Thompsonbc53c522013-07-17 17:11:48 -070080}