Change to use a Counter class and use bind to pass callbacks.
diff --git a/tests/test-get-async.cpp b/tests/test-get-async.cpp
index 60b02b5..76ae5c1 100644
--- a/tests/test-get-async.cpp
+++ b/tests/test-get-async.cpp
@@ -14,42 +14,54 @@
using namespace ndn;
using namespace ptr_lib;
-static int CallbackCount = 0;
-
-void myOnData(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data)
+class Counter
{
- ++CallbackCount;
- cout << "Got data packet with name " << data->getName().to_uri() << endl;
- for (unsigned int i = 0; i < data->getContent().size(); ++i)
- cout << data->getContent()[i];
- cout << endl;
-}
+public:
+ Counter() {
+ callbackCount_ = 0;
+ }
+
+ void onData(const ptr_lib::shared_ptr<const Interest> &interest, const ptr_lib::shared_ptr<Data> &data)
+ {
+ ++callbackCount_;
+ cout << "Got data packet with name " << data->getName().to_uri() << endl;
+ for (unsigned int i = 0; i < data->getContent().size(); ++i)
+ cout << data->getContent()[i];
+ cout << endl;
+ }
-void myOnTimeout(const ptr_lib::shared_ptr<const Interest> &interest)
-{
- ++CallbackCount;
- cout << "Time out for interest " << interest->getName().toUri() << endl;
-}
+ void onTimeout(const ptr_lib::shared_ptr<const Interest> &interest)
+ {
+ ++callbackCount_;
+ cout << "Time out for interest " << interest->getName().toUri() << endl;
+ }
+
+ int callbackCount_;
+};
int main(int argc, char** argv)
{
try {
Face face("E.hub.ndn.ucla.edu");
+ // Counter holds data used by the callbacks.
+ Counter counter;
+
Name name1("/ndn/ucla.edu/apps/ndn-js-test/hello.txt/level2/%FD%05%0B%16%7D%95%0E");
cout << "Express name " << name1.toUri() << endl;
- face.expressInterest(name1, myOnData, myOnTimeout);
+ // Use bind to pass the counter object to the callbacks.
+ face.expressInterest(name1, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Name name2("/ndn/ucla.edu/apps/lwndn-test/howdy.txt/%FD%05%05%E8%0C%CE%1D");
cout << "Express name " << name2.toUri() << endl;
- face.expressInterest(name2, myOnData, myOnTimeout);
+ face.expressInterest(name2, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
Name name3("/test/timeout");
cout << "Express name " << name3.toUri() << endl;
- face.expressInterest(name3, myOnData, myOnTimeout);
+ face.expressInterest(name3, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1));
// The main event loop.
- while (CallbackCount < 3) {
+ while (counter.callbackCount_ < 3) {
face.processEvents();
// We need to sleep for a few milliseconds so we don't use 100% of the CPU.
usleep(10000);