Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include <cstdlib> |
| 8 | #include <sstream> |
| 9 | #include <iostream> |
Jeff Thompson | fe11005 | 2013-10-23 16:41:41 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -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 | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 15 | using namespace ndn::ptr_lib; |
| 16 | using namespace ndn::func_lib; |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 17 | #if NDN_CPP_HAVE_STD_FUNCTION |
Jeff Thompson | 04aa711 | 2013-08-22 19:16:25 -0700 | [diff] [blame] | 18 | // In the std library, the placeholders are in a different namespace than boost. |
| 19 | using namespace func_lib::placeholders; |
| 20 | #endif |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 21 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 22 | class Counter |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 23 | { |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 24 | public: |
| 25 | Counter() { |
| 26 | callbackCount_ = 0; |
| 27 | } |
| 28 | |
Jeff Thompson | 51755b3 | 2013-09-18 16:46:56 -0700 | [diff] [blame] | 29 | void onData(const shared_ptr<const Interest>& interest, const shared_ptr<Data>& data) |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 30 | { |
| 31 | ++callbackCount_; |
| 32 | cout << "Got data packet with name " << data->getName().to_uri() << endl; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 33 | for (size_t i = 0; i < data->getContent().size(); ++i) |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 34 | cout << (*data->getContent())[i]; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 35 | cout << endl; |
| 36 | } |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | 51755b3 | 2013-09-18 16:46:56 -0700 | [diff] [blame] | 38 | void onTimeout(const shared_ptr<const Interest>& interest) |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 39 | { |
| 40 | ++callbackCount_; |
| 41 | cout << "Time out for interest " << interest->getName().toUri() << endl; |
| 42 | } |
| 43 | |
| 44 | int callbackCount_; |
| 45 | }; |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 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 | a2953b8 | 2013-10-03 14:13:52 -0700 | [diff] [blame] | 50 | // Connect to port 9695 until the testbed hubs use NDNx. |
Jeff Thompson | fe11005 | 2013-10-23 16:41:41 -0700 | [diff] [blame] | 51 | Face face("C.hub.ndn.ucla.edu", 9695); |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 53 | // Counter holds data used by the callbacks. |
| 54 | Counter counter; |
| 55 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 56 | Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E"); |
| 57 | cout << "Express name " << name1.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 58 | // Use bind to pass the counter object to the callbacks. |
| 59 | face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 60 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 61 | Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D"); |
| 62 | cout << "Express name " << name2.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 63 | face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 64 | |
| 65 | Name name3("/test/timeout"); |
| 66 | cout << "Express name " << name3.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 67 | face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 69 | // The main event loop. |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 70 | while (counter.callbackCount_ < 3) { |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 71 | face.processEvents(); |
| 72 | // We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 73 | usleep(10000); |
| 74 | } |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 75 | } catch (std::exception& e) { |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 76 | cout << "exception: " << e.what() << endl; |
| 77 | } |
| 78 | return 0; |
| 79 | } |