Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 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" |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 27 | #include "core/resolver.hpp" |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | |
| 31 | using namespace boost::asio; |
| 32 | |
| 33 | NFD_LOG_INIT("WebSocketFactory"); |
| 34 | |
| 35 | WebSocketFactory::WebSocketFactory(const std::string& defaultPort) |
| 36 | : m_defaultPort(defaultPort) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | shared_ptr<WebSocketChannel> |
| 41 | WebSocketFactory::createChannel(const websocket::Endpoint& endpoint) |
| 42 | { |
| 43 | shared_ptr<WebSocketChannel> channel = findChannel(endpoint); |
| 44 | if (static_cast<bool>(channel)) |
| 45 | return channel; |
| 46 | |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 47 | channel = make_shared<WebSocketChannel>(endpoint); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 48 | m_channels[endpoint] = channel; |
| 49 | |
| 50 | return channel; |
| 51 | } |
| 52 | |
| 53 | shared_ptr<WebSocketChannel> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 54 | WebSocketFactory::createChannel(const std::string& host, const std::string& port) |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 55 | { |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 56 | ip::tcp::endpoint tcpEndpoint = TcpResolver::syncResolve(host, port); |
| 57 | websocket::Endpoint endpoint(tcpEndpoint.address(), tcpEndpoint.port()); |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 58 | return createChannel(endpoint); |
| 59 | } |
| 60 | |
| 61 | shared_ptr<WebSocketChannel> |
| 62 | WebSocketFactory::findChannel(const websocket::Endpoint& localEndpoint) |
| 63 | { |
| 64 | ChannelMap::iterator i = m_channels.find(localEndpoint); |
| 65 | if (i != m_channels.end()) |
| 66 | return i->second; |
| 67 | else |
| 68 | return shared_ptr<WebSocketChannel>(); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | WebSocketFactory::createFace(const FaceUri& uri, |
| 73 | const FaceCreatedCallback& onCreated, |
| 74 | const FaceConnectFailedCallback& onConnectFailed) |
| 75 | { |
| 76 | throw Error("WebSocketFactory does not support 'createFace' operation"); |
| 77 | } |
| 78 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 79 | std::list<shared_ptr<const Channel> > |
| 80 | WebSocketFactory::getChannels() const |
| 81 | { |
| 82 | std::list<shared_ptr<const Channel> > channels; |
| 83 | for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i) |
| 84 | { |
| 85 | channels.push_back(i->second); |
| 86 | } |
| 87 | |
| 88 | return channels; |
| 89 | } |
| 90 | |
Wentao Shang | 53df163 | 2014-04-21 12:01:32 -0700 | [diff] [blame] | 91 | } // namespace nfd |