blob: da02c812b22fd49e95c121ab5ab5a6c028de3211 [file] [log] [blame]
Jeff Thompsonc98be1a2013-07-14 22:44:43 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#include <cstdlib>
7#include <sstream>
8#include <iostream>
Jeff Thompson17882b42013-08-23 16:23:22 -07009#include "../ndn-cpp/face.hpp"
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070010
11using namespace std;
12using namespace ndn;
Jeff Thompson1bbc4e72013-07-16 16:30:55 -070013using namespace ptr_lib;
Jeff Thompson04aa7112013-08-22 19:16:25 -070014using namespace func_lib;
15#if HAVE_STD_FUNCTION
16// In the std library, the placeholders are in a different namespace than boost.
17using namespace func_lib::placeholders;
18#endif
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070019
Jeff Thompson0960b652013-08-22 19:10:18 -070020class Counter
Jeff Thompson7aec0252013-08-22 17:29:57 -070021{
Jeff Thompson0960b652013-08-22 19:10:18 -070022public:
23 Counter() {
24 callbackCount_ = 0;
25 }
26
27 void onData(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data)
28 {
29 ++callbackCount_;
30 cout << "Got data packet with name " << data->getName().to_uri() << endl;
31 for (unsigned int i = 0; i < data->getContent().size(); ++i)
32 cout << data->getContent()[i];
33 cout << endl;
34 }
Jeff Thompson7aec0252013-08-22 17:29:57 -070035
Jeff Thompson0960b652013-08-22 19:10:18 -070036 void onTimeout(const ptr_lib::shared_ptr<const Interest> &interest)
37 {
38 ++callbackCount_;
39 cout << "Time out for interest " << interest->getName().toUri() << endl;
40 }
41
42 int callbackCount_;
43};
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070044
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070045int main(int argc, char** argv)
46{
47 try {
Jeff Thompsondc461ab2013-08-19 18:15:59 -070048 Face face("E.hub.ndn.ucla.edu");
Jeff Thompson3a217062013-07-14 23:37:42 -070049
Jeff Thompson0960b652013-08-22 19:10:18 -070050 // Counter holds data used by the callbacks.
51 Counter counter;
52
Jeff Thompson557b81e2013-08-21 15:13:51 -070053 Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
54 cout << "Express name " << name1.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070055 // Use bind to pass the counter object to the callbacks.
56 face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070057
Jeff Thompson557b81e2013-08-21 15:13:51 -070058 Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
59 cout << "Express name " << name2.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070060 face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070061
62 Name name3("/test/timeout");
63 cout << "Express name " << name3.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070064 face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson557b81e2013-08-21 15:13:51 -070065
Jeff Thompson432c8be2013-08-09 16:16:08 -070066 // The main event loop.
Jeff Thompson0960b652013-08-22 19:10:18 -070067 while (counter.callbackCount_ < 3) {
Jeff Thompsonc7e07442013-08-19 15:25:43 -070068 face.processEvents();
69 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
70 usleep(10000);
71 }
Jeff Thompson1bbc4e72013-07-16 16:30:55 -070072 } catch (std::exception &e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070073 cout << "exception: " << e.what() << endl;
74 }
75 return 0;
76}