Files
front_linux/LFtid1056/main_thread.cpp

432 lines
14 KiB
C++
Raw Normal View History

2025-06-13 11:29:59 +08:00
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include "client2.h"
2025-06-20 16:20:59 +08:00
#include "cloudfront/code/interface.h"
#include "pqdif_thread_processor.h"
2025-06-27 16:33:41 +08:00
#include <iostream>
2025-08-08 11:16:38 +08:00
#include <thread>
#include <chrono>
2025-06-25 10:28:14 +08:00
2025-06-24 14:50:50 +08:00
using namespace std;
2025-06-20 16:20:59 +08:00
#if 0
2025-06-13 11:29:59 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
#define THREAD_CONNECTIONS 10 // <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
#define MONITOR_INTERVAL 1 // <20><><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>(<28><>)
/* <20>߳<EFBFBD>״̬ö<CCAC><C3B6> */
typedef enum {
THREAD_RUNNING, // 0:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
THREAD_STOPPED, // 1:<3A><><EFBFBD><EFBFBD>ֹͣ
THREAD_RESTARTING, // 2:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
THREAD_CRASHED // 3:<3A><EFBFBD><ECB3A3><EFBFBD><EFBFBD>
} thread_state_t;
/* <20>߳̿<DFB3><CCBF>ƽ<C6BD><E1B9B9> */
typedef struct {
pthread_t tid; // <20>߳<EFBFBD>ID
int index; // <20>̱߳<DFB3><CCB1><EFBFBD>(0~CONNECTIONS-1)
thread_state_t state; // <20><>ǰ״̬
pthread_mutex_t lock; // <20>߳<EFBFBD>ר<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>
} thread_info_t;
2025-06-20 16:20:59 +08:00
#endif
2025-07-04 09:50:59 +08:00
2025-12-11 15:07:54 +08:00
extern std::atomic<int> INITFLAG;//̨<>˵ȳ<CBB5>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
//extern void cleanup_args(ThreadArgs* args);00:B7:8D:A8:00:D6 00:B7:8D:00:A9:03
2025-08-08 11:16:38 +08:00
2025-07-04 09:50:59 +08:00
void init_daemon(void)
{
int pid;
int i;
if( pid = fork() )
exit(0); /** <20>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
else if( pid < 0 )
exit(1); /** forkʧ<6B>ܣ<EFBFBD><DCA3>˳<EFBFBD> */
/** <20>ǵ<EFBFBD>һ<EFBFBD>ӽ<EFBFBD><D3BD>̣<EFBFBD><CCA3><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4> */
setsid(); /** <20><>һ<EFBFBD>ӽ<EFBFBD><D3BD>̳<EFBFBD>Ϊ<EFBFBD>µĻỰ<C4BB><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD><EFBFBD><E9B3A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն˷<D5B6><CBB7><EFBFBD> */
if( pid = fork() )
exit(0); /** <20>ǵ<EFBFBD>һ<EFBFBD>ӽ<EFBFBD><D3BD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD> */
else if( pid < 0)
exit(1); /** forkʧ<6B>ܣ<EFBFBD><DCA3>˳<EFBFBD> */
chdir("/FeProject/bin/"); //multi process running at same time
umask(0); /** <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
return;
}
2025-06-13 11:29:59 +08:00
/* ȫ<>ֱ<EFBFBD><D6B1><EFBFBD> */
thread_info_t thread_info[THREAD_CONNECTIONS]; // <20>߳<EFBFBD><DFB3><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER; // ȫ<>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>
2025-06-20 09:25:17 +08:00
extern SafeMessageQueue message_queue;
bool PQD_FLAG = false;//pqd<71>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־ <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD>߳<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD>
2025-06-24 14:50:50 +08:00
//̨<>˴<EFBFBD>ӡ
2025-08-08 11:16:38 +08:00
void PrintDevices(const std::vector<DeviceInfo>& devices) {
std::cout << "==== Devices List (" << devices.size() << ") ====\n";
for (const auto& dev : devices) {
std::cout << "Device ID : " << dev.device_id << "\n";
std::cout << "Name : " << dev.name << "\n";
std::cout << "Model : " << dev.model << "\n";
std::cout << "MAC : " << dev.mac << "\n";
std::cout << "Status : " << dev.status << "\n";
std::cout << "Points (" << dev.points.size() << "):\n";
for (const auto& pt : dev.points) {
std::cout << " Point ID : " << pt.point_id << "\n";
std::cout << " Name : " << pt.name << "\n";
std::cout << " Device ID : " << pt.device_id << "\n";
std::cout << " Cpu No : " << pt.nCpuNo << "\n";
std::cout << " PT1 : " << pt.PT1 << "\n";
std::cout << " PT2 : " << pt.PT2 << "\n";
std::cout << " CT1 : " << pt.CT1 << "\n";
std::cout << " CT2 : " << pt.CT2 << "\n";
std::cout << " Scale : " << pt.strScale << "\n";
std::cout << " PTType : " << pt.nPTType << "\n";
std::cout << "----------------------\n";
}
std::cout << "==========================\n";
}
}
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>*/
/* <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD><EFBFBD>̺߳<DFB3><CCBA><EFBFBD>*/
void* client_manager_thread(void* arg) {
int index = *(int*)arg;
free(arg);
2025-06-13 11:29:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>״̬Ϊ<CCAC><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pthread_mutex_lock(&thread_info[index].lock);
printf("Client Manager Thread %d started\n", index);
2025-06-13 11:29:59 +08:00
thread_info[index].state = THREAD_RUNNING;
pthread_mutex_unlock(&thread_info[index].lock);
printf("Started client connections\n");
2025-06-13 11:29:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//std::vector<PointInfo> points1 = {
// {"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1,"0.38k",0},
// {"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1,"0.38k",0}
//};
//std::vector<PointInfo> points2 = {
// {"P003", "Main Voltage", "D002",1 ,1, 1, 1, 1,"0.38k",0},
// {"P004", "Backup Voltage", "D002",2 ,1, 1, 1, 1,"0.38k",0}
//};
////00B78DA800D6 00-B7-8D-01-79-06 00-B7-8D-A8-00-D6 00-B7-8D-01-71-09 00-B7-8D-01-88-7f
//// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD>б<EFBFBD>
//std::vector<DeviceInfo> devices = {
// {
2025-12-08 15:21:15 +08:00
// "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f",
// 1, points1,true
// }
//};
//lnk<6E><6B>̨<EFBFBD>˶<EFBFBD>ȡ<EFBFBD>
2025-09-16 10:13:47 +08:00
std::vector<DeviceInfo> devices = GenerateDeviceInfoFromLedger(terminal_devlist);//lnk<6E><6B><EFBFBD><EFBFBD>
2025-06-24 14:50:50 +08:00
//̨<>˴<EFBFBD>ӡ
2025-09-04 20:59:17 +08:00
PrintDevices(devices);
2025-06-26 16:44:21 +08:00
2025-06-24 14:50:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
start_client_connect(devices);
printf("Stopped all client connections\n");
2025-06-13 11:29:59 +08:00
// <20>߳<EFBFBD><DFB3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
pthread_mutex_lock(&thread_info[index].lock);
thread_info[index].state = THREAD_STOPPED;
printf("Client Manager Thread %d stopped\n", index);
2025-06-13 11:29:59 +08:00
pthread_mutex_unlock(&thread_info[index].lock);
return NULL;
}
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2<><32><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>*/
2025-06-20 09:25:17 +08:00
/* <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>̺߳<DFB3><CCBA><EFBFBD> */
void* message_processor_thread(void* arg) {
int index = *(int*)arg;
free(arg);
// <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>״̬Ϊ<CCAC><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pthread_mutex_lock(&thread_info[index].lock);
printf("Message Processor Thread %d started\n", index);
thread_info[index].state = THREAD_RUNNING;
pthread_mutex_unlock(&thread_info[index].lock);
// <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
while (1) {
deal_message_t msg;
if (message_queue.pop(msg)) {
// ʵ<><CAB5><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
// ע<><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>msg.client_index<65><78><EFBFBD>ֿͻ<D6BF><CDBB><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
// <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
process_received_message(msg.mac, msg.device_id, msg.data, msg.length);
2025-06-20 09:25:17 +08:00
free(msg.data);
}
else {
// <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>100΢<30><CEA2> = 0.1<EFBFBD><EFBFBD><EFBFBD>
usleep(100);
}
2025-06-20 09:25:17 +08:00
}
// <20>߳<EFBFBD><DFB3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
pthread_mutex_lock(&thread_info[index].lock);
thread_info[index].state = THREAD_STOPPED;
printf("Message Processor Thread %d stopped\n", index);
pthread_mutex_unlock(&thread_info[index].lock);
return NULL;
}
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3<><33><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>*/
/* PQDIF<49><46><EFBFBD><EFBFBD><EFBFBD>̺߳<DFB3><CCBA><EFBFBD>*/
void* pqd_thread(void* arg)
{
int index = *(int*)arg;
free(arg);
pthread_mutex_lock(&thread_info[index].lock);
printf("PQDIF Thread %d started\n", index);
thread_info[index].state = THREAD_RUNNING;
pthread_mutex_unlock(&thread_info[index].lock);
try
{
// <20><><EFBFBD><EFBFBD> PQDIF ɨ<><C9A8><EFBFBD>߳<EFBFBD>
RunPqdifScanLoop();
}
catch (const std::exception& ex)
{
printf("PQDIF Thread %d exception: %s\n", index, ex.what());
}
catch (...)
{
printf("PQDIF Thread %d exception: unknown\n", index);
}
pthread_mutex_lock(&thread_info[index].lock);
thread_info[index].state = THREAD_STOPPED;
printf("PQDIF Thread %d stopped\n", index);
pthread_mutex_unlock(&thread_info[index].lock);
return NULL;
}
2025-06-13 11:29:59 +08:00
/* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void restart_thread(int index) {
2025-12-08 15:34:46 +08:00
//lnk20251208
pthread_t old_tid = 0;
2025-06-13 11:29:59 +08:00
pthread_mutex_lock(&global_lock);
2025-12-08 15:34:46 +08:00
2025-06-13 11:29:59 +08:00
if (thread_info[index].state == THREAD_RESTARTING) {
pthread_mutex_unlock(&global_lock);
return; // <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
}
2025-12-08 15:34:46 +08:00
// <20><><EFBFBD><EFBFBD>֮ǰ<D6AE><C7B0> STOPPED<45><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD> tid <20>ǿգ<C7BF><D5A3><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>һ<EFBFBD>¾<EFBFBD><C2BE>̣߳<DFB3><CCA3><EFBFBD>ֹ<EFBFBD><D6B9>Դй¶<D0B9><C2B6>lnk20251208
if (thread_info[index].state == THREAD_STOPPED && thread_info[index].tid) {
old_tid = thread_info[index].tid;
thread_info[index].tid = 0;
}
2025-06-13 11:29:59 +08:00
thread_info[index].state = THREAD_RESTARTING;
2025-12-08 15:34:46 +08:00
2025-06-13 11:29:59 +08:00
printf("Restarting thread %d\n", index);
2025-12-08 15:34:46 +08:00
2025-06-13 11:29:59 +08:00
pthread_mutex_unlock(&global_lock);
2025-12-08 15:34:46 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> join<69><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
if (old_tid) {
pthread_join(old_tid, NULL);
}
// ========== <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD> ==========lnk20251208
if (index == 0) {
// <20>ӿ<EFBFBD> + MQ<4D><51>cloudfrontthread <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӵ<EFBFBD> NULL
if (pthread_create(&thread_info[index].tid, NULL,
cloudfrontthread, NULL) != 0) {
pthread_mutex_lock(&global_lock);
printf("Failed to restart cloudfrontthread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
}
} else if (index == 1 && !PQD_FLAG) {
2025-12-08 15:34:46 +08:00
// <20>ͻ<EFBFBD><CDBB>˹<EFBFBD><CBB9><EFBFBD><EFBFBD>߳<EFBFBD>
int* new_index = (int*)malloc(sizeof(int));
if (!new_index) {
pthread_mutex_lock(&global_lock);
printf("Failed to malloc for client manager thread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
return;
}
*new_index = index;
if (pthread_create(&thread_info[index].tid, NULL,client_manager_thread, new_index) != 0) {
2025-12-08 15:34:46 +08:00
pthread_mutex_lock(&global_lock);
printf("Failed to restart client manager thread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
free(new_index); // ʧ<>ܲ<EFBFBD><DCB2>Լ<EFBFBD> free<65><65><EFBFBD>ɹ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>߳<EFBFBD><DFB3><EFBFBD> free
}
} else if (index == 2 && !PQD_FLAG) {
2025-12-08 15:34:46 +08:00
// <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
int* new_index = (int*)malloc(sizeof(int));
if (!new_index) {
pthread_mutex_lock(&global_lock);
printf("Failed to malloc for message processor thread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
return;
}
*new_index = index;
if (pthread_create(&thread_info[index].tid, NULL,message_processor_thread, new_index) != 0) {
2025-12-08 15:34:46 +08:00
pthread_mutex_lock(&global_lock);
printf("Failed to restart message processor thread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
free(new_index);
}
} else if(index == 3 && PQD_FLAG){
//PQDIF<49><46><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD> <20><><EFBFBD><EFBFBD><E0B9A4><EFBFBD>̲߳<DFB3><CCB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int* new_index = (int*)malloc(sizeof(int));
if (!new_index) {
2025-06-20 09:25:17 +08:00
pthread_mutex_lock(&global_lock);
printf("Failed to malloc for message processor thread %d\n", index);
2025-06-20 09:25:17 +08:00
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
return;
2025-06-20 09:25:17 +08:00
}
*new_index = index;
if (pthread_create(&thread_info[index].tid, NULL, pqd_thread, new_index) != 0) {
2025-06-20 16:20:59 +08:00
pthread_mutex_lock(&global_lock);
printf("Failed to restart message processor thread %d\n", index);
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
free(new_index);
}
2025-12-08 15:34:46 +08:00
} else {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD>
}
2025-06-13 11:29:59 +08:00
}
/* <20>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int is_thread_alive(pthread_t tid) {
return pthread_tryjoin_np(tid, NULL) == EBUSY; // EBUSY<53><59>ʾ<EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2025-06-27 16:33:41 +08:00
int main(int argc ,char** argv) {//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>
if(!parse_param(argc,argv)){
std::cerr << "process param error,exit" << std::endl;
return 1;
}
2026-03-09 19:08:02 +08:00
init_daemon();
2025-06-13 11:29:59 +08:00
srand(time(NULL)); // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><>ʼ<EFBFBD><CABC><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
thread_info[i].index = i;
thread_info[i].state = THREAD_STOPPED;
pthread_mutex_init(&thread_info[i].lock, NULL); // <20><>ʼ<EFBFBD><CABC>ÿ<EFBFBD><C3BF><EFBFBD>̵߳<DFB3><CCB5><EFBFBD>
}
2025-08-08 11:16:38 +08:00
//<2F>ӿں<D3BF>mq
2025-12-08 15:34:46 +08:00
//ThreadArgs* args = make_thread_args_from_strs({ "0" });
if (pthread_create(&thread_info[0].tid, NULL, cloudfrontthread, NULL) != 0) {
2025-08-08 11:16:38 +08:00
printf("Failed to create message processor thread 0\n");
2025-12-08 15:34:46 +08:00
//cleanup_args(args);
2025-08-08 11:16:38 +08:00
}
2025-09-04 20:59:17 +08:00
while(!INITFLAG){
2025-08-08 11:16:38 +08:00
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << "waiting cloudfront initialize ..." << std::endl;
2025-09-04 20:59:17 +08:00
}
2025-08-08 11:16:38 +08:00
2025-06-13 11:29:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
2025-08-08 11:16:38 +08:00
for (int i = 1; i < THREAD_CONNECTIONS; i++) {
2025-06-13 11:29:59 +08:00
int* index = (int*)malloc(sizeof(int));
*index = i;
if (i == 1 && !PQD_FLAG) {
// <20>ͻ<EFBFBD><CDBB>˹<EFBFBD><CBB9><EFBFBD><EFBFBD>߳<EFBFBD>
if (pthread_create(&thread_info[i].tid, NULL, client_manager_thread, index) != 0 ) {
printf("Failed to create client manager thread %d\n", i);
2025-06-13 11:29:59 +08:00
free(index);
}
}
else if (i == 2 && !PQD_FLAG) {
2025-06-20 09:25:17 +08:00
// <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
if (pthread_create(&thread_info[i].tid, NULL, message_processor_thread, index) != 0 ) {
2025-06-20 09:25:17 +08:00
printf("Failed to create message processor thread %d\n", i);
free(index);
}
}
else if (i == 3 && PQD_FLAG){
//PQDIF<49><46><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD> <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><E0B9A4><EFBFBD>߳<EFBFBD>ֹͣ
if (pthread_create(&thread_info[i].tid, NULL, pqd_thread, index) != 0) {
printf("Failed to create pqd_thread %d\n", i);
2025-06-25 13:35:17 +08:00
free(index);
}
2025-06-25 13:35:17 +08:00
}
2025-06-13 11:29:59 +08:00
else {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>ʵ<EFBFBD><CAB5>Ӧ<EFBFBD><D3A6><EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
free(index);
2025-06-13 11:29:59 +08:00
}
2025-06-20 09:25:17 +08:00
2025-06-13 11:29:59 +08:00
}
printf("Thread monitoring system started with %d workers\n", THREAD_CONNECTIONS);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
while (1) {
sleep(MONITOR_INTERVAL);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>״̬
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
pthread_mutex_lock(&thread_info[i].lock);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (thread_info[i].state == THREAD_RUNNING && !is_thread_alive(thread_info[i].tid)) {
printf("Thread %d crashed unexpectedly\n", i);
thread_info[i].state = THREAD_CRASHED;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
if (thread_info[i].state == THREAD_STOPPED || thread_info[i].state == THREAD_CRASHED) {
pthread_mutex_unlock(&thread_info[i].lock);
restart_thread(i); // <20><EFBFBD><ECB2BD><EFBFBD><EFBFBD>
}
else {
pthread_mutex_unlock(&thread_info[i].lock);
}
}
2025-06-20 09:25:17 +08:00
// <20><><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD>״̬
static int queue_monitor = 0;
static bool flag = false;
if (++queue_monitor >= 60) {
2025-06-20 09:25:17 +08:00
printf("Message queue size: %zu\n", message_queue.size());
queue_monitor = 0;
2025-06-20 09:25:17 +08:00
}
2025-06-13 11:29:59 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ(<28><><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD>ִ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>)
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
pthread_mutex_destroy(&thread_info[i].lock);
}
return 0;
}