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

#ifndef TANGO_SERVER_NOTIFDEVENTSUPPLIER_H
#define TANGO_SERVER_NOTIFDEVENTSUPPLIER_H

#include <tango/common/defines.h>
#include <tango/server/EventSupplier.h>

#if defined(_TG_WINDOWS_) && defined(_USRDLL) && !defined(_TANGO_LIB)
  #define USE_stub_in_nt_dll
#endif

#include <COS/CosNotification.hh>
#include <COS/CosNotifyChannelAdmin.hh>
#include <COS/CosNotifyComm.hh>

#if defined(_TG_WINDOWS_) && defined(_USRDLL) && !defined(_TANGO_LIB)
  #undef USE_stub_in_nt_dll
#endif

#include <string>
#include <vector>

namespace Tango
{

typedef struct _NotifService
{
    CosNotifyChannelAdmin::SupplierAdmin_var SupAdm;
    CosNotifyChannelAdmin::ProxyID pID;
    CosNotifyChannelAdmin::ProxyConsumer_var ProCon;
    CosNotifyChannelAdmin::StructuredProxyPushConsumer_var StrProPush;
    CosNotifyChannelAdmin::EventChannelFactory_var EveChaFac;
    CosNotifyChannelAdmin::EventChannel_var EveCha;
    std::string ec_ior;
} NotifService;

//---------------------------------------------------------------------
//
//              NotifdEventSupplier class
//
//---------------------------------------------------------------------

class NotifdEventSupplier : public EventSupplier, public POA_CosNotifyComm::StructuredPushSupplier
{
  public:
    static NotifdEventSupplier *create(CORBA::ORB_var, std::string, Util *);
    static void cleanup();

    void connect();
    void disconnect_structured_push_supplier() override;
    void disconnect_from_notifd();
    void subscription_change(const CosNotification::EventTypeSeq &added,
                             const CosNotification::EventTypeSeq &deled) override;

    void push_heartbeat_event() override;

    std::string &get_event_channel_ior()
    {
        return event_channel_ior;
    }

    void file_db_svr();

    //------------------ Push event -------------------------------

    void push_event(DeviceImpl *,
                    std::string,
                    const std::vector<std::string> &,
                    const std::vector<double> &,
                    const std::vector<std::string> &,
                    const std::vector<long> &,
                    const struct SuppliedEventData &,
                    const std::string &,
                    DevFailed *,
                    bool) override;

  protected:
    NotifdEventSupplier(CORBA::ORB_var,
                        CosNotifyChannelAdmin::SupplierAdmin_var,
                        CosNotifyChannelAdmin::ProxyID,
                        CosNotifyChannelAdmin::ProxyConsumer_var,
                        CosNotifyChannelAdmin::StructuredProxyPushConsumer_var,
                        CosNotifyChannelAdmin::EventChannelFactory_var,
                        CosNotifyChannelAdmin::EventChannel_var,
                        std::string &,
                        Util *);

  private:
    static NotifdEventSupplier *_instance;
    CosNotifyChannelAdmin::EventChannel_var eventChannel;
    CosNotifyChannelAdmin::SupplierAdmin_var supplierAdmin;
    CosNotifyChannelAdmin::ProxyID proxyId;
    CosNotifyChannelAdmin::ProxyConsumer_var proxyConsumer;
    CosNotifyChannelAdmin::StructuredProxyPushConsumer_var structuredProxyPushConsumer;
    CosNotifyChannelAdmin::EventChannelFactory_var eventChannelFactory;
    CORBA::ORB_var orb_;

    std::string event_channel_ior;

    void reconnect_notifd();
    static void connect_to_notifd(NotifService &, const CORBA::ORB_var &, const std::string &, Util *);
};
} // namespace Tango

#endif
