/*++

Program name:

  dm

Module Name:

  WebService.hpp

Notices:

  WebService - Deal Module Web Service

Author:

  Copyright (c) Prepodobny Alen

  mailto: alienufo@inbox.ru
  mailto: ufocomp@gmail.com

--*/

#ifndef DM_WEB_SERVICE_MODULE_HPP
#define DM_WEB_SERVICE_MODULE_HPP
//----------------------------------------------------------------------------------------------------------------------

#include "Common.hpp"
//----------------------------------------------------------------------------------------------------------------------

extern "C++" {

namespace Apostol {

    namespace Module {

        //--------------------------------------------------------------------------------------------------------------

        //-- CWebService -----------------------------------------------------------------------------------------------

        //--------------------------------------------------------------------------------------------------------------

        class CWebService: public CCustomModule {
        private:

            CContextPair m_DefaultServer { BPS_BM_SERVER_ADDRESS, CContext(BPS_BM_SERVER_ADDRESS, CLocation(BPS_SERVER_URL)) };
            CContextList m_Servers {};

            CContext m_CurrentServer {};

            CDateTime m_NexServerTime;

            CHTTPProxyManager m_ProxyManager;

            void InitMethods() override;

            void InitServerList();

            void FetchPGP(CContext &Context);

            int CurrentContextIndex(const CString &Params);

        protected:

            CHTTPProxy *GetProxy(CHTTPServerConnection *AConnection);

            void DoAccount(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override;
            void DoDeal(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI, const CString &Action) override;
            void DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override;
            void DoSignature(CHTTPServerConnection *AConnection) override;

            bool DoProxyExecute(CTCPConnection *AConnection);
            void DoProxyException(CTCPConnection *AConnection, const Delphi::Exception::Exception &E);
            void DoEventHandlerException(CPollEventHandler *AHandler, const Delphi::Exception::Exception &E) override;

            void DoProxyConnected(CObject *Sender);
            void DoProxyDisconnected(CObject *Sender);

        public:

            explicit CWebService(CModuleProcess *AProcess, const CString &ModuleName, const CString &SectionName);

            ~CWebService() override = default;

            static class CWebService *CreateModule(CModuleProcess *AProcess) {
                return new CWebService(AProcess, "web service", "module/WebService");
            }

            void Heartbeat(CDateTime Now) override;

            void Reload() override;

        };
    }
}

using namespace Apostol::Module;
}
#endif //DM_WEB_SERVICE_MODULE_HPP
