blob: 388d7a59f3c74a828808d7a16dc1d406fda7f1d2 [file] [log] [blame]
Jeff Thompsonc98be1a2013-07-14 22:44:43 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonc98be1a2013-07-14 22:44:43 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#include <cstdlib>
8#include <sstream>
9#include <iostream>
Jeff Thompsonfe110052013-10-23 16:41:41 -070010#include <unistd.h>
Jeff Thompson25b4e612013-10-10 16:03:24 -070011#include <ndn-cpp/face.hpp>
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070012
13using namespace std;
14using namespace ndn;
Jeff Thompson25bfdca2013-10-16 17:05:41 -070015using namespace ndn::func_lib;
Jeff Thompsonb7523002013-10-09 10:25:00 -070016#if NDN_CPP_HAVE_STD_FUNCTION
Jeff Thompson04aa7112013-08-22 19:16:25 -070017// In the std library, the placeholders are in a different namespace than boost.
18using namespace func_lib::placeholders;
19#endif
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070020
Jeff Thompson0960b652013-08-22 19:10:18 -070021class Counter
Jeff Thompson7aec0252013-08-22 17:29:57 -070022{
Jeff Thompson0960b652013-08-22 19:10:18 -070023public:
24 Counter() {
25 callbackCount_ = 0;
26 }
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080027
Jeff Thompson06d17802013-12-11 16:34:27 -080028 void onData(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& data)
Jeff Thompson0960b652013-08-22 19:10:18 -070029 {
30 ++callbackCount_;
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080031 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 Thompson0960b652013-08-22 19:10:18 -070033 }
Jeff Thompson7aec0252013-08-22 17:29:57 -070034
Jeff Thompson06d17802013-12-11 16:34:27 -080035 void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest)
Jeff Thompson0960b652013-08-22 19:10:18 -070036 {
37 ++callbackCount_;
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080038 cout << "Time out for interest " << interest->getName().toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070039 }
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080040
Jeff Thompson0960b652013-08-22 19:10:18 -070041 int callbackCount_;
42};
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070043
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070044int main(int argc, char** argv)
45{
46 try {
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080047 Face face;
48
Jeff Thompson0960b652013-08-22 19:10:18 -070049 // Counter holds data used by the callbacks.
50 Counter counter;
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080051
52 Name name1("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY");
Jeff Thompson72b58a42013-11-16 16:46:31 -080053 cout << "Express name " << name1.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070054 // 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 Afanasyev84c2bd42014-01-09 22:35:35 -080056
57 Name name2("/ndnx/ping");
Jeff Thompson72b58a42013-11-16 16:46:31 -080058 cout << "Express name " << name2.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070059 face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080060
Jeff Thompson7aec0252013-08-22 17:29:57 -070061 Name name3("/test/timeout");
Jeff Thompson72b58a42013-11-16 16:46:31 -080062 cout << "Express name " << name3.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070063 face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson557b81e2013-08-21 15:13:51 -070064
Jeff Thompson432c8be2013-08-09 16:16:08 -070065 // The main event loop.
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080066 face.processEvents();
67
Jeff Thompson1656e6a2013-08-29 18:01:48 -070068 } catch (std::exception& e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070069 cout << "exception: " << e.what() << endl;
70 }
71 return 0;
72}