blob: 46e99205cdb46f80a2c8c4d691058255f705f128 [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::ptr_lib;
16using namespace ndn::func_lib;
Jeff Thompsonb7523002013-10-09 10:25:00 -070017#if NDN_CPP_HAVE_STD_FUNCTION
Jeff Thompson04aa7112013-08-22 19:16:25 -070018// In the std library, the placeholders are in a different namespace than boost.
19using namespace func_lib::placeholders;
20#endif
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070021
Jeff Thompson0960b652013-08-22 19:10:18 -070022class Counter
Jeff Thompson7aec0252013-08-22 17:29:57 -070023{
Jeff Thompson0960b652013-08-22 19:10:18 -070024public:
25 Counter() {
26 callbackCount_ = 0;
27 }
28
Jeff Thompson51755b32013-09-18 16:46:56 -070029 void onData(const shared_ptr<const Interest>& interest, const shared_ptr<Data>& data)
Jeff Thompson0960b652013-08-22 19:10:18 -070030 {
31 ++callbackCount_;
32 cout << "Got data packet with name " << data->getName().to_uri() << endl;
Jeff Thompson97223af2013-09-24 17:01:27 -070033 for (size_t i = 0; i < data->getContent().size(); ++i)
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070034 cout << (*data->getContent())[i];
Jeff Thompson0960b652013-08-22 19:10:18 -070035 cout << endl;
36 }
Jeff Thompson7aec0252013-08-22 17:29:57 -070037
Jeff Thompson51755b32013-09-18 16:46:56 -070038 void onTimeout(const shared_ptr<const Interest>& interest)
Jeff Thompson0960b652013-08-22 19:10:18 -070039 {
40 ++callbackCount_;
41 cout << "Time out for interest " << interest->getName().toUri() << endl;
42 }
43
44 int callbackCount_;
45};
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070046
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070047int main(int argc, char** argv)
48{
49 try {
Jeff Thompsona2953b82013-10-03 14:13:52 -070050 // Connect to port 9695 until the testbed hubs use NDNx.
Jeff Thompsonfe110052013-10-23 16:41:41 -070051 Face face("C.hub.ndn.ucla.edu", 9695);
Jeff Thompson3a217062013-07-14 23:37:42 -070052
Jeff Thompson0960b652013-08-22 19:10:18 -070053 // Counter holds data used by the callbacks.
54 Counter counter;
55
Jeff Thompson557b81e2013-08-21 15:13:51 -070056 Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
Jeff Thompson72b58a42013-11-16 16:46:31 -080057 cout << "Express name " << name1.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070058 // Use bind to pass the counter object to the callbacks.
59 face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070060
Jeff Thompson557b81e2013-08-21 15:13:51 -070061 Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
Jeff Thompson72b58a42013-11-16 16:46:31 -080062 cout << "Express name " << name2.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070063 face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson7aec0252013-08-22 17:29:57 -070064
65 Name name3("/test/timeout");
Jeff Thompson72b58a42013-11-16 16:46:31 -080066 cout << "Express name " << name3.toUri() << endl;
Jeff Thompson0960b652013-08-22 19:10:18 -070067 face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Jeff Thompson557b81e2013-08-21 15:13:51 -070068
Jeff Thompson432c8be2013-08-09 16:16:08 -070069 // The main event loop.
Jeff Thompson0960b652013-08-22 19:10:18 -070070 while (counter.callbackCount_ < 3) {
Jeff Thompsonc7e07442013-08-19 15:25:43 -070071 face.processEvents();
72 // We need to sleep for a few milliseconds so we don't use 100% of the CPU.
73 usleep(10000);
74 }
Jeff Thompson1656e6a2013-08-29 18:01:48 -070075 } catch (std::exception& e) {
Jeff Thompsonc98be1a2013-07-14 22:44:43 -070076 cout << "exception: " << e.what() << endl;
77 }
78 return 0;
79}