-
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
阅读(928) | 评论(0) | 转发(0) |