blob: 49f28b057d334760de87fea6e39f36088436c053 [file] [log] [blame]
Wentao Shang53df1632014-04-21 12:01:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Chengyu Fan4381fb62015-01-14 11:37:04 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Wentao Shang53df1632014-04-21 12:01:32 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Wentao Shang53df1632014-04-21 12:01:32 -070025
26#include "websocket-factory.hpp"
Chengyu Fan4381fb62015-01-14 11:37:04 -070027#include <ndn-cxx/util/dns.hpp>
Wentao Shang53df1632014-04-21 12:01:32 -070028
29namespace nfd {
30
31using namespace boost::asio;
32
33NFD_LOG_INIT("WebSocketFactory");
34
35WebSocketFactory::WebSocketFactory(const std::string& defaultPort)
36 : m_defaultPort(defaultPort)
37{
38}
39
40shared_ptr<WebSocketChannel>
41WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
42{
43 shared_ptr<WebSocketChannel> channel = findChannel(endpoint);
44 if (static_cast<bool>(channel))
45 return channel;
46
Alexander Afanasyevf6980282014-05-13 18:28:40 -070047 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -070048 m_channels[endpoint] = channel;
49
50 return channel;
51}
52
53shared_ptr<WebSocketChannel>
Steve DiBenedettoef04f272014-06-04 14:28:31 -060054WebSocketFactory::createChannel(const std::string& host, const std::string& port)
Wentao Shang53df1632014-04-21 12:01:32 -070055{
Chengyu Fan4381fb62015-01-14 11:37:04 -070056 ip::tcp::endpoint tcpEndpoint(ndn::dns::syncResolve(host, getGlobalIoService()),
57 boost::lexical_cast<uint16_t>(port));
Steve DiBenedettoef04f272014-06-04 14:28:31 -060058 websocket::Endpoint endpoint(tcpEndpoint.address(), tcpEndpoint.port());
Wentao Shang53df1632014-04-21 12:01:32 -070059 return createChannel(endpoint);
60}
61
62shared_ptr<WebSocketChannel>
63WebSocketFactory::findChannel(const websocket::Endpoint& localEndpoint)
64{
65 ChannelMap::iterator i = m_channels.find(localEndpoint);
66 if (i != m_channels.end())
67 return i->second;
68 else
69 return shared_ptr<WebSocketChannel>();
70}
71
72void
73WebSocketFactory::createFace(const FaceUri& uri,
74 const FaceCreatedCallback& onCreated,
75 const FaceConnectFailedCallback& onConnectFailed)
76{
77 throw Error("WebSocketFactory does not support 'createFace' operation");
78}
79
Steve DiBenedettoef04f272014-06-04 14:28:31 -060080std::list<shared_ptr<const Channel> >
81WebSocketFactory::getChannels() const
82{
83 std::list<shared_ptr<const Channel> > channels;
84 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
85 {
86 channels.push_back(i->second);
87 }
88
89 return channels;
90}
91
Wentao Shang53df1632014-04-21 12:01:32 -070092} // namespace nfd