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
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index 3e3d956..999f9c9 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -88,11 +88,14 @@
   
 private:
   void
-  handleConnection(const boost::system::error_code& error,
-                   const shared_ptr<boost::asio::ip::tcp::socket>& socket,
-                   const FaceCreatedCallback& onFaceCreated,
-                   const ConnectFailedCallback& onConnectFailed,
-                   bool remoteConnection);
+  createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
+             const FaceCreatedCallback& onFaceCreated);
+
+  void
+  handleSuccessfulAccept(const boost::system::error_code& error,
+                         const shared_ptr<boost::asio::ip::tcp::socket>& socket,
+                         const FaceCreatedCallback& onFaceCreated,
+                         const ConnectFailedCallback& onConnectFailed);
 
   void
   handleSuccessfulConnect(const boost::system::error_code& error,
diff --git a/tests/face/tcp.cpp b/tests/face/tcp.cpp
index de48540..66c0a69 100644
--- a/tests/face/tcp.cpp
+++ b/tests/face/tcp.cpp
@@ -106,6 +106,27 @@
   }
 
   void
+  channel_onFaceCreated(const shared_ptr<TcpFace>& newFace)
+  {
+    m_faces.push_back(newFace);
+    this->afterIo();
+  }
+
+  void
+  channel_onConnectFailed(const std::string& reason)
+  {
+    BOOST_CHECK_MESSAGE(false, reason);
+
+    this->afterIo();
+  }
+
+  void
+  checkFaceList(size_t shouldBe)
+  {
+    BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
+  }
+  
+  void
   abortTestCase(const std::string& message)
   {
     m_ioService.stop();
@@ -130,6 +151,8 @@
   shared_ptr<TcpFace> m_face2;
   std::vector<Interest> m_face2_receivedInterests;
   std::vector<Data> m_face2_receivedDatas;
+
+  std::list< shared_ptr<TcpFace> > m_faces;
 };
 
 
@@ -201,6 +224,72 @@
   BOOST_CHECK_EQUAL(m_face2_receivedDatas    [0].getName(), data1.getName());
 }
 
+BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture)
+{
+  TcpChannelFactory factory(m_ioService);
+  Scheduler scheduler(m_ioService); // to limit the amount of time the test may take
+
+  EventId abortEvent =
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "TcpChannel error: cannot connect or cannot accept connection"));
+  
+  shared_ptr<TcpChannel> channel1 = factory.create("127.0.0.1", "20070");
+  shared_ptr<TcpChannel> channel2 = factory.create("127.0.0.1", "20071");
+  
+  channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated,   this, _1),
+                   bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
+  
+  channel2->connect("127.0.0.1", "20070",
+                    bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
+                    bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
+                    time::milliseconds(100)); // very short timeout
+
+  m_ioRemaining = 2;
+  m_ioService.run();
+  m_ioService.reset();
+  scheduler.cancelEvent(abortEvent);
+
+  BOOST_CHECK_EQUAL(m_faces.size(), 2);
+  
+  shared_ptr<TcpChannel> channel3 = factory.create("127.0.0.1", "20072");
+  channel3->connect("127.0.0.1", "20070",
+                    bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
+                    bind(&EndToEndFixture::channel_onConnectFailed, this, _1),
+                    time::milliseconds(100)); // very short timeout
+
+
+  shared_ptr<TcpChannel> channel4 = factory.create("127.0.0.1", "20073");
+
+  BOOST_CHECK_NE(channel3, channel4);
+  
+  scheduler
+    .scheduleEvent
+    (time::seconds(0.5),
+     bind(&TcpChannel::connect, channel4,
+          "127.0.0.1", "20070",
+          // does not work without static_cast
+          static_cast<TcpChannel::FaceCreatedCallback>(bind(&EndToEndFixture::
+                                                            channel_onFaceCreated, this, _1)),
+          static_cast<TcpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
+                                                              channel_onConnectFailed, this, _1)),
+          time::milliseconds(100)));
+  
+  m_ioRemaining = 4; // 2 connects and 2 accepts
+  abortEvent = 
+    scheduler.scheduleEvent(time::seconds(1),
+                            bind(&EndToEndFixture::abortTestCase, this,
+                                 "TcpChannel error: cannot connect or cannot accept multiple connections"));
+
+  scheduler.scheduleEvent(time::seconds(0.4),
+                          bind(&EndToEndFixture::checkFaceList, this, 4));
+  
+  m_ioService.run();
+  m_ioService.reset();
+  
+  BOOST_CHECK_EQUAL(m_faces.size(), 6);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace nfd