blob: 124ea4c760eb967e4afe40bcbe403d3041110334 [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 Thompson17882b42013-08-23 16:23:22 -070010#include "../ndn-cpp/face.hpp"
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070011
12using namespace std;
13using namespace ndn;
Jeff Thompson1bbc4e72013-07-16 16:30:55 -070014using namespace ptr_lib;
Jeff Thompson04aa7112013-08-22 19:16:25 -070015using namespace func_lib;
16#if HAVE_STD_FUNCTION
17// 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 }
27
Jeff Thompson1656e6a2013-08-29 18:01:48 -070028 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_;
31 cout << "Got data packet with name " << data->getName().to_uri() << endl;
32 for (unsigned int i = 0; i < data->getContent().size(); ++i)
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070033 cout << (*data->getContent())[i];
Jeff Thompson0960b652013-08-22 19:10:18 -070034 cout << endl;
35 }
Jeff Thompson7aec0252013-08-22 17:29:57 -070036
Jeff Thompson1656e6a2013-08-29 18:01:48 -070037 void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest)
Jeff Thompson0960b652013-08-22 19:10:18 -070038 {
39 ++callbackCount_;
40 cout << "Time out for interest " << interest->getName().toUri() << endl;
41 }
42
43 int callbackCount_;
44};
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070045
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070046int main(int argc, char** argv)
47{
48 try {
Jeff Thompsondc461ab2013-08-19 18:15:59 -070049 Face face("E.hub.ndn.ucla.edu");
Jeff Thompson3a217062013-07-14 23:37:42 -070050
Jeff Thompson0960b652013-08-22 19:10:18 -070051 // Counter holds data used by the callbacks.
52 Counter counter;
53
Jeff Thompson557b81e2013-08-21 15:13:51 -070054 Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
55 cout << "Express name " << name1.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070056 // Use bind to pass the counter object to the callbacks.
57 face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070058
Jeff Thompson557b81e2013-08-21 15:13:51 -070059 Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
60 cout << "Express name " << name2.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070061 face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070062
63 Name name3("/test/timeout");
64 cout << "Express name " << name3.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070065 face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson557b81e2013-08-21 15:13:51 -070066
Jeff Thompson432c8be2013-08-09 16:16:08 -070067 // The main event loop.
Jeff Thompson0960b652013-08-22 19:10:18 -070068 while (counter.callbackCount_ < 3) {
Jeff Thompsonc7e07442013-08-19 15:25:43 -070069 face.processEvents();
70 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
71 usleep(10000);
72 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -070073 } catch (std::exception& e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070074 cout << "exception: " << e.what() << endl;
75 }
76 return 0;
77}