Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 4 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 5 | * BSD License, see COPYING for copyright and distribution information. |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 8 | // correct way to include NDN-CPP headers |
| 9 | // #include <ndn-cpp-dev/face.hpp> |
| 10 | #include "face.hpp" |
| 11 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 12 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 13 | namespace ndn { |
| 14 | // Additional nested namespace could be used to prevent/limit name contentions |
| 15 | namespace examples { |
| 16 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 17 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 18 | onData(Face& face, |
| 19 | const Interest& interest, Data& data) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 20 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 21 | std::cout << "I: " << interest.toUri() << std::endl; |
| 22 | std::cout << "D: " << data.getName().toUri() << std::endl; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 26 | onTimeout(Face& face, |
| 27 | const Interest& interest) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 28 | { |
| 29 | std::cout << "Timeout" << std::endl; |
| 30 | } |
| 31 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 32 | int |
| 33 | main(int argc, char** argv) |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 34 | { |
| 35 | try { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 36 | Interest i(Name("/localhost/testApp/randomData")); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 37 | i.setScope(1); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 38 | i.setInterestLifetime(time::milliseconds(1000)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 39 | i.setMustBeFresh(true); |
| 40 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 41 | Face face; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 42 | face.expressInterest(i, |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 43 | bind(onData, boost::ref(face), _1, _2), |
| 44 | bind(onTimeout, boost::ref(face), _1)); |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 45 | |
| 46 | // processEvents will block until the requested data received or timeout occurs |
| 47 | face.processEvents(); |
| 48 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 49 | catch(std::exception& e) { |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 50 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 51 | return 1; |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 52 | } |
| 53 | return 0; |
| 54 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame^] | 55 | |
| 56 | } // namespace examples |
| 57 | } // namespace ndn |
| 58 | |
| 59 | int |
| 60 | main(int argc, char** argv) |
| 61 | { |
| 62 | return ndn::examples::main(argc, argv); |
| 63 | } |