blob: 248a6135a308aa7db2a8e39192968b5f851466cb [file] [log] [blame]
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#include <stdexcept>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07007#include "../node.hpp"
Jeff Thompson5e275b42013-07-16 19:10:11 -07008#include "../c/util/ndn_realloc.h"
Jeff Thompson53412192013-08-06 13:35:50 -07009#include "tcp-transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070010
11using namespace std;
12
13namespace ndn {
14
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070015void TcpTransport::connect(Node &node)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070016{
17 ndn_Error error;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070018 if ((error = ndn_TcpTransport_connect(&transport_, (char *)node.getHost(), node.getPort())))
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070019 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070020
21 // TODO: This belongs in the socket listener.
Jeff Thompson5e275b42013-07-16 19:10:11 -070022 const unsigned int initialLength = 1000;
Jeff Thompsonb002f902013-07-16 18:07:18 -070023 // 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 Thompson0cb7aee2013-07-16 16:18:06 -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 Thompsonfcf347d2013-07-15 11:30:44 -070030}
31
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070032void TcpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070033{
34 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070035 if ((error = ndn_TcpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036 throw std::runtime_error(ndn_getErrorString(error));
37}
38
Jeff Thompson432c8be2013-08-09 16:16:08 -070039void TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070040{
Jeff Thompson432c8be2013-08-09 16:16:08 -070041 int receiveIsReady;
42 ndn_Error error;
43 if ((error = ndn_TcpTransport_receiveIsReady(&transport_, &receiveIsReady)))
44 throw std::runtime_error(ndn_getErrorString(error));
45 if (!receiveIsReady)
46 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070047
Jeff Thompson432c8be2013-08-09 16:16:08 -070048 unsigned char buffer[8000];
49 unsigned int nBytes;
50 if ((error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
51 throw std::runtime_error(ndn_getErrorString(error));
52
53 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070054}
55
Jeff Thompsona4056972013-08-22 11:52:21 -070056bool TcpTransport::getIsConnected()
57{
58 return isConnected_;
59}
60
Jeff Thompson57963882013-08-05 16:01:25 -070061void TcpTransport::close()
62{
63 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070064 if ((error = ndn_TcpTransport_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 -070068TcpTransport::~TcpTransport()
69{
70 if (elementReader_.partialData.array)
71 // Free the memory allocated in connect.
72 free(elementReader_.partialData.array);
73}
74
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070075}