Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include <stdexcept> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 8 | #include "../node.hpp" |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 9 | #include "../c/util/ndn_realloc.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "tcp-transport.hpp" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 16 | TcpTransport::ConnectionInfo::~ConnectionInfo() |
| 17 | { |
| 18 | } |
| 19 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 20 | void |
| 21 | TcpTransport::connect(const Transport::ConnectionInfo& connectionInfo, ElementListener& elementListener) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 22 | { |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 23 | const TcpTransport::ConnectionInfo& tcpConnectionInfo = dynamic_cast<const TcpTransport::ConnectionInfo&>(connectionInfo); |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 24 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 25 | ndn_Error error; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 26 | if ((error = ndn_TcpTransport_connect(&transport_, (char *)tcpConnectionInfo.getHost().c_str(), tcpConnectionInfo.getPort()))) |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 27 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 28 | |
| 29 | // TODO: This belongs in the socket listener. |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 30 | const unsigned int initialLength = 1000; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 31 | // Automatically cast elementReader_ to (struct ndn_ElementListener *) |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 32 | ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 33 | (&elementReader_, &elementListener, (unsigned char *)malloc(initialLength), initialLength, ndn_realloc); |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 35 | isConnected_ = true; |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 36 | elementListener_ = &elementListener; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 39 | void |
| 40 | TcpTransport::send(const unsigned char *data, unsigned int dataLength) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 41 | { |
| 42 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 43 | if ((error = ndn_TcpTransport_send(&transport_, (unsigned char *)data, dataLength))) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 44 | throw std::runtime_error(ndn_getErrorString(error)); |
| 45 | } |
| 46 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 47 | void |
| 48 | TcpTransport::processEvents() |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 49 | { |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 50 | 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 Thompson | 7ed97c7 | 2013-07-16 17:56:41 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 57 | unsigned char buffer[8000]; |
| 58 | unsigned int nBytes; |
| 59 | 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 Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 65 | bool |
| 66 | TcpTransport::getIsConnected() |
Jeff Thompson | a405697 | 2013-08-22 11:52:21 -0700 | [diff] [blame] | 67 | { |
| 68 | return isConnected_; |
| 69 | } |
| 70 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 71 | void |
| 72 | TcpTransport::close() |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 73 | { |
| 74 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 75 | if ((error = ndn_TcpTransport_close(&transport_))) |
Jeff Thompson | 5796388 | 2013-08-05 16:01:25 -0700 | [diff] [blame] | 76 | throw std::runtime_error(ndn_getErrorString(error)); |
| 77 | } |
| 78 | |
Jeff Thompson | a00f4eb | 2013-08-12 12:36:48 -0700 | [diff] [blame] | 79 | TcpTransport::~TcpTransport() |
| 80 | { |
| 81 | if (elementReader_.partialData.array) |
| 82 | // Free the memory allocated in connect. |
| 83 | free(elementReader_.partialData.array); |
| 84 | } |
| 85 | |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 86 | } |