//
// Created by WJG on 2020-5-11.
//

#ifndef MGCORE_MGBUSMANAGER_H
#define MGCORE_MGBUSMANAGER_H

#include <string>
#include <algorithm>
#include <sys/types.h>
#include "MgBusInterface.h"
#include "../CossCore/log_tool.h"
#include "CThreadPool.h"
#include "mgbus/MgBusHolder.h"

#define MSG_SOURCE "source"
#define MSG_TARGET "target"
#define MSG_BODY  "msgbody"

using namespace std;

class MgBusManager {
public:
    static MgBusManager& GetInstance()
    {
        if ( m_pInstance == nullptr )
            m_pInstance = new MgBusManager();
        return *m_pInstance;
    };

    int startMgBusModule(bool needMultiThreading);
    void stopMgBusModule();
    int putMsg2Queue(const string &sourceModule, const string &targetMoudle, const string &msg);
    int sendMsg2Target(const Json::Value &value);
    int sendMsg2Target(const std::string& mod, const std::string& uri, qlibc::QData& msg);
    int sendMsg2TargetResp(const std::string& mod, const std::string& uri, qlibc::QData& msg, qlibc::QData &resp);

    int regMegProcessFun(const string &yourModule, MSG_PROC_CALLBACK func);

    void setMgBusHolder(jedge::MgBusHolder &holder);


    MSG_PROC_CALLBACK getCallbackFun(const string &moduleName);
    vector<string> getModuleName();
    ~MgBusManager();

private:
    static MgBusManager* m_pInstance;
    vector<string> moduleVector;
    unordered_map<string, MSG_PROC_CALLBACK> moduleFun;
    std::mutex vectorMutex;
    CThreadPool* mThreadPool;
    unsigned char hasStarted;
    jedge::MgBusHolder *mgBusHolder;

    MgBusManager();

};


#endif //MGCORE_MGBUSMANAGER_H
