blob: 2fbfeeb402660b8df794b849aea2cb2bae107607 [file] [log] [blame]
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#include <stdexcept>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07008#include "../node.hpp"
Jeff Thompson5e275b42013-07-16 19:10:11 -07009#include "../c/util/ndn_realloc.h"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "tcp-transport.hpp"
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070011
12using namespace std;
13
14namespace ndn {
15
Jeff Thompson10e34382013-08-22 13:34:46 -070016TcpTransport::ConnectionInfo::~ConnectionInfo()
17{
18}
19
Jeff Thompson1656e6a2013-08-29 18:01:48 -070020void TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070021{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070022 const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070023
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070024 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070025 if ((error = ndn_TcpTransport_connect(&transport_, (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070026 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070027
28 // TODO: This belongs in the socket listener.
Jeff Thompson5e275b42013-07-16 19:10:11 -070029 const unsigned int initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070030 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070031 ndn_BinaryXmlElementReader_initialize
Jeff Thompson10e34382013-08-22 13:34:46 -070032 (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070033
Jeff Thompsona4056972013-08-22 11:52:21 -070034 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070035 elementListener_ = &elementListener;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036}
37
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070038void TcpTransport::send(const unsigned char *data, unsigned int dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070039{
40 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070041 if ((error = ndn_TcpTransport_send(&transport_, (unsigned char *)data, dataLength)))
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070042 throw std::runtime_error(ndn_getErrorString(error));
43}
44
Jeff Thompson432c8be2013-08-09 16:16:08 -070045void TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070046{
Jeff Thompson432c8be2013-08-09 16:16:08 -070047 int receiveIsReady;
48 ndn_Error error;
49 if ((error = ndn_TcpTransport_receiveIsReady(&transport_, &receiveIsReady)))
50 throw std::runtime_error(ndn_getErrorString(error));
51 if (!receiveIsReady)
52 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070053
Jeff Thompson432c8be2013-08-09 16:16:08 -070054 unsigned char buffer[8000];
55 unsigned int nBytes;
56 if ((error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
57 throw std::runtime_error(ndn_getErrorString(error));
58
59 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070060}
61
Jeff Thompsona4056972013-08-22 11:52:21 -070062bool TcpTransport::getIsConnected()
63{
64 return isConnected_;
65}
66
Jeff Thompson57963882013-08-05 16:01:25 -070067void TcpTransport::close()
68{
69 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070070 if ((error = ndn_TcpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070071 throw std::runtime_error(ndn_getErrorString(error));
72}
73
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070074TcpTransport::~TcpTransport()
75{
76 if (elementReader_.partialData.array)
77 // Free the memory allocated in connect.
78 free(elementReader_.partialData.array);
79}
80
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070081}