face/tcp: Fixing TcpChannel and TcpFace implementation and enable basic logging in both

Change-Id: If9a00c13caccbeb117204655ced293d49860d6e4
refs: #1134, #1133
diff --git a/daemon/face/stream-face.hpp b/daemon/face/stream-face.hpp
index a764a07..529e3ef 100644
--- a/daemon/face/stream-face.hpp
+++ b/daemon/face/stream-face.hpp
@@ -34,8 +34,12 @@
 private:
   uint8_t m_inputBuffer[MAX_NDN_PACKET_SIZE];
   std::size_t m_inputBufferSize;
+
+  NFD_LOG_INCLASS_DECLARE();
 };
 
+NFD_LOG_INCLASS_TEMPLATE_DEFINE(StreamFace, "StreamFace");
+
 template <class T>
 inline
 StreamFace<T>::StreamFace(const shared_ptr<typename StreamFace::protocol::socket>& socket)
@@ -55,11 +59,20 @@
     if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
       return;
 
-    onFail("Send operation failed: " + error.category().message(error.value()));
+    NFD_LOG_WARN("[id:" << this->getId()
+                 << ",endpoint:" << m_socket->local_endpoint()
+                 << "] Send operation failed, closing socket: "
+                 << error.category().message(error.value()));
+
+    onFail("Send operation failed, closing socket: " +
+           error.category().message(error.value()));
     m_socket->close();
     return;
   }
 
+  NFD_LOG_TRACE("[id:" << this->getId()
+                << ",endpoint:" << m_socket->local_endpoint()
+                << "] Successfully sent: " << wire.size() << " bytes");
   // do nothing (needed to retain validity of wire memory block
 }
 
@@ -68,11 +81,21 @@
 StreamFace<T>::handleReceive(const boost::system::error_code& error,
                              std::size_t bytes_recvd)
 {
+  NFD_LOG_TRACE("[id:" << this->getId()
+                << ",endpoint:" << m_socket->local_endpoint()
+                << "] Received: " << bytes_recvd << " bytes");
+
   if (error || bytes_recvd == 0) {
     if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
       return;
 
-    onFail("Receive operation failed: " + error.category().message(error.value()));
+    NFD_LOG_WARN("[id:" << this->getId()
+                 << ",endpoint:" << m_socket->local_endpoint()
+                 << "] Receive operation failed: "
+                 << error.category().message(error.value()));
+    
+    onFail("Receive operation failed, closing socket: " +
+           error.category().message(error.value()));
     m_socket->close();
     return;
   }
@@ -83,38 +106,50 @@
   std::size_t offset = 0;
   /// @todo Eliminate reliance on exceptions in this path
   try {
-    Block element(m_inputBuffer + offset, m_inputBufferSize - offset);
-    offset += element.size();
+    while(m_inputBufferSize - offset > 0)
+      {
+        Block element(m_inputBuffer + offset, m_inputBufferSize - offset);
+        offset += element.size();
 
-    /// @todo Ensure lazy field decoding process
-    if (element.type() == tlv::Interest)
-      {
-        shared_ptr<Interest> i = make_shared<Interest>();
-        i->wireDecode(element);
-        onReceiveInterest(*i);
-      }
-    else if (element.type() == tlv::Data)
-      {
-        shared_ptr<Data> d = make_shared<Data>();
-        d->wireDecode(element);
-        onReceiveData(*d);
-      }
-    // @todo Add local header support
-    // else if (element.type() == tlv::LocalHeader)
-    //   {
-    //     shared_ptr<Interest> i = make_shared<Interest>();
-    //     i->wireDecode(element);
-    //   }
-    else
-      {
-        /// @todo Add loggin
-
-        // ignore unknown packet and proceed
+        BOOST_ASSERT(offset <= m_inputBufferSize);
+        
+        /// @todo Ensure lazy field decoding process
+        if (element.type() == tlv::Interest)
+          {
+            shared_ptr<Interest> i = make_shared<Interest>();
+            i->wireDecode(element);
+            onReceiveInterest(*i);
+          }
+        else if (element.type() == tlv::Data)
+          {
+            shared_ptr<Data> d = make_shared<Data>();
+            d->wireDecode(element);
+            onReceiveData(*d);
+          }
+        // @todo Add local header support
+        // else if (element.type() == tlv::LocalHeader)
+        //   {
+        //     shared_ptr<Interest> i = make_shared<Interest>();
+        //     i->wireDecode(element);
+        //   }
+        else
+          {
+            NFD_LOG_WARN("[id:" << this->getId()
+                         << ",endpoint:" << m_socket->local_endpoint()
+                         << "] Received unrecognized block of type ["
+                         << element.type() << "]");
+            // ignore unknown packet and proceed
+          }
       }
   }
-  catch(const tlv::Error&) {
+  catch(const tlv::Error& e) {
     if (m_inputBufferSize == MAX_NDN_PACKET_SIZE && offset == 0)
       {
+        NFD_LOG_WARN("[id:" << this->getId()
+                     << ",endpoint:" << m_socket->local_endpoint()
+                     << "] Received input is invalid or too large to process, "
+                     << "closing down the face");
+        
         onFail("Received input is invalid or too large to process, closing down the face");
         m_socket->close();
         return;