Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6543917
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: Android平台

2016-01-04 10:25:23



点击(此处)折叠或打开

  1. 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).

  2. ctrl: Master or Slave, this value is set by batman-adv;
  3.   ctrl=0, SMD is Slave role;
  4.   ctrl=1, SMD is elected as a Master role;

  5. changed: Master or Slave, this value is set by batman-adv;
  6.   changed=0, Master SMD is not changed, so, do not need to create thread in MAIN again for data synchronization;
  7.   changed=1, Master SMD is changed, so, need to create thread in MAIN again for data synchronization;

  8. changed=0;
  9. is_me=-1;
  10. prev_state=-1; //0:Slave; 1:Master;

  11. //READ_CTRL, set URL of application system. Access master_ip in browser.
  12. PROCEDURE: READ_CTRL
  13. prev_ip=0.0.0.0;
  14. while true do
  15.   read the value of ctrl set by batman-adv;
  16.   read master_ip of Master SMD;
  17.   if master_ip==me_ip and is_me!=-1 then
  18.     prev_state=is_me;
  19.     is_me=1;
  20.   else if master_ip!=me_ip and is_me!=-1 then
  21.     prev_state=is_me;
  22.     is_me=0;
  23.   end if
  24.   if master_ip!=prev_ip then
  25.     changed=1;
  26.     prev_ip=master_ip;
  27.     add (or update) "master_ip mpe.localhost" in /etc/hosts;
  28.   end if
  29.   sleep 1s;
  30. done
  31. END PROCEDURE READ_CTRL

  32. //****************************** MAIN Thread
  33. PROCEDURE: MAIN
  34. pthread_create(READ_CTRL);
  35. sleep 1s; //waiting for READ_CTRL to read the value of ctrl and master_ip of Master SMD
  36. start=1; //set initial state
  37. while true do
  38.   if start==1 and ctrl==1 then
  39.     start=0;
  40.     is_me=1;
  41.     pthread_create(Master);
  42.     pthread_join(Master);
  43.   else if start==1 and ctrl==0 then
  44.     start=0;
  45.     is_me=0;
  46.     pthread_create(Slave);
  47.     pthread_join(Slave);
  48.   else if start==0 and changed==1 and ctrl==1 then
  49.     pthread_create(Master);
  50.     pthread_join(Master);
  51.   else if start==0 and changed==1 and ctrl==0 then
  52.     sleep(2);    //waiting master initialize
  53.     pthread_create(Master);
  54.     pthread_join(Master);
  55.   else //start==0 and changed==0
  56.     continue;
  57.   end if
  58. done
  59. END PROCEDURE MAIN

  60. //****************************** Master Thread
  61. PROCEDURE: Master
  62. socket();
  63. bind();
  64. listen();
  65. while true do
  66.   accept(); //waiting for connect from Slave
  67.   pthread_create(M_THREAD);
  68. done
  69. END PROCEDURE Master

  70. //****************************** M_THREAD Thread
  71. PROCEDURE: M_THREAD
  72. while true do //communication between Master & Slave
  73.   if database updating then
  74.     send synchronous data to Slave;
  75.   end if
  76. done
  77. END PROCEDURE M_THREAD

  78. //****************************** Slave Thread
  79. PROCEDURE: Slave
  80. socket();
  81. connect();
  82. while true do
  83.   if receive synchronous data from Master then
  84.     update database;
  85.   end if
  86. done
  87. END PROCEDURE Slave




阅读(889) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~