blob: bcb3970722e693bb46dc89eaad50d21afa8c7583 [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
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020030namespace ip = boost::asio::ip;
Wentao Shang53df1632014-04-21 12:01:32 -070031
32shared_ptr<WebSocketChannel>
33WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
34{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020035 auto channel = findChannel(endpoint);
36 if (channel)
Wentao Shang53df1632014-04-21 12:01:32 -070037 return channel;
38
Alexander Afanasyevf6980282014-05-13 18:28:40 -070039 channel = make_shared<WebSocketChannel>(endpoint);
Wentao Shang53df1632014-04-21 12:01:32 -070040 m_channels[endpoint] = channel;
41
42 return channel;
43}
44
45shared_ptr<WebSocketChannel>
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020046WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort)
Wentao Shang53df1632014-04-21 12:01:32 -070047{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020048 websocket::Endpoint endpoint(ip::address::from_string(localIp),
49 boost::lexical_cast<uint16_t>(localPort));
Wentao Shang53df1632014-04-21 12:01:32 -070050 return createChannel(endpoint);
51}
52
Wentao Shang53df1632014-04-21 12:01:32 -070053void
54WebSocketFactory::createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +080055 ndn::nfd::FacePersistency persistency,
Wentao Shang53df1632014-04-21 12:01:32 -070056 const FaceCreatedCallback& onCreated,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020057 const FaceCreationFailedCallback& onConnectFailed)
Wentao Shang53df1632014-04-21 12:01:32 -070058{
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070059 BOOST_THROW_EXCEPTION(Error("WebSocketFactory does not support 'createFace' operation"));
Wentao Shang53df1632014-04-21 12:01:32 -070060}
61
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020062std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -060063WebSocketFactory::getChannels() const
64{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020065 std::vector<shared_ptr<const Channel>> channels;
66 channels.reserve(m_channels.size());
67
68 for (const auto& i : m_channels)
69 channels.push_back(i.second);
Steve DiBenedettoef04f272014-06-04 14:28:31 -060070
71 return channels;
72}
73
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020074shared_ptr<WebSocketChannel>
75WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const
76{
77 auto i = m_channels.find(endpoint);
78 if (i != m_channels.end())
79 return i->second;
80 else
81 return nullptr;
82}
83
Wentao Shang53df1632014-04-21 12:01:32 -070084} // namespace nfd