/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2018,  Regents of the University of California,
 *                           Arizona Board of Regents,
 *                           Colorado State University,
 *                           University Pierre & Marie Curie, Sorbonne University,
 *                           Washington University in St. Louis,
 *                           Beijing Institute of Technology,
 *                           The University of Memphis.
 *
 * This file is part of NFD (Named Data Networking Forwarding Daemon).
 * See AUTHORS.md for complete list of NFD authors and contributors.
 *
 * NFD is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "websocket-factory.hpp"

namespace nfd {
namespace face {

namespace ip = boost::asio::ip;

NFD_LOG_INIT(WebSocketFactory);
NFD_REGISTER_PROTOCOL_FACTORY(WebSocketFactory);

const std::string&
WebSocketFactory::getId()
{
  static std::string id("websocket");
  return id;
}

WebSocketFactory::WebSocketFactory(const CtorParams& params)
  : ProtocolFactory(params)
{
}

void
WebSocketFactory::processConfig(OptionalConfigSection configSection,
                                FaceSystem::ConfigContext& context)
{
  // websocket
  // {
  //   listen yes
  //   port 9696
  //   enable_v4 yes
  //   enable_v6 yes
  // }

  bool wantListen = false;
  uint16_t port = 9696;
  bool enableV4 = true;
  bool enableV6 = true;

  if (configSection) {
    wantListen = true;
    for (const auto& pair : *configSection) {
      const std::string& key = pair.first;

      if (key == "listen") {
        wantListen = ConfigFile::parseYesNo(pair, "face_system.websocket");
      }
      else if (key == "port") {
        port = ConfigFile::parseNumber<uint16_t>(pair, "face_system.websocket");
      }
      else if (key == "enable_v4") {
        enableV4 = ConfigFile::parseYesNo(pair, "face_system.websocket");
      }
      else if (key == "enable_v6") {
        enableV6 = ConfigFile::parseYesNo(pair, "face_system.websocket");
      }
      else {
        BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.websocket." + key));
      }
    }
  }

  if (!enableV4 && !enableV6) {
    BOOST_THROW_EXCEPTION(ConfigFile::Error(
      "IPv4 and IPv6 WebSocket channels have been disabled. Remove face_system.websocket section "
      "to disable WebSocket channels or enable at least one channel type."));
  }

  if (!enableV4 && enableV6) {
    // websocketpp's IPv6 socket always accepts IPv4 connections.
    BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD does not allow pure IPv6 WebSocket channel."));
  }

  if (!context.isDryRun) {
    if (!wantListen) {
      if (!m_channels.empty()) {
        NFD_LOG_WARN("Cannot close WebSocket channel after initialization");
      }
      return;
    }

    BOOST_ASSERT(enableV4);
    websocket::Endpoint endpoint(enableV6 ? ip::tcp::v6() : ip::tcp::v4(), port);

    auto channel = this->createChannel(endpoint);
    if (!channel->isListening()) {
      channel->listen(this->addFace);
      if (m_channels.size() > 1) {
        NFD_LOG_WARN("Adding WebSocket channel for new endpoint; cannot close existing channels");
      }
    }
  }
}

void
WebSocketFactory::createFace(const CreateFaceRequest& req,
                             const FaceCreatedCallback& onCreated,
                             const FaceCreationFailedCallback& onFailure)
{
  onFailure(406, "Unsupported protocol");
}

shared_ptr<WebSocketChannel>
WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
{
  auto it = m_channels.find(endpoint);
  if (it != m_channels.end())
    return it->second;

  auto channel = make_shared<WebSocketChannel>(endpoint);
  m_channels[endpoint] = channel;

  return channel;
}

std::vector<shared_ptr<const Channel>>
WebSocketFactory::getChannels() const
{
  return getChannelsFromMap(m_channels);
}

} // namespace face
} // namespace nfd
