blob: ededecd82dd91d364ee0f443c1413c2adaf3b4dc [file] [log] [blame]
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +01001/* -*- 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 Pesaventobc4dd8c2014-02-14 20:01:01 +01009#include <boost/filesystem.hpp> // for canonical()
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010010
11namespace nfd {
12
13UnixStreamChannelFactory::UnixStreamChannelFactory(boost::asio::io_service& ioService)
14 : m_ioService(ioService)
15{
16}
17
18shared_ptr<UnixStreamChannel>
19UnixStreamChannelFactory::create(const std::string& unixSocketPath)
20{
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010021 boost::filesystem::path p(unixSocketPath);
22 p = boost::filesystem::canonical(p.parent_path()) / p.filename();
23 unix_stream::Endpoint endpoint(p.string());
Davide Pesaventobc4dd8c2014-02-14 20:01:01 +010024
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
35shared_ptr<UnixStreamChannel>
36UnixStreamChannelFactory::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