Distribution_Service/Server/TaskModel/TaskExecutor.h

133 lines
3.7 KiB
C
Raw Normal View History

2025-12-04 18:12:54 +08:00
#ifndef OH_BS_TASK_EXECUTOR_H
#define OH_BS_TASK_EXECUTOR_H
#include "TL/Map.h"
#include "TL/Queue.h"
#include "Terminal/Terminal.h"
#include "CCThreadPool.h"
#include "Transmitter.h"
struct ExecutorInfo{
int Index = -1;
CCList<int> list = {};
int TaskID = -1;
};
class TaskExecutor{
CTL::Map<int, int> Terms;
CTL::Queue<CTL::ByteArray> M_Queue;
bool Flag = false;
bool Flag_t = false;
2026-03-24 14:43:26 +08:00
static CTL::ThreadPool thread_pool_t;
2025-12-04 18:12:54 +08:00
public:
//------------------------------------------------------------------------------------------------------------------
static void InitPool() {
2026-03-24 18:14:23 +08:00
thread_pool_t.InitStart(100,100,1000 * 5);
2025-12-04 18:12:54 +08:00
}
static void ReleasePool() {
thread_pool_t.Stop();
}
//------------------------------------------------------------------------------------------------------------------
int Index = -1;
int Test = 0;
int TestNum = 0;
int DistributorFlag = 0;
int TaskID = -1;
//------------------------------------------------------------------------------------------------------------------
TaskExecutor() = default;
~TaskExecutor() = default;
TaskExecutor(TaskExecutor& taskExecutor){
this->Terms.SetStdMap(taskExecutor.Terms);
this->Index = taskExecutor.Index;
}
TaskExecutor(const TaskExecutor& taskExecutor){
this->Terms.SetStdMap(taskExecutor.Terms);
this->Index = taskExecutor.Index;
}
void Init(const ExecutorInfo& taskExecutor){
for (auto ID : taskExecutor.list) {
Terms.Put(ID,ID);
}
this->Index = taskExecutor.Index;
this->TaskID = taskExecutor.TaskID;
}
int GetTermSize(){
return Terms.Size();
}
//------------------------------------------------------------------------------------------------------------------
void AddBuffer(const CTL::ByteArray& buffer){
M_Queue.Add(buffer);
}
void Start(){
Flag = true;
Flag_t = true;
thread_pool_t.AddTask(&TaskExecutor::Running,this);
}
void Stop(){
Flag = false;
while (Flag_t){
CTL::Thread::SleepMS(1);
}
}
void AddTerm(const int TID,const CTL::ByteArray& Command){
if(Terms.IsContains(TID)){
return;
}
2025-12-06 10:48:12 +08:00
Terms.Put(TID,TID);
2025-12-04 18:12:54 +08:00
}
void RemoveTerm(const int TID,const CTL::ByteArray& Command){
if(!Terms.IsContains(TID)){
return;
}
2025-12-06 10:48:12 +08:00
Terms.Remove(TID);
2025-12-04 18:12:54 +08:00
}
static bool NotificationTerminal(const int TID,const CTL::ByteArray& Command){
auto Term = Terminal::getData(TID);
if(Term){
auto* AIM = new AIMInfo();
AIM->IP = Term->IP;
AIM->Port = Term->Port;
AIM->Data = Command;
Transmitter::Send_Massage(AIM);
delete AIM;
return true;
}
return false;
}
bool IsStop(){
return Flag_t;
}
private:
void SendTermsData(const CTL::ByteArray& data){
auto list = Terms.Values();
auto* AIM = new AIMInfo();
2026-03-24 14:43:26 +08:00
CTL::AutoDestruct<AIMInfo> Aim_Destruct(AIM);
2025-12-04 18:12:54 +08:00
AIM->Data = data;
for(auto ID : list){
auto Term = Terminal::getData(ID);
if(Term){
if(Term->Status == 1){
AIM->IP = Term->IP;
AIM->Port = Term->StreamPort;
Transmitter::Send_Stream(AIM);
}
}
}
}
void Running(){
while (Flag){
auto buffer = M_Queue.Poll();
if(buffer){
SendTermsData(*buffer.get());
}
else {
CTL::Thread::SleepMS(2);
}
}
this->Flag_t = false;
}
};
2026-03-24 14:43:26 +08:00
2025-12-04 18:12:54 +08:00
#endif