blob: 757823d0c939faad116519f8245f345d9752a1fc [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 Thompson10e34382013-08-22 13:34:46 -070015TcpTransport::ConnectionInfo::~ConnectionInfo()
16{
17}
18
19void TcpTransport::connect(const Transport::ConnectionInfo &connectionInfo, ElementListener &elementListener)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070020{
Jeff Thompson10e34382013-08-22 13:34:46 -070021 const TcpTransport::ConnectionInfo &tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo &>(connectionInfo);
22
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070023 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070024 if ((error = ndn_TcpTransport_connect(&transport_, (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070025 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070026
27 // TODO: This belongs in the socket listener.
Jeff Thompson5e275b42013-07-16 19:10:11 -070028 const unsigned int initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070029 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsonf0fea002013-07-30 17:22:42 -070030 ndn_BinaryXmlElementReader_init
Jeff Thompson10e34382013-08-22 13:34:46 -070031 (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070032
Jeff Thompsona4056972013-08-22 11:52:21 -070033 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070034 elementListener_ = &elementListener;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070035}
36
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070037void TcpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070038{
39 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070040 if ((error = ndn_TcpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070041 throw std::runtime_error(ndn_getErrorString(error));
42}
43
Jeff Thompson432c8be2013-08-09 16:16:08 -070044void TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070045{
Jeff Thompson432c8be2013-08-09 16:16:08 -070046 int receiveIsReady;
47 ndn_Error error;
48 if ((error = ndn_TcpTransport_receiveIsReady(&transport_, &receiveIsReady)))
49 throw std::runtime_error(ndn_getErrorString(error));
50 if (!receiveIsReady)
51 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070052
Jeff Thompson432c8be2013-08-09 16:16:08 -070053 unsigned char buffer[8000];
54 unsigned int nBytes;
55 if ((error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
56 throw std::runtime_error(ndn_getErrorString(error));
57
58 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070059}
60
Jeff Thompsona4056972013-08-22 11:52:21 -070061bool TcpTransport::getIsConnected()
62{
63 return isConnected_;
64}
65
Jeff Thompson57963882013-08-05 16:01:25 -070066void TcpTransport::close()
67{
68 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070069 if ((error = ndn_TcpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070070 throw std::runtime_error(ndn_getErrorString(error));
71}
72
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070073TcpTransport::~TcpTransport()
74{
75 if (elementReader_.partialData.array)
76 // Free the memory allocated in connect.
77 free(elementReader_.partialData.array);
78}
79
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070080}