Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <stdexcept> |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 7 | #include "../face.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 8 | #include "../c/util/ndn_realloc.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 9 | #include "udp-transport.hpp" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 15 | void UdpTransport::connect(Node &node) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 16 | { |
| 17 | ndn_Error error; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 18 | if ((error = ndn_UdpTransport_connect(&transport_, (char *)node.getHost(), node.getPort()))) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 19 | throw std::runtime_error(ndn_getErrorString(error)); |
| 20 | |
| 21 | // TODO: This belongs in the socket listener. |
| 22 | const unsigned int initialLength = 1000; |
| 23 | // Automatically cast ndn_ to (struct ndn_ElementListener *) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 24 | ndn_BinaryXmlElementReader_init |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 25 | (&elementReader_, &node, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 26 | |
| 27 | // TODO: Properly indicate connected status. |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame^] | 28 | isConnected_ = true; |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 29 | node_ = &node; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Jeff Thompson | 60eaae3 | 2013-07-30 15:46:19 -0700 | [diff] [blame] | 32 | void UdpTransport::send(const unsigned char *data, unsigned int dataLength) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 33 | { |
| 34 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 35 | if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength))) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 36 | throw std::runtime_error(ndn_getErrorString(error)); |
| 37 | } |
| 38 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 39 | void UdpTransport::processEvents() |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 40 | { |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 41 | int receiveIsReady; |
| 42 | ndn_Error error; |
| 43 | if ((error = ndn_UdpTransport_receiveIsReady(&transport_, &receiveIsReady))) |
| 44 | throw std::runtime_error(ndn_getErrorString(error)); |
| 45 | if (!receiveIsReady) |
| 46 | return; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 48 | unsigned char buffer[8000]; |
| 49 | unsigned int nBytes; |
| 50 | if ((error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes))) |
| 51 | throw std::runtime_error(ndn_getErrorString(error)); |
| 52 | |
| 53 | ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame^] | 56 | bool UdpTransport::getIsConnected() |
| 57 | { |
| 58 | return isConnected_; |
| 59 | } |
| 60 | |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 61 | void UdpTransport::close() |
| 62 | { |
| 63 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 64 | if ((error = ndn_UdpTransport_close(&transport_))) |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 65 | throw std::runtime_error(ndn_getErrorString(error)); |
| 66 | } |
| 67 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 68 | UdpTransport::~UdpTransport() |
| 69 | { |
| 70 | if (elementReader_.partialData.array) |
| 71 | // Free the memory allocated in connect. |
| 72 | free(elementReader_.partialData.array); |
| 73 | } |
| 74 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 75 | } |