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 | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 20 | void |
| 21 | UdpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 22 | { |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 23 | const UdpTransport::ConnectionInfo& udpConnectionInfo = dynamic_cast<const UdpTransport::ConnectionInfo&>(connectionInfo); |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 24 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 25 | ndn_Error error; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 26 | 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] | 27 | throw std::runtime_error(ndn_getErrorString(error)); |
| 28 | |
| 29 | // TODO: This belongs in the socket listener. |
| 30 | const unsigned int initialLength = 1000; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 31 | // Automatically cast elementReader_ to (struct ndn_ElementListener *) |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 32 | ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 33 | (&elementReader_, &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 35 | isConnected_ = true; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 36 | elementListener_ = &elementListener; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 39 | void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 40 | UdpTransport::send(const uint8_t *data, unsigned int dataLength) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 41 | { |
| 42 | ndn_Error error; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 43 | if ((error = ndn_UdpTransport_send(&transport_, (uint8_t *)data, dataLength))) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 44 | throw std::runtime_error(ndn_getErrorString(error)); |
| 45 | } |
| 46 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 47 | void |
| 48 | UdpTransport::processEvents() |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 49 | { |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 50 | 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame^] | 57 | uint8_t buffer[8000]; |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 58 | 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 Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 65 | bool |
| 66 | UdpTransport::getIsConnected() |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 67 | { |
| 68 | return isConnected_; |
| 69 | } |
| 70 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 71 | void |
| 72 | UdpTransport::close() |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 73 | { |
| 74 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 75 | if ((error = ndn_UdpTransport_close(&transport_))) |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 76 | throw std::runtime_error(ndn_getErrorString(error)); |
| 77 | } |
| 78 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 79 | UdpTransport::~UdpTransport() |
| 80 | { |
| 81 | if (elementReader_.partialData.array) |
| 82 | // Free the memory allocated in connect. |
| 83 | free(elementReader_.partialData.array); |
| 84 | } |
| 85 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 86 | } |