blob: 3ea187ed0f797c086f435744d72bca0d2699142f [file] [log] [blame]
Jeff Thompsonc98be1a2013-07-14 22:44:43 -07001/**
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 Thompson3a217062013-07-14 23:37:42 -070011#include <ndn-cpp/encoding/BinaryXMLStructureDecoder.hpp>
Jeff Thompson25f8e612013-07-15 11:30:21 -070012#include <ndn-cpp/transport/TcpTransport.hpp>
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070013
14using namespace std;
15using namespace ndn;
16int 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 Thompson25f8e612013-07-15 11:30:21 -070024 TcpTransport transport;
25 transport.connect((char *)"E.hub.ndn.ucla.edu", 9695);
26 transport.send(&encoding[0], encoding.size());
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070027
Jeff Thompson3a217062013-07-14 23:37:42 -070028 BinaryXMLStructureDecoder structureDecoder;
29 vector<unsigned char> element;
Jeff Thompson3a217062013-07-14 23:37:42 -070030 while (true) {
31 while (true) {
Jeff Thompson25f8e612013-07-15 11:30:21 -070032 unsigned char buffer[8000];
33 unsigned int nBytes = transport.receive(buffer, sizeof(buffer));
Jeff Thompson3a217062013-07-14 23:37:42 -070034 element.insert(element.end(), buffer, buffer + nBytes);
Jeff Thompson25f8e612013-07-15 11:30:21 -070035
Jeff Thompson3a217062013-07-14 23:37:42 -070036 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 Thompsonc98be1a2013-07-14 22:44:43 -070046 }
Jeff Thompson3a217062013-07-14 23:37:42 -070047
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070048 ContentObject contentObject;
Jeff Thompson3a217062013-07-14 23:37:42 -070049 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 Thompsonc98be1a2013-07-14 22:44:43 -070055 } catch (exception &e) {
56 cout << "exception: " << e.what() << endl;
57 }
58 return 0;
59}