blob: d34ef2a62b7a345ba841044418681663d004baaa [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 Thompsonc98be1a2013-07-14 22:44:43 -070012#include <ndn-cpp/c/network/TcpTransport.h>
13
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
24 struct ndn_TcpTransport transport;
25 ndn_TcpTransport_init(&transport);
26 ndn_Error error;
27 if (error = ndn_TcpTransport_connect(&transport, (char *)"E.hub.ndn.ucla.edu", 9695))
28 return error;
29 if (error = ndn_TcpTransport_send(&transport, &encoding[0], encoding.size()))
30 return error;
31
Jeff Thompson3a217062013-07-14 23:37:42 -070032 BinaryXMLStructureDecoder structureDecoder;
33 vector<unsigned char> element;
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070034 unsigned char buffer[8000];
35 unsigned int nBytes;
Jeff Thompson3a217062013-07-14 23:37:42 -070036 while (true) {
37 while (true) {
38 if (error = ndn_TcpTransport_receive(&transport, buffer, sizeof(buffer), &nBytes))
39 return error;
40 element.insert(element.end(), buffer, buffer + nBytes);
41 if (structureDecoder.findElementEnd(&element[0], element.size()))
42 break;
43 }
44
45 if (element[0] == 0x04)
46 // Assume this is a ContentObject.
47 break;
48
49 // Erase this element and try again.
50 element.erase(element.begin(), element.begin() + structureDecoder.getOffset());
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070051 }
Jeff Thompson3a217062013-07-14 23:37:42 -070052
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070053 ContentObject contentObject;
Jeff Thompson3a217062013-07-14 23:37:42 -070054 contentObject.decode(&element[0], structureDecoder.getOffset());
55
56 cout << "Got content with name " << contentObject.getName().to_uri() << endl;
57 for (unsigned int i = 0; i < contentObject.getContent().size(); ++i)
58 cout << contentObject.getContent()[i];
59 cout << endl;
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070060 } catch (exception &e) {
61 cout << "exception: " << e.what() << endl;
62 }
63 return 0;
64}