face+node: Improving async operations

Now expressInterest is executed strictly in io_service thread, which
should guarantee absence of resource contention, provided that
io_service has exactly one thread.

Instead of numerical IDs for expressed Interests and registered
prefixes, an opaque PendingInterestId and RegisteredPrefixId classed are
used.  These classes are basically reinterpret_cast pointers to the
PendingInterest and RegisteredPrefix objects stored on the node.

refs #1142 (http://redmine.named-data.net/issues/1142)

Change-Id: I4b146ee34b98bfa6564935d5f2fe33056a402694
diff --git a/src/node.cpp b/src/node.cpp
index d227199..22f4963 100644
--- a/src/node.cpp
+++ b/src/node.cpp
@@ -25,9 +25,6 @@
 
 namespace ndn {
 
-uint64_t Node::PendingInterest::lastPendingInterestId_ = 0;
-uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0;
-
 Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
   : pitTimeoutCheckTimerActive_(false)
   , transport_(transport)
@@ -48,28 +45,35 @@
   processEventsTimeoutTimer_ = ptr_lib::make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
 }
 
-uint64_t 
+const PendingInterestId*
 Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
 {
   if (!transport_->isConnected())
     transport_->connect(*ioService_,
                         ptr_lib::bind(&Node::onReceiveElement, this, _1));
-  
-  uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
-  pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
-    (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
 
-  transport_->send(interest.wireEncode());
+  ptr_lib::shared_ptr<const Interest> interestToExpress(new Interest(interest));
+  
+  ioService_->post(func_lib::bind(&Node::asyncExpressInterest, this, interestToExpress, onData, onTimeout));
+  
+  return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
+}
+
+void
+Node::asyncExpressInterest(const ptr_lib::shared_ptr<const Interest> &interest, const OnData& onData, const OnTimeout& onTimeout)
+{
+  pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
+    (interest, onData, onTimeout)));
+
+  transport_->send(interest->wireEncode());
 
   if (!pitTimeoutCheckTimerActive_) {
     pitTimeoutCheckTimerActive_ = true;
     pitTimeoutCheckTimer_->expires_from_now(boost::posix_time::milliseconds(100));
     pitTimeoutCheckTimer_->async_wait(func_lib::bind(&Node::checkPitExpire, this));
   }
-  
-  return pendingInterestId;
 }
-
+    
 void
 Node::put(const Data &data)
 {
@@ -82,83 +86,78 @@
 
 
 void
-Node::removePendingInterest(uint64_t pendingInterestId)
+Node::removePendingInterest(const PendingInterestId *pendingInterestId)
 {
-  // Go backwards through the list so we can erase entries.
-  // Remove all entries even though pendingInterestId should be unique.
-  for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
-    if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
-      pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
-  }
+  ioService_->post(func_lib::bind(&Node::asyncRemovePendingInterest, this, pendingInterestId));
 }
 
-uint64_t 
+
+void
+Node::asyncRemovePendingInterest(const PendingInterestId *pendingInterestId)
+{
+  std::remove_if(pendingInterestTable_.begin(), pendingInterestTable_.end(),
+                 MatchPendingInterestId(pendingInterestId));
+}
+
+const RegisteredPrefixId*
 Node::registerPrefix
   (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
 {
-  // Get the registeredPrefixId now so we can return it to the caller.
-  uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
-  ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
+  ptr_lib::shared_ptr<RegisteredPrefix> prefixToRegister(new RegisteredPrefix(prefix, onInterest));
   
   if (ndndId_.size() == 0) {
     // First fetch the ndndId of the connected hub.
     NdndIdFetcher fetcher(ndndId_,
                           func_lib::bind(&Node::registerPrefixHelper, this,
-                                         registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
-                          func_lib::bind(onRegisterFailed, prefixPtr));
+                                         prefixToRegister, onRegisterFailed, flags),
+                          func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this()));
 
     // @todo: Check if this crash
     // It is OK for func_lib::function make a copy of the function object because the Info is in a ptr_lib::shared_ptr.
     expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
   }
   else
-    registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
+    registerPrefixHelper(prefixToRegister, onRegisterFailed, flags);
   
-  return registeredPrefixId;
+  return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
 }
 
 void
-Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
+Node::removeRegisteredPrefix(const RegisteredPrefixId *registeredPrefixId)
 {
-  // Go backwards through the list so we can erase entries.
-  // Remove all entries even though pendingInterestId should be unique.
-
-  for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
-       i != registeredPrefixTable_.end();
-       ++i)
+  RegisteredPrefixTable::iterator i = std::find_if(registeredPrefixTable_.begin(), registeredPrefixTable_.end(),
+                                                   MatchRegisteredPrefixId(registeredPrefixId));  
+  if (i != registeredPrefixTable_.end())
     {
-      if ((*i)->getRegisteredPrefixId() == registeredPrefixId) {
-        ForwardingEntry forwardingEntry("unreg", *(*i)->getPrefix(), faceId_);
-        Data data;
-        data.setContent(forwardingEntry.wireEncode());
+      ForwardingEntry forwardingEntry("unreg", (*i)->getPrefix(), faceId_);
+      Data data;
+      data.setContent(forwardingEntry.wireEncode());
         
-        SignatureSha256WithRsa signature;
-        signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
-        data.setSignature(signature);
+      SignatureSha256WithRsa signature;
+      signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
+      data.setSignature(signature);
 
-        // Create an interest where the name has the encoded Data packet.
-        Name interestName;
-        interestName.append("ndnx");
-        interestName.append(ndndId_);
-        interestName.append("unreg");
-        interestName.append(data.wireEncode());
+      // Create an interest where the name has the encoded Data packet.
+      Name interestName;
+      interestName.append("ndnx");
+      interestName.append(ndndId_);
+      interestName.append("unreg");
+      interestName.append(data.wireEncode());
 
-        Interest interest(interestName);
-        interest.setScope(1);
-        interest.setInterestLifetime(1000);
+      Interest interest(interestName);
+      interest.setScope(1);
+      interest.setInterestLifetime(1000);
 
-        expressInterest(interest, OnData(), OnTimeout());
+      expressInterest(interest, OnData(), OnTimeout());
         
-        registeredPrefixTable_.erase(i);
-        break;
-      }
+      registeredPrefixTable_.erase(i);
     }
+
+  // there cannot be two registered prefixes with the same id. if there are, then something is broken
 }
 
 void 
-Node::registerPrefixHelper(uint64_t registeredPrefixId,
-                           const ptr_lib::shared_ptr<const Name>& prefix,
-                           const OnInterest& onInterest,
+Node::registerPrefixHelper(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
                            const OnRegisterFailed& onRegisterFailed, 
                            const ForwardingFlags& flags)
 {
@@ -166,7 +165,7 @@
 
   // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
   //        to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
-  ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
+  ForwardingEntry forwardingEntry("selfreg", prefixToRegister->getPrefix(), -1, flags, -1);
   Block content = forwardingEntry.wireEncode();
 
   // Set the ForwardingEntry as the content of a Data packet and sign.
@@ -192,14 +191,12 @@
 
   expressInterest(interest,
                   func_lib::bind(&Node::registerPrefixFinal, this,
-                                 registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2),
-                  func_lib::bind(onRegisterFailed, prefix));
+                                 prefixToRegister, onRegisterFailed, _1, _2),
+                  func_lib::bind(onRegisterFailed, prefixToRegister->getPrefix().shared_from_this()));
 }
 
 void
-Node::registerPrefixFinal(uint64_t registeredPrefixId,
-                          const ptr_lib::shared_ptr<const Name>& prefix,
-                          const OnInterest& onInterest,
+Node::registerPrefixFinal(const ptr_lib::shared_ptr<RegisteredPrefix> &prefixToRegister,
                           const OnRegisterFailed& onRegisterFailed,
                           const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
 {
@@ -208,7 +205,7 @@
 
   if (content.getAll().empty())
     {
-      onRegisterFailed(prefix);
+      onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
       return;
     }
 
@@ -222,7 +219,7 @@
         entry.wireDecode(*val);
 
         // Save the onInterest callback and send the registration interest.
-        registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
+        registeredPrefixTable_.push_back(prefixToRegister);
 
         /// @todo Notify user about successful registration
         
@@ -237,14 +234,14 @@
 
         // std::cerr << "StatusReponse: " << resp << std::endl;
       
-        onRegisterFailed(prefix);
+        onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
         return;
       }
     default:
       {
         // failed :(
       
-        onRegisterFailed(prefix);
+        onRegisterFailed(prefixToRegister->getPrefix().shared_from_this());
         return;
       }
     }
@@ -343,7 +340,7 @@
     
       RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
       if (entry != registeredPrefixTable_.end()) {
-        (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
+        (*entry)->getOnInterest()((*entry)->getPrefix().shared_from_this(), interest);
       }
     }
   else if (block.type() == Tlv::Data)
@@ -406,7 +403,7 @@
        ++i)
     {
       if (longestPrefix == registeredPrefixTable_.end() ||
-          (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
+          (*i)->getPrefix().size() > (*longestPrefix)->getPrefix().size())
         {
           longestPrefix = i;
         }
@@ -414,11 +411,9 @@
   return longestPrefix;
 }
 
-Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
-                                       const ptr_lib::shared_ptr<const Interest>& interest,
+Node::PendingInterest::PendingInterest(const ptr_lib::shared_ptr<const Interest>& interest,
                                        const OnData& onData, const OnTimeout& onTimeout)
-: pendingInterestId_(pendingInterestId),
-  interest_(interest),
+: interest_(interest),
   onData_(onData), onTimeout_(onTimeout)
 {
   // Set up timeoutTime_.