blob: d4c8d3ffe9aff6d337ed27ba64bf74f087b067cd [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"
27
28namespace nfd {
29
30using namespace boost::asio;
31
32NFD_LOG_INIT("WebSocketFactory");
33
34WebSocketFactory::WebSocketFactory(const std::string& defaultPort)
35 : m_defaultPort(defaultPort)
36{
37}
38
39shared_ptr<WebSocketChannel>
40WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
41{
42 shared_ptr<WebSocketChannel> channel = findChannel(endpoint);
43 if (static_cast<bool>(channel))
44 return channel;
45
Alexander Afanasyevf6980282014-05-13 18:28:40 -070046 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -070047 m_channels[endpoint] = channel;
48
49 return channel;
50}
51
52shared_ptr<WebSocketChannel>
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080053WebSocketFactory::createChannel(const std::string& localIp, const std::string& port)
Wentao Shang53df1632014-04-21 12:01:32 -070054{
Alexander Afanasyev0e156df2015-01-26 22:33:43 -080055 using namespace boost::asio::ip;
56 websocket::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(port));
Wentao Shang53df1632014-04-21 12:01:32 -070057 return createChannel(endpoint);
58}
59
60shared_ptr<WebSocketChannel>
61WebSocketFactory::findChannel(const websocket::Endpoint& localEndpoint)
62{
63 ChannelMap::iterator i = m_channels.find(localEndpoint);
64 if (i != m_channels.end())
65 return i->second;
66 else
67 return shared_ptr<WebSocketChannel>();
68}
69
70void
71WebSocketFactory::createFace(const FaceUri& uri,
72 const FaceCreatedCallback& onCreated,
73 const FaceConnectFailedCallback& onConnectFailed)
74{
75 throw Error("WebSocketFactory does not support 'createFace' operation");
76}
77
Steve DiBenedettoef04f272014-06-04 14:28:31 -060078std::list<shared_ptr<const Channel> >
79WebSocketFactory::getChannels() const
80{
81 std::list<shared_ptr<const Channel> > channels;
82 for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
83 {
84 channels.push_back(i->second);
85 }
86
87 return channels;
88}
89
Wentao Shang53df1632014-04-21 12:01:32 -070090} // namespace nfd