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