face: Minor updates of TCP faces

Change-Id: I9a11fb4b2cd1aaaa670313cab0b08f097554e1fc
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index a983db6..246cdb0 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -154,9 +154,10 @@
 
     NFD_LOG_DEBUG("Connect to remote endpoint failed: "
                   << error.category().message(error.value()));
-    
-    onAcceptFailed("Connect to remote endpoint failed: " +
-                   error.category().message(error.value()));
+
+    if (static_cast<bool>(onAcceptFailed))
+      onAcceptFailed("Connect to remote endpoint failed: " +
+                     error.category().message(error.value()));
     return;
   }
 
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index 45b1a48..0994719 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -7,6 +7,9 @@
 #include "tcp-factory.hpp"
 #include "core/global-io.hpp"
 #include "core/resolver.hpp"
+#include "core/logger.hpp"
+
+NFD_LOG_INIT("TcpFactory");
 
 namespace nfd {
 
@@ -24,23 +27,14 @@
 
   channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint));
   m_channels[endpoint] = channel;
+  NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
   return channel;
 }
 
 shared_ptr<TcpChannel>
 TcpFactory::createChannel(const std::string& localHost, const std::string& localPort)
 {
-  using boost::asio::ip::tcp;
-
-  tcp::resolver::query query(localHost, localPort);
-  tcp::resolver resolver(getGlobalIoService());
-
-  tcp::resolver::iterator end;
-  tcp::resolver::iterator i = resolver.resolve(query);
-  if (i == end)
-    return shared_ptr<TcpChannel>();
-
-  return createChannel(*i);
+  return createChannel(TcpResolver::syncResolve(localHost, localPort));
 }
 
 shared_ptr<TcpChannel>
@@ -64,13 +58,12 @@
   else if (uri.getScheme() == "tcp6")
     addressSelector = resolver::Ipv6Address();
 
-  using boost::asio::ip::tcp;
-  Resolver<tcp>::asyncResolve(uri.getDomain(),
-                              uri.getPort().empty() ? m_defaultPort : uri.getPort(),
-                              bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
-                                   onCreated, onConnectFailed),
-                              onConnectFailed,
-                              addressSelector);
+  TcpResolver::asyncResolve(uri.getDomain(),
+                            uri.getPort().empty() ? m_defaultPort : uri.getPort(),
+                            bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
+                                 onCreated, onConnectFailed),
+                            onConnectFailed,
+                            addressSelector);
 }
 
 void
@@ -95,5 +88,4 @@
                   + boost::lexical_cast<std::string>(endpoint));
 }
 
-
 } // namespace nfd
diff --git a/daemon/face/tcp-factory.hpp b/daemon/face/tcp-factory.hpp
index 1886f24..6ba2e37 100644
--- a/daemon/face/tcp-factory.hpp
+++ b/daemon/face/tcp-factory.hpp
@@ -54,7 +54,7 @@
    *
    * Note that this call will **BLOCK** until resolution is done or failed.
    *
-   * \throws TcpFactory::Error
+   * \throws TcpFactory::Error or std::runtime_error
    */
   shared_ptr<TcpChannel>
   createChannel(const std::string& localHost, const std::string& localPort);
diff --git a/tests/face/tcp.cpp b/tests/face/tcp.cpp
index b601be2..ff5dc50 100644
--- a/tests/face/tcp.cpp
+++ b/tests/face/tcp.cpp
@@ -18,11 +18,11 @@
 BOOST_AUTO_TEST_CASE(ChannelMap)
 {
   TcpFactory factory;
-  
+
   shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
   BOOST_CHECK_EQUAL(channel1, channel1a);
-  
+
   shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
   BOOST_CHECK_NE(channel1, channel2);
 }
@@ -39,7 +39,7 @@
       bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
     m_face1->onReceiveData +=
       bind(&EndToEndFixture::face1_onReceiveData, this, _1);
-    m_face1->onFail += 
+    m_face1->onFail +=
       bind(&EndToEndFixture::face1_onFail, this);
 
     this->afterIo();
@@ -52,7 +52,7 @@
 
     this->afterIo();
   }
-  
+
   void
   face1_onReceiveInterest(const Interest& interest)
   {
@@ -60,7 +60,7 @@
 
     this->afterIo();
   }
-  
+
   void
   face1_onReceiveData(const Data& data)
   {
@@ -85,7 +85,7 @@
       bind(&EndToEndFixture::face2_onReceiveInterest, this, _1);
     m_face2->onReceiveData +=
       bind(&EndToEndFixture::face2_onReceiveData, this, _1);
-    m_face2->onFail += 
+    m_face2->onFail +=
       bind(&EndToEndFixture::face2_onFail, this);
 
     this->afterIo();
@@ -98,7 +98,7 @@
 
     this->afterIo();
   }
-  
+
   void
   face2_onReceiveInterest(const Interest& interest)
   {
@@ -106,7 +106,7 @@
 
     this->afterIo();
   }
-  
+
   void
   face2_onReceiveData(const Data& data)
   {
@@ -142,14 +142,14 @@
   {
     BOOST_CHECK_EQUAL(m_faces.size(), shouldBe);
   }
-  
+
   void
   abortTestCase(const std::string& message)
   {
     g_io.stop();
     BOOST_FAIL(message);
   }
-  
+
 private:
   void
   afterIo()
@@ -180,13 +180,13 @@
     scheduler::schedule(time::seconds(10),
                         bind(&EndToEndFixture::abortTestCase, this,
                              "TcpChannel error: cannot connect or cannot accept connection"));
-  
+
   shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
-  
+
   channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated,   this, _1),
                    bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
-  
+
   channel2->connect("127.0.0.1", "20070",
                     bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
                     bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
@@ -207,7 +207,7 @@
   BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true);
 
   // integrated tests needs to check that TcpFace for non-loopback fails these tests...
-  
+
   abortEvent =
     scheduler::schedule(time::seconds(10),
                         bind(&EndToEndFixture::abortTestCase, this,
@@ -222,11 +222,11 @@
 
   ndn::SignatureSha256WithRsa fakeSignature;
   fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
-  
+
   // set fake signature on data1 and data2
   data1.setSignature(fakeSignature);
   data2.setSignature(fakeSignature);
-  
+
   m_face1->sendInterest(interest1);
   m_face1->sendData    (data1    );
   m_face2->sendInterest(interest2);
@@ -241,7 +241,7 @@
   BOOST_REQUIRE_EQUAL(m_face1_receivedDatas    .size(), 1);
   BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1);
   BOOST_REQUIRE_EQUAL(m_face2_receivedDatas    .size(), 1);
-  
+
   BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName());
   BOOST_CHECK_EQUAL(m_face1_receivedDatas    [0].getName(), data2.getName());
   BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName());
@@ -256,13 +256,13 @@
     scheduler::schedule(time::seconds(10),
                         bind(&EndToEndFixture::abortTestCase, this,
                              "TcpChannel error: cannot connect or cannot accept connection"));
-  
+
   shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<TcpChannel> channel2 = factory.createChannel("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),
@@ -274,7 +274,7 @@
   scheduler::cancel(abortEvent);
 
   BOOST_CHECK_EQUAL(m_faces.size(), 2);
-  
+
   shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072");
   channel3->connect("127.0.0.1", "20070",
                     bind(&EndToEndFixture::channel_onFaceCreated, this, _1),
@@ -285,7 +285,7 @@
   shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073");
 
   BOOST_CHECK_NE(channel3, channel4);
-  
+
   scheduler
     ::schedule
     (time::seconds(1),
@@ -297,16 +297,16 @@
           static_cast<TcpChannel::ConnectFailedCallback>(bind(&EndToEndFixture::
                                                               channel_onConnectFailed, this, _1)),
           time::seconds(4)));
-  
+
   m_ioRemaining = 4; // 2 connects and 2 accepts
-  abortEvent = 
+  abortEvent =
     scheduler::schedule(time::seconds(10),
                         bind(&EndToEndFixture::abortTestCase, this,
                              "TcpChannel error: cannot connect or cannot accept multiple connections"));
 
   scheduler::schedule(time::seconds(0.5),
                       bind(&EndToEndFixture::checkFaceList, this, 4));
-  
+
   g_io.run();
   g_io.reset();
   scheduler::cancel(abortEvent);
@@ -323,13 +323,13 @@
     scheduler::schedule(time::seconds(10),
                         bind(&EndToEndFixture::abortTestCase, this,
                              "TcpChannel error: cannot connect or cannot accept connection"));
-  
+
   shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
-  
+
   channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated,   this, _1),
                    bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
-  
+
   channel2->connect("127.0.0.1", "20070",
                     bind(&EndToEndFixture::channel2_onFaceCreated, this, _1),
                     bind(&EndToEndFixture::channel2_onConnectFailed, this, _1),
@@ -355,7 +355,7 @@
   m_ioRemaining = 2;
   // just double check that we are calling the virtual method
   static_pointer_cast<Face>(m_face1)->close();
-  
+
   BOOST_REQUIRE_NO_THROW(g_io.run());
   g_io.reset();
   scheduler::cancel(abortEvent);
@@ -363,7 +363,7 @@
   // both faces should get closed
   BOOST_CHECK(!static_cast<bool>(m_face1));
   BOOST_CHECK(!static_cast<bool>(m_face2));
-  
+
   BOOST_CHECK_EQUAL(channel1->size(), 0);
   BOOST_CHECK_EQUAL(channel2->size(), 0);
 }