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> |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 11 | #include <ndn-cpp-dev/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::func_lib; |
Jeff Thompson | b752300 | 2013-10-09 10:25:00 -0700 | [diff] [blame] | 16 | #if NDN_CPP_HAVE_STD_FUNCTION |
Jeff Thompson | 04aa711 | 2013-08-22 19:16:25 -0700 | [diff] [blame] | 17 | // In the std library, the placeholders are in a different namespace than boost. |
| 18 | using namespace func_lib::placeholders; |
| 19 | #endif |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 21 | class Counter |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 22 | { |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 23 | public: |
| 24 | Counter() { |
| 25 | callbackCount_ = 0; |
| 26 | } |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 27 | |
Jeff Thompson | 06d1780 | 2013-12-11 16:34:27 -0800 | [diff] [blame] | 28 | 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] | 29 | { |
| 30 | ++callbackCount_; |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 31 | cout << "Got data packet with name " << data->getName().toUri() << endl; |
| 32 | // cout << string(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size()) << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 33 | } |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | 06d1780 | 2013-12-11 16:34:27 -0800 | [diff] [blame] | 35 | void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest) |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 36 | { |
| 37 | ++callbackCount_; |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 38 | cout << "Time out for interest " << interest->getName().toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 39 | } |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 40 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 41 | int callbackCount_; |
| 42 | }; |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 44 | int main(int argc, char** argv) |
| 45 | { |
| 46 | try { |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 47 | Face face; |
| 48 | |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 49 | // Counter holds data used by the callbacks. |
| 50 | Counter counter; |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 51 | |
| 52 | Name name1("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"); |
Jeff Thompson | 72b58a4 | 2013-11-16 16:46:31 -0800 | [diff] [blame] | 53 | cout << "Express name " << name1.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 54 | // Use bind to pass the counter object to the callbacks. |
| 55 | face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 56 | |
| 57 | Name name2("/ndnx/ping"); |
Jeff Thompson | 72b58a4 | 2013-11-16 16:46:31 -0800 | [diff] [blame] | 58 | cout << "Express name " << name2.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 59 | face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 60 | |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 61 | Name name3("/test/timeout"); |
Jeff Thompson | 72b58a4 | 2013-11-16 16:46:31 -0800 | [diff] [blame] | 62 | cout << "Express name " << name3.toUri() << endl; |
Jeff Thompson | 0960b65 | 2013-08-22 19:10:18 -0700 | [diff] [blame] | 63 | 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] | 64 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 65 | // The main event loop. |
Alexander Afanasyev | 84c2bd4 | 2014-01-09 22:35:35 -0800 | [diff] [blame] | 66 | face.processEvents(); |
| 67 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 68 | } catch (std::exception& e) { |
Jeff Thompson | c98be1a | 2013-07-14 22:44:43 -0700 | [diff] [blame] | 69 | cout << "exception: " << e.what() << endl; |
| 70 | } |
| 71 | return 0; |
| 72 | } |