Files
front_linux/LFtid1056/main_thread.cpp

461 lines
15 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"
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-08-08 11:16:38 +08:00
extern int INITFLAG;//̨<>˵ȳ<CBB5>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
extern void cleanup_args(ThreadArgs* args);
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;
2025-06-24 14:50:50 +08:00
// <20><><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>װ<EFBFBD><D7B0>
std::vector<DeviceInfo> generate_test_devices(int count) {
std::vector<DeviceInfo> devices;
for (int i = 1; i <= count; ++i) {
// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>ID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::string dev_id = "D" + std::to_string(1000 + i).substr(1); // D001, D002, ..., D100
std::string dev_name = "Device " + std::to_string(i);
// <20><><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
std::vector<PointInfo> points = {
{
"P" + dev_id.substr(1) + "01", // <20><><EFBFBD><EFBFBD>ID<49><44> P00101
"Voltage " + dev_name,
dev_id,
1,
2025-06-24 14:50:50 +08:00
0.0, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹֵ
0.0,
100.0,
80.0
},
{
"P" + dev_id.substr(1) + "02", // <20><><EFBFBD><EFBFBD>ID<49><44> P00102
"Current " + dev_name,
dev_id,
2,
2025-06-24 14:50:50 +08:00
0.0, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
0.0,
20.0,
15.0
}
};
// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>
devices.push_back({
dev_id,
dev_name,
(i % 2 == 0) ? "Model-X" : "Model-Y", // <20><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͺ<EFBFBD>
"00-B7-8D-A8-00-D6", // <20><><EFBFBD><EFBFBD>MAC<41><43>ַ
2025-06-24 14:50:50 +08:00
1, // ״̬ (1=<3D><><EFBFBD><EFBFBD>)
points
});
}
return devices;
}
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";
}
}
2025-06-20 09:25:17 +08:00
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0<><30><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 = {
// {
// "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f",
// 1, points1,true
// },
// {
// "D002", "Primary Device1", "Model-X1", "00-B7-8D-01-71-09",
// 1, points2,true
// }
//};
2025-06-24 14:50:50 +08:00
// <20><><EFBFBD><EFBFBD>100<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>
//std::vector<DeviceInfo> test_devices = generate_test_devices(100);
//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;
}
2025-06-20 09:25:17 +08:00
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>*/
/* <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;
}
2025-06-13 11:29:59 +08:00
/* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void restart_thread(int index) {
pthread_mutex_lock(&global_lock);
if (thread_info[index].state == THREAD_RESTARTING) {
pthread_mutex_unlock(&global_lock);
return; // <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
}
thread_info[index].state = THREAD_RESTARTING;
printf("Restarting thread %d\n", index);
pthread_mutex_unlock(&global_lock);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
int* new_index = (int*)malloc(sizeof(int));
*new_index = index;
2025-08-08 11:16:38 +08:00
if (index == 1) {
// <20>ͻ<EFBFBD><CDBB>˹<EFBFBD><CBB9><EFBFBD><EFBFBD>߳<EFBFBD>
if (pthread_create(&thread_info[index].tid, NULL, client_manager_thread, new_index) != 0) {
2025-06-13 11:29:59 +08:00
pthread_mutex_lock(&global_lock);
printf("Failed to restart client manager thread %d\n", index);
2025-06-13 11:29:59 +08:00
thread_info[index].state = THREAD_CRASHED;
pthread_mutex_unlock(&global_lock);
free(new_index);
}
}
2025-08-08 11:16:38 +08:00
else if (index == 2) {
2025-06-20 09:25:17 +08:00
// <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
if (pthread_create(&thread_info[index].tid, NULL, message_processor_thread, new_index) != 0) {
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-08-08 11:16:38 +08:00
else if (index == 0) {
2025-06-20 16:20:59 +08:00
// <20>ӿڣ<D3BF>mq
2025-06-27 16:33:41 +08:00
char* argv[] = { (char*)new_index };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̺Ų<CCBA><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ThreadArgs* args = new ThreadArgs{1, argv};
2025-06-20 16:20:59 +08:00
if (pthread_create(&thread_info[index].tid, NULL, cloudfrontthread, args) != 0) {
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);
delete args; // <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6>ͷ<EFBFBD>
free(new_index);
}
}
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>
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>
}
2025-08-08 11:16:38 +08:00
//lnk<6E><6B><EFBFBD><EFBFBD>
ThreadArgs* make_thread_args_from_strs(const std::vector<std::string>& args_vec) {
char** argv = new char*[args_vec.size() + 1]; // <20><>һ<EFBFBD><D2BB> nullptr <20><>β
for (size_t i = 0; i < args_vec.size(); ++i) {
argv[i] = strdup(args_vec[i].c_str()); // strdup <20><> malloc <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
argv[args_vec.size()] = nullptr;
return new ThreadArgs{static_cast<int>(args_vec.size()), argv};
}
2025-06-13 11:29:59 +08:00
/* <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;
}
2025-09-16 10:13:47 +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
ThreadArgs* args = make_thread_args_from_strs({ "0" });
if (pthread_create(&thread_info[0].tid, NULL, cloudfrontthread, args) != 0) {
printf("Failed to create message processor thread 0\n");
cleanup_args(args);
}
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;
2025-08-08 11:16:38 +08:00
if (i == 1) {
// <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);
}
}
2025-08-08 11:16:38 +08:00
else if (i == 2) {
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) {
printf("Failed to create message processor thread %d\n", i);
free(index);
}
}
2025-08-08 11:16:38 +08:00
else if (i == 3){
/*//<2F>ӿں<D3BF>mq
2025-06-27 16:33:41 +08:00
char* argv[] = { (char*)index };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̺Ų<CCBA><C5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ThreadArgs* args = new ThreadArgs{1, argv};
2025-06-25 13:35:17 +08:00
if (pthread_create(&thread_info[i].tid, NULL, cloudfrontthread, args) != 0) {
printf("Failed to create message processor thread %d\n", i);
delete args; // <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6>ͷ<EFBFBD>
free(index);
2025-08-08 11:16:38 +08:00
}*/
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
2025-09-04 20:59:17 +08:00
/*// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-08-08 16:24:17 +08:00
std::vector<PointInfo> points2 = {
{"P101", "Generator Output", "D002",1 ,1, 1, 1, 1,"0.38k",0}
};
//00B78DA800D6 00-B7-8D-01-79-06 00-B7-8D-A8-00-D6
2025-08-08 16:24:17 +08:00
// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD>б<EFBFBD>
std::vector<DeviceInfo> devices = {
{
"D002", "Backup Device", "Model-Y", "00-B7-8D-A8-00-D6",
1, points2,true
2025-08-08 16:24:17 +08:00
}
2025-09-04 20:59:17 +08:00
};*/
2025-08-08 16:24:17 +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 = {
// {
// "D001", "Primary Device", "Model-X", "00-B7-8D-01-88-7f",
// 1, points1,true
// },
// {
// "D002", "Primary Device1", "Model-X1", "00-B7-8D-01-71-09",
// 1, points2,true
// }
//};
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;
//static int count = 3;
if (++queue_monitor >= 60) { // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>
2025-06-20 09:25:17 +08:00
printf("Message queue size: %zu\n", message_queue.size());
queue_monitor = 0;
/*if (flag) {
flag = false;
for (const auto& device : devices) {
ClientManager::instance().add_device(device);
}
}
else {
flag = true;
ClientManager::instance().remove_device("D001");
ClientManager::instance().remove_device("D002");
}*/
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;
}