blob: bb4ab31a2343c76f99efed0537574aa8186e6739 [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08004 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev151a8552014-04-11 00:54:43 -07005 * BSD License, see COPYING for copyright and distribution information.
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08006 */
7
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008// correct way to include NDN-CPP headers
9// #include <ndn-cpp-dev/face.hpp>
10#include "face.hpp"
11
Alexander Afanasyev151a8552014-04-11 00:54:43 -070012// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
13namespace ndn {
14// Additional nested namespace could be used to prevent/limit name contentions
15namespace examples {
16
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080017void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070018onData(Face& face,
19 const Interest& interest, Data& data)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080020{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021 std::cout << "I: " << interest.toUri() << std::endl;
22 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080023}
24
25void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070026onTimeout(Face& face,
27 const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080028{
29 std::cout << "Timeout" << std::endl;
30}
31
Alexander Afanasyev151a8552014-04-11 00:54:43 -070032int
33main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080034{
35 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070036 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080037 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070038 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080039 i.setMustBeFresh(true);
40
Alexander Afanasyev151a8552014-04-11 00:54:43 -070041 Face face;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080042 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070043 bind(onData, boost::ref(face), _1, _2),
44 bind(onTimeout, boost::ref(face), _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080045
46 // processEvents will block until the requested data received or timeout occurs
47 face.processEvents();
48 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070049 catch(std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080050 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070051 return 1;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080052 }
53 return 0;
54}
Alexander Afanasyev151a8552014-04-11 00:54:43 -070055
56} // namespace examples
57} // namespace ndn
58
59int
60main(int argc, char** argv)
61{
62 return ndn::examples::main(argc, argv);
63}