void loop() {

    clock_update();

    if(pulse_100ms){

        main_prev_millis=millis();

        main_period_actual=millis()-main_period_prev_millis;
        main_period_prev_millis=millis();

        if(main_period_actual>main_period_max)
            main_period_max=main_period_actual;

        //~ Serial.print("1");
        /* recupere les valeurs des cartes d'entrees si changement détecté*/

        if(NumberOfBoardIn4Dimmer4 && !digitalRead(int_i2c))
            BoardIn4Dimmer4_pre();

        if(NumberOfBoardIn16 && !digitalRead(int_i2c))
            BoardIn16_pre();

        if(NumberOfBoardIn8R8 && !digitalRead(int_i2c))
            BoardIn8R8_pre();

		if(NumberOfRFDevice)
            RFDevice_pre();  

        /* update all the preliminary condition */
        pre_update();  // cf custom.ino

        scenario(); // cf custom.ino


        /* update the status of the shutters */
        if (NumberOfShutter)
            shutter_post();

        /* update the post conditions */
        post_update(); // cf custom.ino

        /* update the output of the boards */
        if(NumberOfBoardIn4Dimmer4)
            BoardIn4Dimmer4_post();

        if(NumberOfBoardR8)
            BoardR8_post();

        if(NumberOfBoardIn8R8)
            BoardIn8R8_post();

		if(NumberOfRFDevice)
            RFDevice_post(); 

        main_actual=millis()-main_prev_millis;
        if(main_actual>main_max)
            main_max=main_actual;


    digitalWrite(STATUS_LED, LOW);
/**/


   }

    other_prev_millis=millis();


    if(pulse_1000ms){

        hbeat++;

        RTC.getTime(); /// update time should be every 500ms or 1s

        if(NumberOfBoardIn4Dimmer4)
            BoardIn4Dimmer4_hbeat();

        if(NumberOfBoardIn8R8)
            BoardIn8R8_hbeat();

        if(NumberOfBoardIn16)
            BoardIn16_hbeat();

        if(NumberOfBoardR8)
            BoardR8_hbeat();


        #ifdef WITH_LIGHTING
        if (NumberOfLighting)
            lighting_hard_status();
        #endif
        //~ Serial.print("1");
        if(NumberOfShutter)
            shutter_hard_status();
        //~ Serial.print("2");
        if(NumberOfTemp){
            //~ Serial.print("3");
         Read_DS2482_Temp(true); //lecture des temperature sur les sodes OneWire en mode seconde
        }
        temperature_sendXPL(); /// temporaire

        TeleInfo.read();

    }

    if(pulse_1mn){

        if(minutes == 59){
            minutes =0;
            heure++;
        }else{
            minutes++;
        }

        TeleInfoSendXPL();  //envois de toutes les trame OneWire en XPL

    }

    loop_Udp();

    runBitlash();

    other_actual=millis()-other_prev_millis;
    //~ other_prev_millis=millis();

    if(other_actual>other_max)
        other_max=other_actual;

}
Пример #2
0
int main(int argc, char** argv) {
    const char true = 1;
    const char false = 0;
    const int kMaxReadLength = sizeof(StudentSt);
    char read_buf[kMaxReadLength];
    char peer_server[32] = "";
    int listen_updategrade_pid;
    int listen_lookupgrade_pid;
    int listen_mainupdate_pid;
    char has_new = 0;   //whether has new updates to backup
    int syncfail_count = 0;
    int update_count = 0;   //primary fails after three updates, this is the counter.

    //check argument validity
    if(argc != 3) {
        printf("Usage: server [mode] peer_server_address\n");
        printf("mode=p: primary \t mode=b:backup\n");
        return 0;
    }

    //address of the peer server
    strcpy(peer_server, argv[2]);

    switch(argv[1][0]) {
        case 'p':
            break;
        case 'b':
        i_know_its_dirty:         
            if((listen_mainupdate_pid = fork()) == 0) {
                //in child process
                listen_mainserver_update();
            }
            else {
                //in main process
                listen_mainserver_failure();
                kill(listen_mainupdate_pid, SIGKILL);
                printf("My turn to become primary!\n");
            }
            printf("-----------Main server fail detected-----------\n");
            printf("Switched to main server mode\n");
            break;
        default:
            printf("Usage: server [mode]\n");
            printf("mode=:m primary \t mode=b:backup\n");
            return 0;
    }


    if(pipe(pipe_fd) < 0) {
        printf("not able to create pipe\n");
        return -1;
    }

    //int flags;
    //set pipe to be non-blocking
    if(fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK) < 0) {
        printf("not able to set pipe props\n");
        return -1;
    }

    if((listen_updategrade_pid = fork()) == 0) {
        //in child, process 1
        listen_client_update();
    } else if((listen_lookupgrade_pid = fork()) == 0) {
        //in child, process 2
        listen_client_lookup();
    } else {
        //in parent
        //first student in list
        StudentSt *student_list = NULL;      
        
        //last student in un-backuped student list
        //need this one if sync fails, and some new updates from client
        StudentSt *current_student = NULL; 
        while(1) {
            while( read(pipe_fd[0], read_buf, kMaxReadLength) >0 ) {
                //client sent some update requests
                //printf("new data in pipe detected\ndata:%3d\n",(int) (*read_buf));
                has_new = true;
                if(!current_student){   //no students in list
                    current_student = (StudentSt*)malloc(sizeof(StudentSt));
                    student_list = current_student;
                } else {                //list non-emtpy, append
                    current_student->next = (StudentSt*)malloc(sizeof(StudentSt));
                    current_student = current_student->next;
                }

                current_student->next = NULL;
                //read_buf is in fact a StudentSt
                current_student->student_id = ((StudentSt*)read_buf)->student_id;
                current_student->grade = ((StudentSt*)read_buf)->grade;

                //debug
                //printf("new arrival: id: %d grade: %c\n", 
                 //      current_student->student_id, 
                 //      current_student->grade);
            } //inner while ends
            
            if(has_new) {
                //needs to sync to bakcup
                if(post_update("localhost", student_list)) {
                    //sync successful, cleanup
                    FreeStudentList(student_list);
                    has_new = false;
                    student_list = NULL;
                    current_student = NULL;
                    printf("updates sync-ed with bakcup server\n");
                    update_count++;
                    if(update_count >= 3) {
                        update_count = 0;
                        syncfail_count = 0; //will become backup, reset counters
                        kill(listen_lookupgrade_pid, SIGKILL);
                        kill(listen_updategrade_pid, SIGKILL);
                        post_failure(peer_server);
                        close(pipe_fd[0]);
                        close(pipe_fd[1]);
                        printf("Primary server says goodbye to the evil world.\n\n");
                        printf("Restarting as backup\n");
                        printf("Recovering..........Takes 30 secs.\n");
                        sleep(30);
                        printf("I'm here as the bakcup!\n");
                        printf("--------------------------------------------\n\n");
                        goto i_know_its_dirty;
                        exit(0);//not reached
                    }
                } else {
                    //sync failed, pass~will retry next time
                    //retry limit is 3. After that, save the update for next time
                    syncfail_count ++;
                    if(syncfail_count >= 3) {
                        //limit reached
                        printf("Backup server seems unreachable at the moment.\n");
                        has_new = false;
                        syncfail_count = 0;
                    }
                }
            }
        }
    }
}