blob: 692ed36ab60f0bde1422187505c16853ccbabe94 [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>
Jeff Thompson53412192013-08-06 13:35:50 -07009#include <ndn-cpp/interest.hpp>
Jeff Thompson56ec9e22013-08-02 11:34:07 -070010#include <ndn-cpp/data.hpp>
Jeff Thompsonb9e3c8e2013-08-02 11:42:51 -070011#include <ndn-cpp/face.hpp>
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070012
13using namespace std;
14using namespace ndn;
Jeff Thompson1bbc4e72013-07-16 16:30:55 -070015using namespace ptr_lib;
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070016
17class MyClosure : public Closure {
18public:
Jeff Thompsonb002f902013-07-16 18:07:18 -070019 MyClosure()
Jeff Thompson48917f02013-08-21 17:12:45 -070020 : gotCallbackCount_(0)
Jeff Thompsonb002f902013-07-16 18:07:18 -070021 {
22 }
23
Jeff Thompson48917f02013-08-21 17:12:45 -070024 virtual UpcallResult upcall(UpcallKind kind, const UpcallInfo &upcallInfo)
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070025 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -070026 if (kind == UPCALL_DATA || kind == UPCALL_DATA_UNVERIFIED) {
Jeff Thompson48917f02013-08-21 17:12:45 -070027 ++gotCallbackCount_;
Jeff Thompson56ec9e22013-08-02 11:34:07 -070028 cout << "Got data packet with name " << upcallInfo.getData()->getName().to_uri() << endl;
29 for (unsigned int i = 0; i < upcallInfo.getData()->getContent().size(); ++i)
30 cout << upcallInfo.getData()->getContent()[i];
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070031 cout << endl;
32
33 return CLOSURE_RESULT_OK;
34 }
Jeff Thompson48917f02013-08-21 17:12:45 -070035 else if (kind == UPCALL_INTEREST_TIMED_OUT) {
36 ++gotCallbackCount_;
37 cout << "Time out for interest " << upcallInfo.getInterest()->getName().toUri() << endl;
38 return CLOSURE_RESULT_OK;
39 }
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070040 else
41 return CLOSURE_RESULT_OK;
42 }
Jeff Thompsonb002f902013-07-16 18:07:18 -070043
Jeff Thompson48917f02013-08-21 17:12:45 -070044 int gotCallbackCount_;
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070045};
46
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070047int main(int argc, char** argv)
48{
49 try {
Jeff Thompson2a4724b2013-08-07 17:13:34 -070050 MyClosure closure;
Jeff Thompsondc461ab2013-08-19 18:15:59 -070051 Face face("E.hub.ndn.ucla.edu");
Jeff Thompson3a217062013-07-14 23:37:42 -070052
Jeff Thompson557b81e2013-08-21 15:13:51 -070053 Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
54 cout << "Express name " << name1.toUri() << endl;
55 face.expressInterest(name1, &closure);
Jeff Thompson557b81e2013-08-21 15:13:51 -070056 Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
57 cout << "Express name " << name2.toUri() << endl;
58 face.expressInterest(name2, &closure);
Jeff Thompson557b81e2013-08-21 15:13:51 -070059
Jeff Thompson432c8be2013-08-09 16:16:08 -070060 // The main event loop.
Jeff Thompson48917f02013-08-21 17:12:45 -070061 while (closure.gotCallbackCount_ < 2) {
Jeff Thompsonc7e07442013-08-19 15:25:43 -070062 face.processEvents();
63 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
64 usleep(10000);
65 }
Jeff Thompson1bbc4e72013-07-16 16:30:55 -070066 } catch (std::exception &e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070067 cout << "exception: " << e.what() << endl;
68 }
69 return 0;
70}