blob: b1d4f0dcd178b89f2ccabff7c7f8e654c5aa33fa [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/node.hpp>
10#include "../c/transport/tcp-transport.h"
11#include "../c/encoding/binary-xml-element-reader.h"
Jeff Thompson5e275b42013-07-16 19:10:11 -070012#include "../c/util/ndn_realloc.h"
Jeff Thompson25b4e612013-10-10 16:03:24 -070013#include <ndn-cpp/transport/tcp-transport.hpp>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070014
15using namespace std;
16
17namespace ndn {
18
Jeff Thompson10e34382013-08-22 13:34:46 -070019TcpTransport::ConnectionInfo::~ConnectionInfo()
20{
21}
22
Jeff Thompson25b4e612013-10-10 16:03:24 -070023TcpTransport::TcpTransport()
24 : elementListener_(0), isConnected_(false), transport_(new struct ndn_TcpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
25{
26 ndn_TcpTransport_initialize(transport_.get());
27 elementReader_->partialData.array = 0;
28}
29
Jeff Thompson0050abe2013-09-17 12:50:25 -070030void
31TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070032{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070033 const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070034
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070035 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070036 if ((error = ndn_TcpTransport_connect(transport_.get(), (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
Jeff Thompson4affbf52013-10-18 14:36:46 -070037 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070038
39 // TODO: This belongs in the socket listener.
Jeff Thompson97223af2013-09-24 17:01:27 -070040 const size_t initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070041 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070042 ndn_BinaryXmlElementReader_initialize
Jeff Thompson25b4e612013-10-10 16:03:24 -070043 (elementReader_.get(), &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070044
Jeff Thompsona4056972013-08-22 11:52:21 -070045 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070046 elementListener_ = &elementListener;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070047}
48
Jeff Thompson0050abe2013-09-17 12:50:25 -070049void
Jeff Thompson97223af2013-09-24 17:01:27 -070050TcpTransport::send(const uint8_t *data, size_t dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070051{
52 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070053 if ((error = ndn_TcpTransport_send(transport_.get(), (uint8_t *)data, dataLength)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070054 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070055}
56
Jeff Thompson0050abe2013-09-17 12:50:25 -070057void
58TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070059{
Jeff Thompson432c8be2013-08-09 16:16:08 -070060 int receiveIsReady;
61 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070062 if ((error = ndn_TcpTransport_receiveIsReady(transport_.get(), &receiveIsReady)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070063 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070064 if (!receiveIsReady)
65 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070066
Jeff Thompson10ad12a2013-09-24 16:19:11 -070067 uint8_t buffer[8000];
Jeff Thompson97223af2013-09-24 17:01:27 -070068 size_t nBytes;
Jeff Thompson25b4e612013-10-10 16:03:24 -070069 if ((error = ndn_TcpTransport_receive(transport_.get(), buffer, sizeof(buffer), &nBytes)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070070 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070071
Jeff Thompson25b4e612013-10-10 16:03:24 -070072 ndn_BinaryXmlElementReader_onReceivedData(elementReader_.get(), buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070073}
74
Jeff Thompson0050abe2013-09-17 12:50:25 -070075bool
76TcpTransport::getIsConnected()
Jeff Thompsona4056972013-08-22 11:52:21 -070077{
78 return isConnected_;
79}
80
Jeff Thompson0050abe2013-09-17 12:50:25 -070081void
82TcpTransport::close()
Jeff Thompson57963882013-08-05 16:01:25 -070083{
84 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070085 if ((error = ndn_TcpTransport_close(transport_.get())))
Jeff Thompson4affbf52013-10-18 14:36:46 -070086 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson57963882013-08-05 16:01:25 -070087}
88
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070089TcpTransport::~TcpTransport()
90{
Jeff Thompson25b4e612013-10-10 16:03:24 -070091 if (elementReader_->partialData.array)
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070092 // Free the memory allocated in connect.
Jeff Thompson25b4e612013-10-10 16:03:24 -070093 free(elementReader_->partialData.array);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070094}
95
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070096}