Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <ndn.cxx.h> |
| 12 | #include <iostream> |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | const char *FILENAME = NULL; |
| 17 | ndn::Name InterestBaseName; |
| 18 | |
| 19 | // create a global handler |
| 20 | ndn::Wrapper handler; |
| 21 | |
| 22 | void OnData (ndn::Name name, ndn::PcoPtr pco); |
| 23 | void OnTimeout (ndn::Name name, const ndn::Closure &closure, ndn::InterestPtr origInterest); |
| 24 | |
| 25 | void OnData (ndn::Name name, ndn::PcoPtr pco) |
| 26 | { |
| 27 | ndn::BytesPtr content = pco->contentPtr (); |
| 28 | cout << string ((char*)ndn::head (*content), content->size ()); |
| 29 | |
| 30 | int seqnum = ndn::Name::asSeqNum (*name.rbegin ()); |
| 31 | if (seqnum >= 10) |
| 32 | { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | cerr << ">> C++ " << ndn::Name (InterestBaseName).appendSeqNum (seqnum + 1) << endl; // a shortcut to construct name |
| 37 | handler.sendInterest (ndn::Interest () |
| 38 | .setName (ndn::Name (InterestBaseName).appendSeqNum (seqnum + 1)) |
| 39 | .setScope (ndn::Interest::SCOPE_LOCAL_HOST), |
| 40 | ndn::Closure (OnData, OnTimeout)); |
| 41 | } |
| 42 | |
| 43 | void OnTimeout (ndn::Name name, const ndn::Closure &closure, ndn::InterestPtr origInterest) |
| 44 | { |
| 45 | // re-express interest |
| 46 | handler.sendInterest (*origInterest, closure); |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | main (int argc, char **argv) |
| 51 | { |
| 52 | if (argc < 2) |
| 53 | { |
| 54 | std::cerr << "You have to specify filename as an argument" << std::endl; |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | // this code does not check for most of the bad conditions |
| 59 | FILENAME = argv[1]; |
| 60 | |
| 61 | InterestBaseName = ndn::Name ("/my-local-prefix/simple-fetch/file"); |
| 62 | InterestBaseName.append (FILENAME); |
| 63 | |
| 64 | cerr << ">> C++ " << ndn::Name (InterestBaseName).appendSeqNum (0) << endl; |
| 65 | handler.sendInterest (ndn::Interest () |
| 66 | .setName (ndn::Name (InterestBaseName).appendSeqNum (0)) |
| 67 | .setScope (ndn::Interest::SCOPE_LOCAL_HOST), |
| 68 | ndn::Closure (OnData, OnTimeout)); |
| 69 | |
| 70 | sleep (3); |
| 71 | return 0; |
| 72 | } |