// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/TaNDiagnosticsDispatcherFactory.cpp 976 2018-10-31 09:25:44Z depa $
#include "tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/TaNDiagnosticsDispatcherFactory.h"

#include "tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/DiagnosticRecordDispatcher.h"
#include "tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/PropertyName.h"

#include "tiptilt_ctrl/bcu/util/Assertions.h"
// #include "tiptilt_ctrl/bcu/util/SubversionId.h"

#include <Basda/Microgate/Diagnostics/DiagnosticRecordReceiverImpl.h>

#include <boost/numeric/conversion/cast.hpp>
#include <string>

using std::string;

namespace Sharknir { namespace Bcu {

TaNDiagnosticsDispatcherFactory::TaNDiagnosticsDispatcherFactory(
        const Nice::PropertyPtr& diagDestinationUdpPort,
        const OverallDiagnosticRecordBufferPtr& diagBuffer) :
    diagDestinationUdpPort_(diagDestinationUdpPort),
    diagBuffer_(diagBuffer)
{
}

DiagnosticsDispatcherPtr TaNDiagnosticsDispatcherFactory::build()
{
    SHARKNIR_PRECONDITION(0 != diagDestinationUdpPort_,
            "UDP port property is given");
    Basda::Microgate::DiagnosticRecordReceiverImpl::Config config;
    const std::string dontCareHostName= "1.2.3.4";
    const uint16_t FIXED_BCU_SOURCE_UDP_PORT= 10001;
    const uint16_t destinationUdpPort= boost::numeric_cast<uint16_t>(
            diagDestinationUdpPort_->get<int>());
    config.setRemoteHostName(dontCareHostName).
           setRemoteUdpPort(FIXED_BCU_SOURCE_UDP_PORT).
           setLocalUdpPort(destinationUdpPort).
           setReceiverBufferSize(1024*1024).
           verify();
    Basda::Microgate::DiagnosticRecordReceiverPtr receiver=
            Basda::Microgate::DiagnosticRecordReceiverImpl::create(config);

    return DiagnosticsDispatcherPtr(
            new DiagnosticRecordDispatcher("Slope BCU record dispatcher",
                    receiver, diagBuffer_));
}

}
}
