/*
  broker-protocol.cpp

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

#include "broker-protocol.h"
#include "rdmnet.h"

namespace RDMnet {
namespace BrokerProtocol {

/**
 * @brief BrokerProtocol::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(2)
{
}


/**
 * @brief BrokerProtocol::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);                 // flags, length, and vector
  // has no header                              // header

  switch (vector())
    {
    case VECTOR_BROKER_CONNECT:
      createData<client_connect_data>();        // data
      break;
    case VECTOR_BROKER_CONNECT_REPLY:
      createData<connect_reply_data>();         // data
      break;
    case VECTOR_BROKER_CLIENT_ENTRY_UPDATE:
      createData<client_entry_update_data>();   // data
      break;
    case VECTOR_BROKER_REDIRECT_V4:
      createData<client_redirect_ipv4_data>();  // data
      break;
    case VECTOR_BROKER_REDIRECT_V6:
      createData<client_redirect_ipv6_data>();  // data
      break;
    case VECTOR_BROKER_FETCH_CLIENT_LIST:
      break;
    case VECTOR_BROKER_CONNECTED_CLIENT_LIST:
      createDataBlock<ClientEntry::Pdu>();      // data
      break;
    case VECTOR_BROKER_CLIENT_ADD:
      createDataBlock<ClientEntry::Pdu>();      // data
      break;
    case VECTOR_BROKER_CLIENT_REMOVE:
      createDataBlock<ClientEntry::Pdu>();      // data
      break;
    case VECTOR_BROKER_CLIENT_ENTRY_CHANGE:
      createDataBlock<ClientEntry::Pdu>();      // data
      break;
    case VECTOR_BROKER_REQUEST_DYNAMIC_UIDS:
      createData<request_dynamic_uid_data>();   // data
      break;
    case VECTOR_BROKER_ASSIGNED_DYNAMIC_UIDS:
      createData<dynamic_uid_assignment_data>();  // data
      break;
    case VECTOR_BROKER_FETCH_DYNAMIC_UID_LIST:
      createData<fetch_dynamcic_assignment_data>(); // data
      break;
    case VECTOR_BROKER_DISCONNECT:
      createData<disconnect_data>();            // data
      break;
    case VECTOR_BROKER_NULL:
      break;
  }
}


/**
 * @brief client_connect_data::iStream
 * @param stream
 */
void client_connect_data::iStream(ACN::PDU::Stream stream)
{
  stream->readString(clientScope, 63);
  *stream >> version;
  stream->readString(searchDomain, 231);
  *stream >> flags.byte;
  *stream >> *pdu;
}


/**
 * @brief client_connect_data::oStream
 * @param stream
 */
void client_connect_data::oStream(ACN::PDU::Stream stream) const
{
  stream->writeString(clientScope, 63);
  *stream << version;
  stream->writeString(searchDomain, 231);
  *stream << flags.byte;
  *stream << pdu;
}


/**
 * @brief connect_reply_data::iStream
 * @param stream
 */
void connect_reply_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> code;
  *stream >> version;
  *stream >> broker_uid;
  *stream >> client_uid;
}


/**
 * @brief connect_reply_data::oStream
 * @param stream
 */
void connect_reply_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << code;
  *stream << version;
  *stream << broker_uid;
  *stream << client_uid;
}


/**
 * @brief client_entry_update_data::iStream
 * @param stream
 */
void client_entry_update_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> flags.byte;
  *stream >> *pdu;
}


/**
 * @brief client_entry_update_data::oStream
 * @param stream
 */
void client_entry_update_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << flags.byte;
  *stream << pdu;
}


/**
 * @brief client_redirect_ipv4_data::iStream
 * @param stream
 */
void client_redirect_ipv4_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> ip;
  *stream >> port;
}


/**
 * @brief client_redirect_ipv4_data::oStream
 * @param stream
 */
void client_redirect_ipv4_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << ip;
  *stream << port;
}


/**
 * @brief client_redirect_ipv6_data::iStream
 * @param stream
 */
void client_redirect_ipv6_data::iStream(ACN::PDU::Stream stream)
{
  stream->read(ip, sizeof(ip));
  if (stream->gcount() != sizeof(ip))
    return stream->setstate(std::ios_base::failbit);
  *stream >> port;
}


/**
 * @brief client_redirect_ipv4_data::oStream
 * @param stream
 */
void client_redirect_ipv6_data::oStream(ACN::PDU::Stream stream) const
{
  stream->write(ip, sizeof(ip));
  *stream << port;
}


/**
 * @brief dynamic_uid_request::iStream
 * @param stream
 */
void dynamic_uid_request::iStream(ACN::PDU::Stream stream)
{
  *stream >> uid;
  *stream >> rid;
}


/**
 * @brief dynamic_uid_request::oStream
 * @param stream
 */
void dynamic_uid_request::oStream(ACN::PDU::Stream stream) const
{
  *stream << uid;
  *stream << rid;
}


/**
 * @brief request_dynamic_uid_data::iStream
 * @param stream
 */
void request_dynamic_uid_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    {
      list.emplace_back(dynamic_uid_request());
      *stream >> list.back();
    }
}


/**
 * @brief request_dynamic_uid_data::oStream
 * @param stream
 */
void request_dynamic_uid_data::oStream(ACN::PDU::Stream stream) const
{
  for (const auto & r : list)
    *stream << r;
}


/**
 * @brief dynamic_uid_mapping::iStream
 * @param stream
 */
void dynamic_uid_mapping::iStream(ACN::PDU::Stream stream)
{
  *stream >> uid;
  *stream >> rid;
  *stream >> status;
}


/**
 * @brief dynamic_uid_mapping::oStream
 * @param stream
 */
void dynamic_uid_mapping::oStream(ACN::PDU::Stream stream) const
{
  *stream << uid;
  *stream << rid;
  *stream << status;
}


/**
 * @brief dynamic_uid_assignment_data::iStream
 * @param stream
 */
void dynamic_uid_assignment_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    {
      list.emplace_back(dynamic_uid_mapping());
      *stream >> list.back();
    }
}


/**
 * @brief dynamic_uid_assignment_data::oStream
 * @param stream
 */
void dynamic_uid_assignment_data::oStream(ACN::PDU::Stream stream) const
{
  for (const auto & r : list)
    *stream << r;
}


/**
 * @brief fetch_dynamcic_assignment_data::iStream
 * @param stream
 */
void fetch_dynamcic_assignment_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    {
      list.emplace_back(RDM::UID());
      *stream >> list.back();
    }
}


/**
 * @brief fetch_dynamcic_assignment_data::oStream
 * @param stream
 */
void fetch_dynamcic_assignment_data::oStream(ACN::PDU::Stream stream) const
{
  for (const auto & id : list)
    *stream << id;
}


namespace ClientEntry {

/**
 * @brief ClientEntry::Pdu::ClientEntryPdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(4)
{
}


/**
 * @brief Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);           // flags, length, and vector
  createHeader<entry_header>();     // header

  switch (vector())
    {
    case CLIENT_PROTOCOL_RPT:
      createData<entry_rpt_data>();  // data
      break;
    case CLIENT_PROTOCOL_EPT:
      createData<entry_ept_data>();  // data
      break;
    default:
      break;
    }
}


/**
 * @brief ClientEntry_header::streamSize
 * @return
 */
size_t entry_header::streamSize() const
{
    return UUID_LENGTH;
}


/**
 * @brief header::iStream
 * @param stream
 */
void entry_header::iStream(ACN::PDU::Stream stream)
{
  *stream >> cid;
}


/**
 * @brief ClientEntry_header::oStream
 * @param stream
 */
void entry_header::oStream(ACN::PDU::Stream stream) const
{
  *stream << cid;
}


/**
 * @brief rpt_data::iStream
 * @param stream
 */
void entry_rpt_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> client_uid;
  *stream >> client_type;
  *stream >> binding_cid;
}


/**
 * @brief rpt_data::oStream
 * @param stream
 */
void entry_rpt_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << client_uid;
  *stream << client_type;
  *stream << binding_cid;
}


/**
 * @brief ept_data::iStream
 * @param stream
 */
void entry_ept_data::iStream(ACN::PDU::Stream stream)
{
  while (stream->good())
    subprotocol_list.push_back(stream->readType<uint8_t>());
}


/**
 * @brief ept_data::oStream
 * @param stream
 */
void entry_ept_data::oStream(ACN::PDU::Stream stream) const
{
  for (const auto & v : subprotocol_list)
    *stream << v;
}


} // namespace ClientEntry
} // namespace BrokerProtocol
} // namespace RDMnet
