Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6684710
  • 博文数量: 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)

分类: C/C++

2016-01-17 16:15:21

在同一台电脑测试:

[client-manet.c] send to
[(MASTER) handle_client(void *arg) IN server-manet.c] send to
[(MASTER) m_thread(void *arg) IN server-manet.c] send to
[(SLAVE) slave(void *arg) IN server-manet-slave.c]

启动顺序:
[root@localhost server-client-pthread-c]# ./server-manet
[root@localhost server-client-pthread-c]# ./server-manet-slave
[root@localhost server-client-pthread-c]# ./client-manet

++++++++++++++++++++++++++++++++++++++++++++++
在CORE中测试时:
1.     在每个节点的主目录创建文件: ctrl.txt, masterip.txt, hosts,
    根据batman-adv协议,选择 主节点,其它为从节点;要修改 上面3个文件的值

[client-manet.c] send to
[(MASTER) handle_client(void *arg) IN server-manet.c] send to
[(MASTER) m_thread(void *arg) IN server-manet.c] send to
[(SLAVE) slave(void *arg) IN server-manet.c]


点击(此处)折叠或打开

  1. //******************** client-manet.c
  2. int main(int argc, char *argv[])
  3. {
  4.     masterip <- read from masterip.txt;            //global variable, echo 1.1.1.1 >masterip.txt
  5.     connect(sock, masterip);
  6.     CHK(send(sock, "running process, update database", strlen("running process, update database"), 0));
  7. }
  8. //******************** client-manet.c


点击(此处)折叠或打开

  1. //******************** server-manet.c

  2. int ctrl;            //global variable
  3. int changed=0;
  4. int pipe_fd[2];
  5. char *masterip;
  6. char *self_ip;

  7. //read * from client-manet.c
  8. //模拟 流程执行,数据库更新的事件,事件驱动 数据库同步
  9. void *handle_client(void *arg)
  10. {
  11.     bind(listener, self_ip);
  12.     int pipe_write = *((int *)arg);
  13.     while (1) {
  14.         CHK2(client, accept(listener, (struct sockaddr *)&peer, &socklen));
  15.         while (1) {
  16.             CHK2(len, recv(client, clientmsg, CLIENTMSG_SIZE, MSG_NOSIGNAL));
  17.             CHK(write(pipe_write, clientmsg, strlen(clientmsg)));    //send MSG to m_thread(void *arg)
  18.         }
  19.     }
  20. }

  21. int main(int argc, char *argv[])
  22. {
  23.     sprintf(self_ip,"%s",getipaddress("enp13s0"));

  24.     pthread_create(&readctrl, NULL, read_ctrl, NULL);
  25.     pthread_create(&writer, NULL, handle_client, (void *)&pipe_fd[1]);
  26.     while (1) {
  27.             pthread_create(&tid, NULL, master, NULL);    //ctrl==1
  28.         or
  29.             pthread_create(&tid, NULL, slave, NULL);    //ctrl==0
  30.     }
  31. }

  32. void *read_ctrl(void *arg)
  33. {
  34.     while (1) {
  35.         ctrl <- read from ctrl.txt;                    //global variable, echo 1 >ctrl.txt
  36.         masterip <- read from masterip.txt;            //global variable, echo 1.1.1.1 >masterip.txt
  37.         master_ip = ip2uint(getipaddress(masterip));
  38.         if (master_ip!=prev_ip) {
  39.             replaceline(hosts, line, "masterip mpe.localhost");        //update hosts
  40.         }
  41.     }
  42. }

  43. void *slave(void *arg)
  44. {
  45.     connect(sock, masterip);
  46.     while (1)
  47.     {
  48.         if (changed) break;
  49.         recv(sock, buf, BUF_SIZE, MSG_NOSIGNAL);    //read * from master
  50.     }
  51. }

  52. void *master(void *arg)
  53. {
  54.     sprintf(self_ip,"%s",getipaddress("enp13s0"));
  55.     bind(listener, self_ip);
  56.     while (1) {
  57.         if (changed) break;
  58.         CHK2(client, accept(listener, (struct sockaddr *)&peer, &socklen));
  59.         int rt = pthread_create(&reader, NULL, m_thread, (void *)&client);
  60.     }
  61. }

  62. //event_driven, read pipe_fd[0] from handle_client(void *arg) which read * from client-manet.c
  63. void *m_thread(void *arg)
  64. {
  65.     int client = *((int *)arg);
  66.     CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, pipe_fd[0], &ev));
  67.     //使用epoll模拟 流程执行,数据库更新的事件,事件驱动 数据库同步
  68.     while (1) {        //communication between master & slave
  69.         if (changed) break;
  70.         if((epoll_events_count = epoll_wait(epfd, events, 1, EPOLL_RUN_TIMEOUT)) < 0){
  71.             sleep(1);
  72.             continue;
  73.         }
  74.         for (int i = 0; i < epoll_events_count; i++) {
  75.             if (events[i].data.fd == pipe_fd[0])    //管道读端,从client-manet.c接受信息
  76.             {
  77.                 CHK2(res, read(pipe_fd[0], clientmsg, CLIENTMSG_SIZE));
  78.                 CHK(send(client, "synchronous data", strlen("synchronous data"), MSG_NOSIGNAL));
  79.             }
  80.         }
  81.     }
  82. }
  83. //******************** server-manet.c

点击(此处)折叠或打开

  1. //******************** server-manet-slave.c
  2. int main(int argc, char *argv[])
  3. {
  4.     sprintf(self_ip,"%s",getipaddress("enp13s0"));
  5.     pthread_create(&readctrl, NULL, read_ctrl, NULL);
  6.     while (1) {
  7.         pthread_create(&tid, NULL, slave, NULL);
  8.         pthread_join(tid, &tret);
  9.     } //end while

  10. }

  11. void *read_ctrl(void *arg)
  12. {
  13.     char filename[6] = "hosts";    //set master_ip in /etc/hosts
  14.     uint32_t me_ip = ip2uint(getipaddress("enp13s0"));
  15.     while (1) {
  16.         ctrl <- read from ctrl_slave.txt;            //global variable, echo 0 >ctrl_slave.txt
  17.         masterip <- read from masterip.txt;            //global variable, echo 1.1.1.1 >masterip.txt
  18.         master_ip = ip2uint(getipaddress(masterip));
  19.         if (master_ip!=prev_ip) {
  20.             replaceline(hosts, line, "masterip mpe.localhost");        //update hosts
  21.         }
  22.     }
  23. }

  24. void *slave(void *arg)
  25. {
  26.     connect(sock, masterip);
  27.     while (1)
  28.     {
  29.         if (changed) break;
  30.         recv(sock, buf, BUF_SIZE, MSG_NOSIGNAL));
  31.     }
  32. }
  33. //******************** server-manet-slave.c





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