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 | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 7 | #include "../NDN.hpp" |
| 8 | #include "../c/encoding/BinaryXMLElementReader.h" |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 9 | #include "TcpTransport.hpp" |
| 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 15 | void TcpTransport::connect(NDN &ndn) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 16 | { |
| 17 | ndn_Error error; |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 18 | if (error = ndn_TcpTransport_connect(&transport_, (char *)ndn.getHost(), ndn.getPort())) |
| 19 | throw std::runtime_error(ndn_getErrorString(error)); |
| 20 | |
| 21 | // TODO: Properly indicate connected status. |
| 22 | ndn_ = &ndn; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void TcpTransport::send(unsigned char *data, unsigned int dataLength) |
| 26 | { |
| 27 | ndn_Error error; |
| 28 | if (error = ndn_TcpTransport_send(&transport_, data, dataLength)) |
| 29 | throw std::runtime_error(ndn_getErrorString(error)); |
| 30 | } |
| 31 | |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 32 | void TcpTransport::tempReceive() |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 33 | { |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 34 | if (!ndn_) |
| 35 | // TODO: Properly check if connected. |
| 36 | return; |
| 37 | ndn_BinaryXMLElementReader elementReader; |
| 38 | // Automaticall cast ndn_ to (struct ndn_ElementListener *) |
| 39 | ndn_BinaryXMLElementReader_init(&elementReader, ndn_); |
| 40 | |
| 41 | unsigned char buffer[8000]; |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 42 | ndn_Error error; |
| 43 | unsigned int nBytes; |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 44 | if (error = ndn_TcpTransport_receive(&transport_, buffer, sizeof(buffer), &nBytes)) |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 45 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 0cb7aee | 2013-07-16 16:18:06 -0700 | [diff] [blame^] | 46 | ndn_BinaryXMLElementReader_onReceivedData(&elementReader, buffer, nBytes); |
Jeff Thompson | fcf347d | 2013-07-15 11:30:44 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } |