blob: 9a712fb75930bfcdddb4f09779fe12b83401e977 [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 Thompson0050abe2013-09-17 12:50:25 -070020void
21TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070022{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070023 const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070024
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070025 ndn_Error error;
Jeff Thompson10e34382013-08-22 13:34:46 -070026 if ((error = ndn_TcpTransport_connect(&transport_, (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070027 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070028
29 // TODO: This belongs in the socket listener.
Jeff Thompson97223af2013-09-24 17:01:27 -070030 const size_t initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070031 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070032 ndn_BinaryXmlElementReader_initialize
Jeff Thompson10ad12a2013-09-24 16:19:11 -070033 (&elementReader_, &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070034
Jeff Thompsona4056972013-08-22 11:52:21 -070035 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070036 elementListener_ = &elementListener;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070037}
38
Jeff Thompson0050abe2013-09-17 12:50:25 -070039void
Jeff Thompson97223af2013-09-24 17:01:27 -070040TcpTransport::send(const uint8_t *data, size_t dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070041{
42 ndn_Error error;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070043 if ((error = ndn_TcpTransport_send(&transport_, (uint8_t *)data, dataLength)))
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070044 throw std::runtime_error(ndn_getErrorString(error));
45}
46
Jeff Thompson0050abe2013-09-17 12:50:25 -070047void
48TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070049{
Jeff Thompson432c8be2013-08-09 16:16:08 -070050 int receiveIsReady;
51 ndn_Error error;
52 if ((error = ndn_TcpTransport_receiveIsReady(&transport_, &receiveIsReady)))
53 throw std::runtime_error(ndn_getErrorString(error));
54 if (!receiveIsReady)
55 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070056
Jeff Thompson10ad12a2013-09-24 16:19:11 -070057 uint8_t buffer[8000];
Jeff Thompson97223af2013-09-24 17:01:27 -070058 size_t nBytes;
Jeff Thompson432c8be2013-08-09 16:16:08 -070059 if ((error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)))
60 throw std::runtime_error(ndn_getErrorString(error));
61
62 ndn_BinaryXmlElementReader_onReceivedData(&elementReader_, buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070063}
64
Jeff Thompson0050abe2013-09-17 12:50:25 -070065bool
66TcpTransport::getIsConnected()
Jeff Thompsona4056972013-08-22 11:52:21 -070067{
68 return isConnected_;
69}
70
Jeff Thompson0050abe2013-09-17 12:50:25 -070071void
72TcpTransport::close()
Jeff Thompson57963882013-08-05 16:01:25 -070073{
74 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070075 if ((error = ndn_TcpTransport_close(&transport_)))
Jeff Thompson57963882013-08-05 16:01:25 -070076 throw std::runtime_error(ndn_getErrorString(error));
77}
78
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070079TcpTransport::~TcpTransport()
80{
81 if (elementReader_.partialData.array)
82 // Free the memory allocated in connect.
83 free(elementReader_.partialData.array);
84}
85
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070086}