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 | 17882b4 | 2013-08-23 16:23:22 -0700 | [diff] [blame] | 9 | #include "../ndn-cpp/face.hpp" |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
| 12 | using namespace ndn; |
Jeff Thompson | 1bbc4e7 | 2013-07-16 16:30:55 -0700 | [diff] [blame] | 13 | using namespace ptr_lib; |
Jeff Thompson | 04aa711 | 2013-08-22 19:16:25 -0700 | [diff] [blame] | 14 | using namespace func_lib; |
| 15 | #if HAVE_STD_FUNCTION |
| 16 | // In the std library, the placeholders are in a different namespace than boost. |
| 17 | using namespace func_lib::placeholders; |
| 18 | #endif |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 19 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 20 | class Counter |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 21 | { |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 22 | public: |
| 23 | Counter() { |
| 24 | callbackCount_ = 0; |
| 25 | } |
| 26 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 27 | void onData(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& data) |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 28 | { |
| 29 | ++callbackCount_; |
| 30 | cout << "Got data packet with name " << data->getName().to_uri() << endl; |
| 31 | for (unsigned int i = 0; i < data->getContent().size(); ++i) |
| 32 | cout << data->getContent()[i]; |
| 33 | cout << endl; |
| 34 | } |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 35 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 36 | void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest) |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 37 | { |
| 38 | ++callbackCount_; |
| 39 | cout << "Time out for interest " << interest->getName().toUri() << endl; |
| 40 | } |
| 41 | |
| 42 | int callbackCount_; |
| 43 | }; |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 44 | |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 45 | int main(int argc, char** argv) |
| 46 | { |
| 47 | try { |
Jeff Thompson | dc461ab | 2013-08-19 18:15:59 -0700 | [diff] [blame] | 48 | Face face("E.hub.ndn.ucla.edu"); |
Jeff Thompson | 3a21706 | 2013-07-14 23:37:42 -0700 | [diff] [blame] | 49 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 50 | // Counter holds data used by the callbacks. |
| 51 | Counter counter; |
| 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; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 55 | // Use bind to pass the counter object to the callbacks. |
| 56 | 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] | 57 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 58 | Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D"); |
| 59 | cout << "Express name " << name2.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 60 | 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] | 61 | |
| 62 | Name name3("/test/timeout"); |
| 63 | cout << "Express name " << name3.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 64 | 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] | 65 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 66 | // The main event loop. |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 67 | while (counter.callbackCount_ < 3) { |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 68 | face.processEvents(); |
| 69 | // We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 70 | usleep(10000); |
| 71 | } |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame^] | 72 | } catch (std::exception& e) { |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 73 | cout << "exception: " << e.what() << endl; |
| 74 | } |
| 75 | return 0; |
| 76 | } |