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> |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/interest.hpp> |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 10 | #include <ndn-cpp/data.hpp> |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 11 | #include <ndn-cpp/face.hpp> |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
| 14 | using namespace ndn; |
Jeff Thompson | 1bbc4e7 | 2013-07-16 16:30:55 -0700 | [diff] [blame] | 15 | using namespace ptr_lib; |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 16 | |
| 17 | class MyClosure : public Closure { |
| 18 | public: |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 19 | MyClosure() |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 20 | : gotCallbackCount_(0) |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 21 | { |
| 22 | } |
| 23 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 24 | virtual UpcallResult upcall(UpcallKind kind, const UpcallInfo &upcallInfo) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 25 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 26 | if (kind == UPCALL_DATA || kind == UPCALL_DATA_UNVERIFIED) { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 27 | ++gotCallbackCount_; |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 28 | 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 Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 31 | cout << endl; |
| 32 | |
| 33 | return CLOSURE_RESULT_OK; |
| 34 | } |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 35 | 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 Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 40 | else |
| 41 | return CLOSURE_RESULT_OK; |
| 42 | } |
Jeff Thompson | b002f90 | 2013-07-16 18:07:18 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 44 | int gotCallbackCount_; |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 47 | int main(int argc, char** argv) |
| 48 | { |
| 49 | try { |
Jeff Thompson | 2a4724b | 2013-08-07 17:13:34 -0700 | [diff] [blame] | 50 | MyClosure closure; |
Jeff Thompson | dc461ab | 2013-08-19 18:15:59 -0700 | [diff] [blame] | 51 | Face face("E.hub.ndn.ucla.edu"); |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 53 | 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 56 | 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 60 | // The main event loop. |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 61 | while (closure.gotCallbackCount_ < 2) { |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 62 | 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 Thompson | 1bbc4e7 | 2013-07-16 16:30:55 -0700 | [diff] [blame] | 66 | } catch (std::exception &e) { |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 67 | cout << "exception: " << e.what() << endl; |
| 68 | } |
| 69 | return 0; |
| 70 | } |