blob: ea32c4d0ac53c9cce78a1c12799dfd6fac824464 [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 Thompson53430e02013-10-23 10:12:41 -07009#include <stdlib.h>
Jeff Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/node.hpp>
11#include "../c/transport/tcp-transport.h"
12#include "../c/encoding/binary-xml-element-reader.h"
Jeff Thompson5e275b42013-07-16 19:10:11 -070013#include "../c/util/ndn_realloc.h"
Jeff Thompson25b4e612013-10-10 16:03:24 -070014#include <ndn-cpp/transport/tcp-transport.hpp>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070015
16using namespace std;
17
18namespace ndn {
19
Jeff Thompson10e34382013-08-22 13:34:46 -070020TcpTransport::ConnectionInfo::~ConnectionInfo()
21{
22}
23
Jeff Thompson25b4e612013-10-10 16:03:24 -070024TcpTransport::TcpTransport()
25 : elementListener_(0), isConnected_(false), transport_(new struct ndn_TcpTransport), elementReader_(new struct ndn_BinaryXmlElementReader)
26{
27 ndn_TcpTransport_initialize(transport_.get());
28 elementReader_->partialData.array = 0;
29}
30
Jeff Thompson0050abe2013-09-17 12:50:25 -070031void
32TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070033{
Jeff Thompson1656e6a2013-08-29 18:01:48 -070034 const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo);
Jeff Thompson10e34382013-08-22 13:34:46 -070035
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070036 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070037 if ((error = ndn_TcpTransport_connect(transport_.get(), (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort())))
Jeff Thompson4affbf52013-10-18 14:36:46 -070038 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonb002f902013-07-16 18:07:18 -070039
40 // TODO: This belongs in the socket listener.
Jeff Thompson97223af2013-09-24 17:01:27 -070041 const size_t initialLength = 1000;
Jeff Thompson10e34382013-08-22 13:34:46 -070042 // Automatically cast elementReader_ to (struct ndn_ElementListener *)
Jeff Thompsond1427fb2013-08-29 17:20:32 -070043 ndn_BinaryXmlElementReader_initialize
Jeff Thompson25b4e612013-10-10 16:03:24 -070044 (elementReader_.get(), &elementListener, (uint8_t *)malloc(initialLength), initialLength, ndn_realloc);
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070045
Jeff Thompsona4056972013-08-22 11:52:21 -070046 isConnected_ = true;
Jeff Thompson10e34382013-08-22 13:34:46 -070047 elementListener_ = &elementListener;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070048}
49
Jeff Thompson0050abe2013-09-17 12:50:25 -070050void
Jeff Thompson97223af2013-09-24 17:01:27 -070051TcpTransport::send(const uint8_t *data, size_t dataLength)
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070052{
53 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070054 if ((error = ndn_TcpTransport_send(transport_.get(), (uint8_t *)data, dataLength)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070055 throw runtime_error(ndn_getErrorString(error));
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070056}
57
Jeff Thompson0050abe2013-09-17 12:50:25 -070058void
59TcpTransport::processEvents()
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070060{
Jeff Thompson432c8be2013-08-09 16:16:08 -070061 int receiveIsReady;
62 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070063 if ((error = ndn_TcpTransport_receiveIsReady(transport_.get(), &receiveIsReady)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070064 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070065 if (!receiveIsReady)
66 return;
Jeff Thompson7ed97c72013-07-16 17:56:41 -070067
Jeff Thompson10ad12a2013-09-24 16:19:11 -070068 uint8_t buffer[8000];
Jeff Thompson97223af2013-09-24 17:01:27 -070069 size_t nBytes;
Jeff Thompson25b4e612013-10-10 16:03:24 -070070 if ((error = ndn_TcpTransport_receive(transport_.get(), buffer, sizeof(buffer), &nBytes)))
Jeff Thompson4affbf52013-10-18 14:36:46 -070071 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson432c8be2013-08-09 16:16:08 -070072
Jeff Thompson25b4e612013-10-10 16:03:24 -070073 ndn_BinaryXmlElementReader_onReceivedData(elementReader_.get(), buffer, nBytes);
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070074}
75
Jeff Thompson0050abe2013-09-17 12:50:25 -070076bool
77TcpTransport::getIsConnected()
Jeff Thompsona4056972013-08-22 11:52:21 -070078{
79 return isConnected_;
80}
81
Jeff Thompson0050abe2013-09-17 12:50:25 -070082void
83TcpTransport::close()
Jeff Thompson57963882013-08-05 16:01:25 -070084{
85 ndn_Error error;
Jeff Thompson25b4e612013-10-10 16:03:24 -070086 if ((error = ndn_TcpTransport_close(transport_.get())))
Jeff Thompson4affbf52013-10-18 14:36:46 -070087 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson57963882013-08-05 16:01:25 -070088}
89
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070090TcpTransport::~TcpTransport()
91{
Jeff Thompson25b4e612013-10-10 16:03:24 -070092 if (elementReader_->partialData.array)
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070093 // Free the memory allocated in connect.
Jeff Thompson25b4e612013-10-10 16:03:24 -070094 free(elementReader_->partialData.array);
Jeff Thompsona00f4eb2013-08-12 12:36:48 -070095}
96
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070097}