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 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 9 | #include <boost/filesystem.hpp> // for canonical() |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 10 | |
| 11 | namespace nfd { |
| 12 | |
| 13 | UnixStreamChannelFactory::UnixStreamChannelFactory(boost::asio::io_service& ioService) |
| 14 | : m_ioService(ioService) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | shared_ptr<UnixStreamChannel> |
| 19 | UnixStreamChannelFactory::create(const std::string& unixSocketPath) |
| 20 | { |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 21 | boost::filesystem::path p(unixSocketPath); |
| 22 | p = boost::filesystem::canonical(p.parent_path()) / p.filename(); |
| 23 | unix_stream::Endpoint endpoint(p.string()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 24 | |
| 25 | shared_ptr<UnixStreamChannel> channel = find(endpoint); |
| 26 | if (channel) |
| 27 | return channel; |
| 28 | |
| 29 | channel = make_shared<UnixStreamChannel>(boost::ref(m_ioService), |
| 30 | boost::cref(endpoint)); |
| 31 | m_channels[endpoint] = channel; |
| 32 | return channel; |
| 33 | } |
| 34 | |
| 35 | shared_ptr<UnixStreamChannel> |
| 36 | UnixStreamChannelFactory::find(const unix_stream::Endpoint& endpoint) |
| 37 | { |
| 38 | ChannelMap::iterator i = m_channels.find(endpoint); |
| 39 | if (i != m_channels.end()) |
| 40 | return i->second; |
| 41 | else |
| 42 | return shared_ptr<UnixStreamChannel>(); |
| 43 | } |
| 44 | |
| 45 | } // namespace nfd |