Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "unix-stream-channel-factory.hpp" |
| 8 | |
| 9 | #if BOOST_VERSION >= 104800 |
| 10 | #include <boost/filesystem.hpp> // for canonical() |
| 11 | #endif |
| 12 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | UnixStreamChannelFactory::UnixStreamChannelFactory(boost::asio::io_service& ioService) |
| 16 | : m_ioService(ioService) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | shared_ptr<UnixStreamChannel> |
| 21 | UnixStreamChannelFactory::create(const std::string& unixSocketPath) |
| 22 | { |
| 23 | #if BOOST_VERSION >= 104800 |
| 24 | boost::filesystem::path p(unixSocketPath); |
| 25 | p = boost::filesystem::canonical(p.parent_path()) / p.filename(); |
| 26 | unix_stream::Endpoint endpoint(p.string()); |
| 27 | #else |
| 28 | unix_stream::Endpoint endpoint(unixSocketPath); |
| 29 | #endif |
| 30 | |
| 31 | shared_ptr<UnixStreamChannel> channel = find(endpoint); |
| 32 | if (channel) |
| 33 | return channel; |
| 34 | |
| 35 | channel = make_shared<UnixStreamChannel>(boost::ref(m_ioService), |
| 36 | boost::cref(endpoint)); |
| 37 | m_channels[endpoint] = channel; |
| 38 | return channel; |
| 39 | } |
| 40 | |
| 41 | shared_ptr<UnixStreamChannel> |
| 42 | UnixStreamChannelFactory::find(const unix_stream::Endpoint& endpoint) |
| 43 | { |
| 44 | ChannelMap::iterator i = m_channels.find(endpoint); |
| 45 | if (i != m_channels.end()) |
| 46 | return i->second; |
| 47 | else |
| 48 | return shared_ptr<UnixStreamChannel>(); |
| 49 | } |
| 50 | |
| 51 | } // namespace nfd |