Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #include <stdexcept> |
Jeff Thompson | 53430e0 | 2013-10-23 10:12:41 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 10 | #include <ndn-cpp/face.hpp> |
| 11 | #include "../c/transport/udp-transport.h" |
| 12 | #include "../c/encoding/binary-xml-element-reader.h" |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 13 | #include "../c/util/ndn_realloc.h" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 14 | #include <ndn-cpp/transport/udp-transport.hpp> |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame^] | 20 | UdpTransport::UdpTransport(const char *host, unsigned short port/* = 6363*/) |
| 21 | : host_(host), port_(port) |
| 22 | , isConnected_(false), transport_(new struct ndn_UdpTransport), elementReader_(new struct ndn_BinaryXmlElementReader) |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 23 | { |
| 24 | ndn_UdpTransport_initialize(transport_.get()); |
| 25 | elementReader_->partialData.array = 0; |
| 26 | } |
| 27 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 28 | void |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame^] | 29 | UdpTransport::connect(ElementListener& elementListener) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 30 | { |
| 31 | ndn_Error error; |
Alexander Afanasyev | 0b688dc | 2013-12-18 16:43:37 -0800 | [diff] [blame^] | 32 | if ((error = ndn_UdpTransport_connect(transport_.get(), (char *)host_.c_str(), port_))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 33 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 34 | |
| 35 | // TODO: This belongs in the socket listener. |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 36 | const size_t initialLength = 1000; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 37 | // Automatically cast elementReader_ to (struct ndn_ElementListener *) |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 38 | ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 39 | (elementReader_.get(), &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 41 | isConnected_ = true; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 44 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 45 | UdpTransport::send(const uint8_t *data, size_t dataLength) |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 46 | { |
| 47 | ndn_Error error; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 48 | if ((error = ndn_UdpTransport_send(transport_.get(), (uint8_t *)data, dataLength))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 49 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 52 | void |
| 53 | UdpTransport::processEvents() |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 55 | int receiveIsReady; |
| 56 | ndn_Error error; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 57 | if ((error = ndn_UdpTransport_receiveIsReady(transport_.get(), &receiveIsReady))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 58 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 59 | if (!receiveIsReady) |
| 60 | return; |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 62 | uint8_t buffer[8000]; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 63 | size_t nBytes; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 64 | if ((error = ndn_UdpTransport_receive(transport_.get(), buffer, sizeof(buffer), &nBytes))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 65 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 66 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 67 | ndn_BinaryXmlElementReader_onReceivedData(elementReader_.get(), buffer, nBytes); |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 70 | bool |
| 71 | UdpTransport::getIsConnected() |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 72 | { |
| 73 | return isConnected_; |
| 74 | } |
| 75 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 76 | void |
| 77 | UdpTransport::close() |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 78 | { |
| 79 | ndn_Error error; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 80 | if ((error = ndn_UdpTransport_close(transport_.get()))) |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 81 | throw runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 84 | UdpTransport::~UdpTransport() |
| 85 | { |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 86 | if (elementReader_->partialData.array) |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 87 | // Free the memory allocated in connect. |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 88 | free(elementReader_->partialData.array); |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Thompson | bc53c52 | 2013-07-17 17:11:48 -0700 | [diff] [blame] | 91 | } |