paper4—Multi-MPE_Trigger_Algorithm—testing

5817 ワード

同じコンピュータでテスト:
[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]

    //******************** 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