/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef TANGO_SERVER_BLACKBOX_H
#define TANGO_SERVER_BLACKBOX_H

#include <tango/common/defines.h>
#include <tango/common/tango_const.h>
#include <tango/server/BlackBoxElt.h>
#include <tango/server/client_addr.h>

#ifdef _TG_WINDOWS_
  #include <winsock2.h>
#endif

#include <chrono>
#include <string>
#include <vector>

namespace Tango
{

//==================================================================================================================
//
//            The BlackBox class
//
// description :
//        Class to implement the black box itself. This is mainly a vector of BlackBoxElt managed as a circular vector
//
//===================================================================================================================

class BlackBox
{
  public:
    BlackBox();
    BlackBox(long);

    void insert_corba_attr(BlackBoxElt_AttrType);
    void insert_cmd(const char *, long vers = 1, DevSource = Tango::DEV);
    void insert_attr(const Tango::DevVarStringArray &, long vers = 1, DevSource = Tango::DEV);
    void insert_attr(const Tango::DevVarStringArray &, const ClntIdent &, long vers = 1, DevSource = Tango::DEV);
    void insert_attr(const char *, const ClntIdent &, long);
    void insert_attr(const Tango::AttributeValueList &, long vers = 1);
    void insert_attr(const Tango::AttributeValueList_4 &, const ClntIdent &, long vers);
    void insert_attr(const Tango::DevPipeData &, const ClntIdent &, long vers);
    void insert_wr_attr(const Tango::AttributeValueList_4 &,
                        const Tango::DevVarStringArray &,
                        const ClntIdent &,
                        long vers);
    void insert_op(BlackBoxElt_OpType);
    void insert_op(BlackBoxElt_OpType, const ClntIdent &);

    void insert_cmd_nl(const char *, long, DevSource);
    void insert_cmd_cl_ident(const char *, const ClntIdent &, long vers = 1, DevSource = Tango::DEV);
    void add_cl_ident(const ClntIdent &, client_addr *);
    void update_client_host(client_addr *);

    Tango::DevVarStringArray *read(long);

  private:
    void inc_indexes();
    void get_client_host();
    void build_info_as_str(long);
    std::string timestamp_unix_to_str(const std::chrono::system_clock::time_point &);
    void add_source(long);
    void insert_op_nl(BlackBoxElt_OpType);
    void insert_attr_nl(const Tango::AttributeValueList &, long);
    void insert_attr_nl_4(const Tango::AttributeValueList_4 &);
    void insert_attr_wr_nl(const Tango::AttributeValueList_4 &, const Tango::DevVarStringArray &, long);

    std::vector<BlackBoxElt> box;
    long insert_elt;
    long nb_elt;
    long max_elt;

    omni_mutex sync;

    std::string elt_str;
};

} // namespace Tango

#endif /* _BLACKBOX_ */
