face/tcp: Add map of remoteEndpoint-Face within channel

If connect operation requested twice for the same remote endpoint (and
the first connection was successful), the second request will call
FaceCreated callback immediately (or after endpoint resolution) with the
previously created face.

refs: #1132, #1134

Change-Id: I5f7e5237a42289a96d7fd9d16bda47fc7cda4dfc
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index bbefa44..9da453e 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -42,6 +42,12 @@
                     const TcpChannel::ConnectFailedCallback& onConnectFailed,
                     const time::Duration& timeout/* = time::seconds(4)*/)
 {
+  ChannelFaceMap::iterator i = m_channelFaces.find(remoteEndpoint);
+  if (i != m_channelFaces.end()) {
+    onFaceCreated(i->second);
+    return;
+  }
+
   shared_ptr<ip::tcp::socket> clientSocket =
     make_shared<ip::tcp::socket>(boost::ref(m_ioService));
 
@@ -116,6 +122,9 @@
    */
   shared_ptr<TcpFace> face = make_shared<TcpFace>(1, boost::cref(socket));
   onFaceCreated(face);
+
+  tcp::Endpoint remoteEndpoint = socket->remote_endpoint();
+  m_channelFaces[remoteEndpoint] = face;
 }
 
 void
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index 64596d0..8536203 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -118,6 +118,9 @@
   boost::asio::io_service& m_ioService;
   tcp::Endpoint m_localEndpoint;
 
+  typedef std::map< tcp::Endpoint, shared_ptr<TcpFace> > ChannelFaceMap;
+  ChannelFaceMap m_channelFaces;
+
   bool isListening;
   shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptor;
 };