blob: 4ae9e37d10e661a1392cb64dc20ccf6ad360acea [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 Thompsonb9e3c8e2013-08-02 11:42:51 -070015void UdpTransport::connect(Face &face)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070016{
17 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070018 if ((error = ndn_UdpTransport_connect(&transport_, (char *)face.getHost(), face.getPort())))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070019 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 Thompsonf0fea002013-07-30 17:22:42 -070024 ndn_BinaryXmlElementReader_init
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070025 (&elementReader_, &face, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070026
27 // TODO: Properly indicate connected status.
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070028 face_ = &face;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070029}
30
Jeff Thompson60eaae32013-07-30 15:46:19 -070031void UdpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070032{
33 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070034 if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070035 throw std::runtime_error(ndn_getErrorString(error));
36}
37
Jeff Thompson432c8be2013-08-09 16:16:08 -070038void UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070039{
Jeff Thompson432c8be2013-08-09 16:16:08 -070040 int receiveIsReady;
41 ndn_Error error;
42 if ((error = ndn_UdpTransport_receiveIsReady(&transport_, &receiveIsReady)))
43 throw std::runtime_error(ndn_getErrorString(error));
44 if (!receiveIsReady)
45 return;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070046
Jeff Thompson432c8be2013-08-09 16:16:08 -070047 unsigned char buffer[8000];
48 unsigned int nBytes;
49 if ((error = ndn_UdpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
50 throw std::runtime_error(ndn_getErrorString(error));
51
52 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070053}
54
Jeff Thompson57963882013-08-05 16:01:25 -070055void UdpTransport::close()
56{
57 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070058 if ((error = ndn_UdpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070059 throw std::runtime_error(ndn_getErrorString(error));
60}
61
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070062UdpTransport::~UdpTransport()
63{
64 if (elementReader_.partialData.array)
65 // Free the memory allocated in connect.
66 free(elementReader_.partialData.array);
67}
68
Jeff Thompsonbc53c522013-07-17 17:11:48 -070069}