blob: 536e16415b4c9464c5136d03e8878c93813f8360 [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;
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 }
27
Jeff Thompson51755b32013-09-18 16:46:56 -070028 void onData(const shared_ptr<const Interest>& interest, const 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;
Jeff Thompson97223af2013-09-24 17:01:27 -070032 for (size_t 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 Thompson51755b32013-09-18 16:46:56 -070037 void onTimeout(const 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 Thompsona2953b82013-10-03 14:13:52 -070049 // Connect to port 9695 until the testbed hubs use NDNx.
Jeff Thompson640aa622013-10-07 10:32:50 -070050 Face face("A.hub.ndn.ucla.edu", 9695);
Jeff Thompson3a217062013-07-14 23:37:42 -070051
Jeff Thompson0960b652013-08-22 19:10:18 -070052 // Counter holds data used by the callbacks.
53 Counter counter;
54
Jeff Thompson557b81e2013-08-21 15:13:51 -070055 Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
56 cout << "Express name " << name1.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070057 // Use bind to pass the counter object to the callbacks.
58 face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070059
Jeff Thompson557b81e2013-08-21 15:13:51 -070060 Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
61 cout << "Express name " << name2.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070062 face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070063
64 Name name3("/test/timeout");
65 cout << "Express name " << name3.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070066 face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson557b81e2013-08-21 15:13:51 -070067
Jeff Thompson432c8be2013-08-09 16:16:08 -070068 // The main event loop.
Jeff Thompson0960b652013-08-22 19:10:18 -070069 while (counter.callbackCount_ < 3) {
Jeff Thompsonc7e07442013-08-19 15:25:43 -070070 face.processEvents();
71 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
72 usleep(10000);
73 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -070074 } catch (std::exception& e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070075 cout << "exception: " << e.what() << endl;
76 }
77 return 0;
78}