[(SLAVE) slave(void *arg) IN server-manet.c]
//******************** client-manet.c int main(int argc, char *argv[]) { masterip <- read from masterip.txt; //global variable, echo 1.1.1.1 >masterip.txt connect(sock, masterip); CHK(send(sock, "running process, update database", strlen("running process, update database"), 0)); } //******************** client-manet.c
//******************** server-manet.c int ctrl; //global variable int changed=0; int pipe_fd[2]; char *masterip; char *self_ip; //read * from client-manet.c //模拟 流程执行,数据库更新的事件,事件驱动 数据库同步 void *handle_client(void *arg) { bind(listener, self_ip); int pipe_write = *((int *)arg); while (1) { CHK2(client, accept(listener, (struct sockaddr *)&peer, &socklen)); while (1) { CHK2(len, recv(client, clientmsg, CLIENTMSG_SIZE, MSG_NOSIGNAL)); CHK(write(pipe_write, clientmsg, strlen(clientmsg))); //send MSG to m_thread(void *arg) } } } int main(int argc, char *argv[]) { sprintf(self_ip,"%s",getipaddress("enp13s0")); pthread_create(&readctrl, NULL, read_ctrl, NULL); pthread_create(&writer, NULL, handle_client, (void *)&pipe_fd[1]); while (1) { pthread_create(&tid, NULL, master, NULL); //ctrl==1 or pthread_create(&tid, NULL, slave, NULL); //ctrl==0 } } void *read_ctrl(void *arg) { while (1) { ctrl <- read from ctrl.txt; //global variable, echo 1 >ctrl.txt masterip <- read from masterip.txt; //global variable, echo 1.1.1.1 >masterip.txt master_ip = ip2uint(getipaddress(masterip)); if (master_ip!=prev_ip) { replaceline(hosts, line, "masterip mpe.localhost"); //update hosts } } } void *slave(void *arg) { connect(sock, masterip); while (1) { if (changed) break; recv(sock, buf, BUF_SIZE, MSG_NOSIGNAL); //read * from master } } void *master(void *arg) { sprintf(self_ip,"%s",getipaddress("enp13s0")); bind(listener, self_ip); while (1) { if (changed) break; CHK2(client, accept(listener, (struct sockaddr *)&peer, &socklen)); int rt = pthread_create(&reader, NULL, m_thread, (void *)&client); } } //event_driven, read pipe_fd[0] from handle_client(void *arg) which read * from client-manet.c void *m_thread(void *arg) { int client = *((int *)arg); CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, pipe_fd[0], &ev)); //使用epoll模拟 流程执行,数据库更新的事件,事件驱动 数据库同步 while (1) { //communication between master & slave if (changed) break; if((epoll_events_count = epoll_wait(epfd, events, 1, EPOLL_RUN_TIMEOUT)) < 0){ sleep(1); continue; } for (int i = 0; i < epoll_events_count; i++) { if (events[i].data.fd == pipe_fd[0]) //管道读端,从client-manet.c接受信息 { CHK2(res, read(pipe_fd[0], clientmsg, CLIENTMSG_SIZE)); CHK(send(client, "synchronous data", strlen("synchronous data"), MSG_NOSIGNAL)); } } } } //******************** server-manet.c
//******************** server-manet-slave.c int main(int argc, char *argv[]) { sprintf(self_ip,"%s",getipaddress("enp13s0")); pthread_create(&readctrl, NULL, read_ctrl, NULL); while (1) { pthread_create(&tid, NULL, slave, NULL); pthread_join(tid, &tret); } //end while } void *read_ctrl(void *arg) { char filename[6] = "hosts"; //set master_ip in /etc/hosts uint32_t me_ip = ip2uint(getipaddress("enp13s0")); while (1) { ctrl <- read from ctrl_slave.txt; //global variable, echo 0 >ctrl_slave.txt masterip <- read from masterip.txt; //global variable, echo 1.1.1.1 >masterip.txt master_ip = ip2uint(getipaddress(masterip)); if (master_ip!=prev_ip) { replaceline(hosts, line, "masterip mpe.localhost"); //update hosts } } } void *slave(void *arg) { connect(sock, masterip); while (1) { if (changed) break; recv(sock, buf, BUF_SIZE, MSG_NOSIGNAL)); } } //******************** server-manet-slave.c