blob: 42551ad16c058462151e375e4ad81faee0389d06 [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 Thompsonbf50a1a2013-08-20 18:01:01 -070015void UdpTransport::connect(Node &node)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070016{
17 ndn_Error error;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070018 if ((error = ndn_UdpTransport_connect(&transport_, (char *)node.getHost(), node.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 Thompsonbf50a1a2013-08-20 18:01:01 -070025 (&elementReader_, &node, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompsonbc53c522013-07-17 17:11:48 -070026
27 // TODO: Properly indicate connected status.
Jeff Thompsona4056972013-08-22 11:52:21 -070028 isConnected_ = true;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070029 node_ = &node;
Jeff Thompsonbc53c522013-07-17 17:11:48 -070030}
31
Jeff Thompson60eaae32013-07-30 15:46:19 -070032void UdpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonbc53c522013-07-17 17:11:48 -070033{
34 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070035 if ((error = ndn_UdpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonbc53c522013-07-17 17:11:48 -070036 throw std::runtime_error(ndn_getErrorString(error));
37}
38
Jeff Thompson432c8be2013-08-09 16:16:08 -070039void UdpTransport::processEvents()
Jeff Thompsonbc53c522013-07-17 17:11:48 -070040{
Jeff Thompson432c8be2013-08-09 16:16:08 -070041 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 Thompsonbc53c522013-07-17 17:11:48 -070047
Jeff Thompson432c8be2013-08-09 16:16:08 -070048 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 Thompsonbc53c522013-07-17 17:11:48 -070054}
55
Jeff Thompsona4056972013-08-22 11:52:21 -070056bool UdpTransport::getIsConnected()
57{
58 return isConnected_;
59}
60
Jeff Thompson57963882013-08-05 16:01:25 -070061void UdpTransport::close()
62{
63 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070064 if ((error = ndn_UdpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070065 throw std::runtime_error(ndn_getErrorString(error));
66}
67
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070068UdpTransport::~UdpTransport()
69{
70 if (elementReader_.partialData.array)
71 // Free the memory allocated in connect.
72 free(elementReader_.partialData.array);
73}
74
Jeff Thompsonbc53c522013-07-17 17:11:48 -070075}