Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include <stdexcept> |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 8 | #include "../face.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 9 | #include "../c/util/ndn_realloc.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "udp-transport.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 16 | UdpTransport::ConnectionInfo::~ConnectionInfo() |
| 17 | { |
| 18 | } |
| 19 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 20 | void UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 21 | { |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 22 | const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo); |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 23 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 24 | ndn_Error error; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 25 | if ((error = ndn_UdpTransport_connect(&transport_, (char *)udpConnectionInfo.getHost().c_str(), udpConnectionInfo.getPort()))) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 26 | throw std::runtime_error(ndn_getErrorString(error)); |
| 27 | |
| 28 | // TODO: This belongs in the socket listener. |
| 29 | const unsigned int initialLength = 1000; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 30 | // Automatically cast elementReader_ to (struct ndn_ElementListener *) |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 31 | ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 32 | (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 34 | isConnected_ = true; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 35 | elementListener_ = &elementListener; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Jeff Thompson | 60eaae3 | 2013-07-30 15:46:19 -0700 | [diff] [blame] | 38 | void UdpTransport::send(const unsigned char *data, unsigned int dataLength) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 39 | { |
| 40 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 41 | if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength))) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 42 | throw std::runtime_error(ndn_getErrorString(error)); |
| 43 | } |
| 44 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 45 | void UdpTransport::processEvents() |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 46 | { |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 47 | 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 54 | 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 62 | bool UdpTransport::getIsConnected() |
| 63 | { |
| 64 | return isConnected_; |
| 65 | } |
| 66 | |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 67 | void UdpTransport::close() |
| 68 | { |
| 69 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 70 | if ((error = ndn_UdpTransport_close(&transport_))) |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 71 | throw std::runtime_error(ndn_getErrorString(error)); |
| 72 | } |
| 73 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 74 | UdpTransport::~UdpTransport() |
| 75 | { |
| 76 | if (elementReader_.partialData.array) |
| 77 | // Free the memory allocated in connect. |
| 78 | free(elementReader_.partialData.array); |
| 79 | } |
| 80 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 81 | } |