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 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 7 | #include "unix-stream-factory.hpp" |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 8 | #include "core/global-io.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 9 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 10 | #include <boost/filesystem.hpp> // for canonical() |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
| 13 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 14 | shared_ptr<UnixStreamChannel> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 15 | UnixStreamFactory::create(const std::string& unixSocketPath) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 16 | { |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 17 | boost::filesystem::path p(unixSocketPath); |
| 18 | p = boost::filesystem::canonical(p.parent_path()) / p.filename(); |
| 19 | unix_stream::Endpoint endpoint(p.string()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 20 | |
| 21 | shared_ptr<UnixStreamChannel> channel = find(endpoint); |
| 22 | if (channel) |
| 23 | return channel; |
| 24 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 25 | channel = make_shared<UnixStreamChannel>(boost::ref(getGlobalIoService()), |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 26 | boost::cref(endpoint)); |
| 27 | m_channels[endpoint] = channel; |
| 28 | return channel; |
| 29 | } |
| 30 | |
| 31 | shared_ptr<UnixStreamChannel> |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame^] | 32 | UnixStreamFactory::find(const unix_stream::Endpoint& endpoint) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 33 | { |
| 34 | ChannelMap::iterator i = m_channels.find(endpoint); |
| 35 | if (i != m_channels.end()) |
| 36 | return i->second; |
| 37 | else |
| 38 | return shared_ptr<UnixStreamChannel>(); |
| 39 | } |
| 40 | |
| 41 | } // namespace nfd |