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>
|
2025-06-16 19:52:17 +08:00
|
|
|
|
#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-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-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-13 11:29:59 +08:00
|
|
|
|
|
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,
|
2025-07-03 11:13:16 +08:00
|
|
|
|
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,
|
2025-07-03 11:13:16 +08:00
|
|
|
|
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>
|
2025-06-24 18:40:10 +08:00
|
|
|
|
"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-06-20 09:25:17 +08:00
|
|
|
|
/* <20>̹߳<DFB3><CCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0<><30><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>*/
|
2025-06-16 19:52:17 +08:00
|
|
|
|
/* <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);
|
2025-06-16 19:52:17 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-06-16 19:52:17 +08:00
|
|
|
|
printf("Started client connections\n");
|
2025-06-13 11:29:59 +08:00
|
|
|
|
|
2025-06-24 10:33:31 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
std::vector<PointInfo> points1 = {
|
2025-07-03 11:13:16 +08:00
|
|
|
|
{"P001", "Main Voltage", "D001",1 ,1, 1, 1, 1},
|
|
|
|
|
|
{"P002", "Backup Voltage", "D001",2 ,1, 1, 1, 1}
|
2025-06-24 10:33:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<PointInfo> points2 = {
|
2025-07-03 11:13:16 +08:00
|
|
|
|
{"P101", "Generator Output", "D002",1 ,1, 1, 1, 1}
|
2025-06-24 10:33:31 +08:00
|
|
|
|
};
|
2025-07-03 11:13:16 +08:00
|
|
|
|
//00-B7-8D-A8-00-D6
|
2025-06-24 10:33:31 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD>б<EFBFBD>
|
|
|
|
|
|
std::vector<DeviceInfo> devices = {
|
|
|
|
|
|
{
|
2025-06-25 10:54:09 +08:00
|
|
|
|
"D001", "Primary Device", "Model-X", "00-B7-8D-A8-00-D9",
|
2025-06-24 10:33:31 +08:00
|
|
|
|
1, points1
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-07-03 11:13:16 +08:00
|
|
|
|
"D002", "Backup Device", "Model-Y", "00-B7-8D-01-79-06",
|
2025-06-24 10:33:31 +08:00
|
|
|
|
1, points2
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2025-06-26 16:44:21 +08:00
|
|
|
|
//std::vector<DeviceInfo> devices = GenerateDeviceInfoFromLedger(terminal_devlist);//lnk<6E><6B><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
2025-06-24 14:50:50 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2025-06-24 10:33:31 +08:00
|
|
|
|
start_client_connect(devices);
|
2025-06-16 19:52:17 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
2025-06-16 19:52:17 +08:00
|
|
|
|
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>
|
2025-06-25 10:54:09 +08:00
|
|
|
|
process_received_message(msg.mac, msg.device_id, msg.data, msg.length);
|
2025-06-20 09:25:17 +08:00
|
|
|
|
|
|
|
|
|
|
free(msg.data);
|
|
|
|
|
|
}
|
2025-06-24 18:40:10 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
if (index == 0) {
|
2025-06-16 19:52:17 +08:00
|
|
|
|
// <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);
|
2025-06-16 19:52:17 +08:00
|
|
|
|
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-06-20 09:25:17 +08:00
|
|
|
|
else if (index == 1) {
|
|
|
|
|
|
// <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-06-25 13:37:01 +08:00
|
|
|
|
else if (false) {
|
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 {
|
2025-06-16 19:52:17 +08:00
|
|
|
|
// <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>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* <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-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>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
|
|
|
|
|
|
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
|
|
|
|
|
|
int* index = (int*)malloc(sizeof(int));
|
|
|
|
|
|
*index = i;
|
|
|
|
|
|
|
|
|
|
|
|
if (i == 0) {
|
2025-06-16 19:52:17 +08:00
|
|
|
|
// <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-06-20 09:25:17 +08:00
|
|
|
|
else if (i == 1) {
|
|
|
|
|
|
// <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-06-25 13:35:17 +08:00
|
|
|
|
else if (i == 2){
|
|
|
|
|
|
//<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-06-13 11:29:59 +08:00
|
|
|
|
else {
|
2025-06-16 19:52:17 +08:00
|
|
|
|
// <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>״̬
|
2025-06-24 18:40:10 +08:00
|
|
|
|
static int queue_monitor = 0;
|
|
|
|
|
|
//static int count = 3;
|
2025-06-20 09:25:17 +08:00
|
|
|
|
if (++queue_monitor >= 10) { // ÿ10<31>뱨<EFBFBD><EBB1A8>һ<EFBFBD><D2BB>
|
|
|
|
|
|
printf("Message queue size: %zu\n", message_queue.size());
|
|
|
|
|
|
queue_monitor = 0;
|
2025-06-24 18:40:10 +08:00
|
|
|
|
|
|
|
|
|
|
/*std::vector<DeviceInfo> test_devices = generate_test_devices(count);
|
|
|
|
|
|
count++;
|
|
|
|
|
|
for (const auto& device : test_devices) {
|
|
|
|
|
|
ClientManager::instance().add_device(device);
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const auto& device : test_devices) {
|
|
|
|
|
|
ClientManager::instance().remove_device("D001");
|
|
|
|
|
|
}*/
|
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;
|
|
|
|
|
|
}
|