face/tcp: Implementing accepting and establishing connections with TCP Channel

refs: #1132, #1133, #1134, #1135

Change-Id: I8878784059fd962187bfcdd23b5adcaf9135cf4e
diff --git a/daemon/face/tcp-channel-factory.cpp b/daemon/face/tcp-channel-factory.cpp
index 8ec1c3f..4c6df87 100644
--- a/daemon/face/tcp-channel-factory.cpp
+++ b/daemon/face/tcp-channel-factory.cpp
@@ -8,22 +8,49 @@
 
 namespace ndn {
 
+TcpChannelFactory::TcpChannelFactory(boost::asio::io_service& ioService)
+  : m_ioService(ioService)
+{
+}
+
 shared_ptr<TcpChannel>
 TcpChannelFactory::create(const tcp::Endpoint& endpoint)
 {
-  return shared_ptr<ndn::TcpChannel>();
+  shared_ptr<TcpChannel> channel = find(endpoint);
+  if(static_cast<bool>(channel))
+    return channel;
+
+  channel = make_shared<TcpChannel>(boost::ref(m_ioService), boost::cref(endpoint));
+  m_channels[endpoint] = channel;
+  return channel;
 }
 
 shared_ptr<TcpChannel>
 TcpChannelFactory::create(const std::string& localHost, const std::string& localPort)
 {
-  return shared_ptr<ndn::TcpChannel>();
+  using boost::asio::ip::tcp;
+  
+  tcp::resolver::query query(localHost, localPort);
+  // shared_ptr<tcp::resolver> resolver =
+  //   make_shared<tcp::resolver>(boost::ref(m_ioService));
+  tcp::resolver resolver(m_ioService);
+
+  tcp::resolver::iterator end;
+  tcp::resolver::iterator i = resolver.resolve(query);
+  if (i == end)
+    return shared_ptr<ndn::TcpChannel>();
+
+  return create(*i);
 }
 
 shared_ptr<TcpChannel>
 TcpChannelFactory::find(const tcp::Endpoint& localEndpoint)
 {
-  return shared_ptr<ndn::TcpChannel>();
+  ChannelMap::iterator i = m_channels.find(localEndpoint);
+  if (i != m_channels.end())
+    return i->second;
+  else
+    return shared_ptr<ndn::TcpChannel>();
 }
 
 } // namespace ndn