face/tcp: Fixing multiple accept on a channel

Also, this commit includes more thorough test for TCP channel (testing
if multiple connections can be made to the same channel).

Change-Id: I3b89a6d994b5b17286448013aa04a77c92c0267a
refs: #1134
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index e3ad8d5..026c338 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -33,10 +33,9 @@
   shared_ptr<ip::tcp::socket> clientSocket =
     make_shared<ip::tcp::socket>(boost::ref(m_ioService));
   m_acceptor->async_accept(*clientSocket,
-                           bind(&TcpChannel::handleConnection, this, _1,
+                           bind(&TcpChannel::handleSuccessfulAccept, this, _1,
                                 clientSocket,
-                                onFaceCreated, onAcceptFailed,
-                                true));
+                                onFaceCreated, onAcceptFailed));
 }
 
 void
@@ -114,11 +113,21 @@
 
 
 void
-TcpChannel::handleConnection(const boost::system::error_code& error,
-                             const shared_ptr<ip::tcp::socket>& socket,
-                             const FaceCreatedCallback& onFaceCreated,
-                             const ConnectFailedCallback& onConnectFailed,
-                             bool remoteConnection)
+TcpChannel::createFace(const shared_ptr<ip::tcp::socket>& socket,
+                       const FaceCreatedCallback& onFaceCreated)
+{
+  shared_ptr<TcpFace> face = make_shared<TcpFace>(boost::cref(socket));
+  onFaceCreated(face);
+
+  tcp::Endpoint remoteEndpoint = socket->remote_endpoint();
+  m_channelFaces[remoteEndpoint] = face;
+}
+
+void
+TcpChannel::handleSuccessfulAccept(const boost::system::error_code& error,
+                                   const shared_ptr<boost::asio::ip::tcp::socket>& socket,
+                                   const FaceCreatedCallback& onFaceCreated,
+                                   const ConnectFailedCallback& onAcceptFailed)
 {
   if (error) {
     if (error == boost::system::errc::operation_canceled) // when socket is closed by someone
@@ -127,30 +136,22 @@
     NFD_LOG_DEBUG("Connect to remote endpoint failed: "
                   << error.category().message(error.value()));
     
-    onConnectFailed("Connect to remote endpoint failed: " +
-                    error.category().message(error.value()));
+    onAcceptFailed("Connect to remote endpoint failed: " +
+                   error.category().message(error.value()));
     return;
   }
 
-  if (remoteConnection)
-    {
-      NFD_LOG_DEBUG("[" << socket->local_endpoint() << "] "
-                    "<< Connection from " << socket->remote_endpoint());
-    }
-  else
-    {
-      NFD_LOG_DEBUG("[" << socket->local_endpoint() << "] "
-                    ">> Connection to " << socket->remote_endpoint());
-    }
-  
-  /**
-   * \todo Remove FaceId from here
-   */
-  shared_ptr<TcpFace> face = make_shared<TcpFace>(boost::cref(socket));
-  onFaceCreated(face);
+  // prepare accepting the next connection
+  shared_ptr<ip::tcp::socket> clientSocket =
+    make_shared<ip::tcp::socket>(boost::ref(m_ioService));
+  m_acceptor->async_accept(*clientSocket,
+                           bind(&TcpChannel::handleSuccessfulAccept, this, _1,
+                                clientSocket,
+                                onFaceCreated, onAcceptFailed));
 
-  tcp::Endpoint remoteEndpoint = socket->remote_endpoint();
-  m_channelFaces[remoteEndpoint] = face;
+  NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
+                "<< Connection from " << socket->remote_endpoint());
+  createFace(socket, onFaceCreated);
 }
 
 void
@@ -176,7 +177,10 @@
     return;
   }
 
-  handleConnection(error, socket, onFaceCreated, onConnectFailed, false);
+  NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
+                ">> Connection to " << socket->remote_endpoint());
+  
+  createFace(socket, onFaceCreated);
 }
 
 void