blob: 8639db06cbcf56ca50ab073df18aae6b7466d02a [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080013 */
14
Alexander Afanasyev766cea72014-04-24 19:16:42 -070015// correct way to include ndn-cxx headers
16// #include <ndn-cxx/face.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080017#include "face.hpp"
18
Alexander Afanasyev151a8552014-04-11 00:54:43 -070019// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
20namespace ndn {
21// Additional nested namespace could be used to prevent/limit name contentions
22namespace examples {
23
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080024void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070025onData(Face& face,
26 const Interest& interest, Data& data)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080027{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028 std::cout << "I: " << interest.toUri() << std::endl;
29 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080030}
31
32void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070033onTimeout(Face& face,
34 const Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080035{
36 std::cout << "Timeout" << std::endl;
37}
38
Alexander Afanasyev151a8552014-04-11 00:54:43 -070039int
40main(int argc, char** argv)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080041{
42 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070043 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080044 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070045 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080046 i.setMustBeFresh(true);
47
Alexander Afanasyev151a8552014-04-11 00:54:43 -070048 Face face;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080049 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070050 bind(onData, boost::ref(face), _1, _2),
51 bind(onTimeout, boost::ref(face), _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080052
53 // processEvents will block until the requested data received or timeout occurs
54 face.processEvents();
55 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070056 catch(std::exception& e) {
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080057 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058 return 1;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080059 }
60 return 0;
61}
Alexander Afanasyev151a8552014-04-11 00:54:43 -070062
63} // namespace examples
64} // namespace ndn
65
66int
67main(int argc, char** argv)
68{
69 return ndn::examples::main(argc, argv);
70}