Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <sstream> |
| 8 | #include <iostream> |
| 9 | #include <ndn-cpp/Interest.hpp> |
| 10 | #include <ndn-cpp/ContentObject.hpp> |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 11 | #include <ndn-cpp/encoding/BinaryXMLStructureDecoder.hpp> |
Jeff Thompson | 25f8e61 | 2013-07-15 11:30:21 -0700 | [diff] [blame] | 12 | #include <ndn-cpp/transport/TcpTransport.hpp> |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | using namespace ndn; |
| 16 | int main(int argc, char** argv) |
| 17 | { |
| 18 | try { |
| 19 | Interest interest; |
| 20 | interest.getName() = Name("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E"); |
| 21 | vector<unsigned char> encoding; |
| 22 | interest.encode(encoding); |
| 23 | |
Jeff Thompson | 25f8e61 | 2013-07-15 11:30:21 -0700 | [diff] [blame] | 24 | TcpTransport transport; |
| 25 | transport.connect((char *)"E.hub.ndn.ucla.edu", 9695); |
| 26 | transport.send(&encoding[0], encoding.size()); |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 27 | |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 28 | BinaryXMLStructureDecoder structureDecoder; |
| 29 | vector<unsigned char> element; |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 30 | while (true) { |
| 31 | while (true) { |
Jeff Thompson | 25f8e61 | 2013-07-15 11:30:21 -0700 | [diff] [blame] | 32 | unsigned char buffer[8000]; |
| 33 | unsigned int nBytes = transport.receive(buffer, sizeof(buffer)); |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 34 | element.insert(element.end(), buffer, buffer + nBytes); |
Jeff Thompson | 25f8e61 | 2013-07-15 11:30:21 -0700 | [diff] [blame] | 35 | |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 36 | if (structureDecoder.findElementEnd(&element[0], element.size())) |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | if (element[0] == 0x04) |
| 41 | // Assume this is a ContentObject. |
| 42 | break; |
| 43 | |
| 44 | // Erase this element and try again. |
| 45 | element.erase(element.begin(), element.begin() + structureDecoder.getOffset()); |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 46 | } |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 48 | ContentObject contentObject; |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 49 | contentObject.decode(&element[0], structureDecoder.getOffset()); |
| 50 | |
| 51 | cout << "Got content with name " << contentObject.getName().to_uri() << endl; |
| 52 | for (unsigned int i = 0; i < contentObject.getContent().size(); ++i) |
| 53 | cout << contentObject.getContent()[i]; |
| 54 | cout << endl; |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 55 | } catch (exception &e) { |
| 56 | cout << "exception: " << e.what() << endl; |
| 57 | } |
| 58 | return 0; |
| 59 | } |