Description: when SMD start, this algorithm will run as a deamon, two roles: Master, Slave. When the SMD is elected as a Master role, then, the MPE will be the master MPE accessed by other SMD, and related process data will backup to other SMD (Slave MPE).
ctrl: Master or Slave, this value is set by batman-adv;
ctrl=0, SMD is Slave role;
ctrl=1, SMD is elected as a Master role;
changed: Master or Slave, this value is set by batman-adv;
changed=0, Master SMD is not changed, so, do not need to create thread in MAIN again for data synchronization;
changed=1, Master SMD is changed, so, need to create thread in MAIN again for data synchronization;
changed=0;
is_me=-1;
prev_state=-1; //0:Slave; 1:Master;
//READ_CTRL, set URL of application system. Access master_ip in browser.
PROCEDURE: READ_CTRL
prev_ip=0.0.0.0;
while true do
read the value of ctrl set by batman-adv;
read master_ip of Master SMD;
if master_ip==me_ip and is_me!=-1 then
prev_state=is_me;
is_me=1;
else if master_ip!=me_ip and is_me!=-1 then
prev_state=is_me;
is_me=0;
end if
if master_ip!=prev_ip then
changed=1;
prev_ip=master_ip;
add (or update) "master_ip mpe.localhost" in /etc/hosts;
end if
sleep 1s;
done
END PROCEDURE READ_CTRL
//****************************** MAIN Thread
PROCEDURE: MAIN
pthread_create(READ_CTRL);
sleep 1s; //waiting for READ_CTRL to read the value of ctrl and master_ip of Master SMD
start=1; //set initial state
while true do
if start==1 and ctrl==1 then
start=0;
is_me=1;
pthread_create(Master);
pthread_join(Master);
else if start==1 and ctrl==0 then
start=0;
is_me=0;
pthread_create(Slave);
pthread_join(Slave);
else if start==0 and changed==1 and ctrl==1 then
pthread_create(Master);
pthread_join(Master);
else if start==0 and changed==1 and ctrl==0 then
sleep(2); //waiting master initialize
pthread_create(Master);
pthread_join(Master);
else //start==0 and changed==0
continue;
end if
done
END PROCEDURE MAIN
//****************************** Master Thread
PROCEDURE: Master
socket();
bind();
listen();
while true do
accept(); //waiting for connect from Slave
pthread_create(M_THREAD);
done
END PROCEDURE Master
//****************************** M_THREAD Thread
PROCEDURE: M_THREAD
while true do //communication between Master & Slave
if database updating then
send synchronous data to Slave;
end if
done
END PROCEDURE M_THREAD
//****************************** Slave Thread
PROCEDURE: Slave
socket();
connect();
while true do
if receive synchronous data from Master then
update database;
end if
done
END PROCEDURE Slave
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通