Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Chengyu Fan | 4381fb6 | 2015-01-14 11:37:04 -0700 | [diff] [blame] | 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 Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 25 | |
| 26 | #include "websocket-factory.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 30 | namespace ip = boost::asio::ip; |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 31 | |
| 32 | shared_ptr<WebSocketChannel> |
| 33 | WebSocketFactory::createChannel(const websocket::Endpoint& endpoint) |
| 34 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 35 | auto channel = findChannel(endpoint); |
| 36 | if (channel) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 37 | return channel; |
| 38 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 39 | channel = make_shared<WebSocketChannel>(endpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 40 | m_channels[endpoint] = channel; |
| 41 | |
| 42 | return channel; |
| 43 | } |
| 44 | |
| 45 | shared_ptr<WebSocketChannel> |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 46 | WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 47 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 48 | websocket::Endpoint endpoint(ip::address::from_string(localIp), |
| 49 | boost::lexical_cast<uint16_t>(localPort)); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 50 | return createChannel(endpoint); |
| 51 | } |
| 52 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 53 | void |
| 54 | WebSocketFactory::createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 55 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 56 | bool wantLocalFieldsEnabled, |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 57 | const FaceCreatedCallback& onCreated, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 58 | const FaceCreationFailedCallback& onFailure) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 59 | { |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame^] | 60 | onFailure(406, "Unsupported protocol"); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 63 | std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 64 | WebSocketFactory::getChannels() const |
| 65 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 66 | std::vector<shared_ptr<const Channel>> channels; |
| 67 | channels.reserve(m_channels.size()); |
| 68 | |
| 69 | for (const auto& i : m_channels) |
| 70 | channels.push_back(i.second); |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 71 | |
| 72 | return channels; |
| 73 | } |
| 74 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 75 | shared_ptr<WebSocketChannel> |
| 76 | WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const |
| 77 | { |
| 78 | auto i = m_channels.find(endpoint); |
| 79 | if (i != m_channels.end()) |
| 80 | return i->second; |
| 81 | else |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 85 | } // namespace nfd |