示例#1
0
文件: clock.c 项目: danfa688/TSEA81
/* set_task: reads messages from the user interface, and 
   sets the clock, or exits the program */ 
void * set_thread(void *unused)
{
    /* message array */ 
    char message[SI_UI_MAX_MESSAGE_SIZE]; 

    /* time read from user interface */ 
    int hours, minutes, seconds; 

    /* set GUI size */ 
    si_ui_set_size(400, 200); 

    while(1)
    {
        /* read a message */ 
        si_ui_receive(message); 
        /* check if it is a set message */ 
        if (strncmp(message, "set", 3) == 0)
        {
            time_from_set_message(message, &hours, &minutes, &seconds); 
            if (time_ok(hours, minutes, seconds))
            {
                clock_set_time(hours, minutes, seconds); 
            }
            else
            {
                si_ui_show_error("Illegal value for hours, minutes or seconds"); 
            }
        }
        else if (strncmp(message, "alarm", 5) == 0)
        {
            time_from_alarm_message(message, &hours, &minutes, &seconds);
            if (time_ok(hours, minutes, seconds))
            {
                clock_set_alarm(hours, minutes, seconds); 
            }
            else
            {
                si_ui_show_error("Illegal value for hours, minutes or seconds"); 
            }
        }
        else if (strcmp(message, "reset") == 0)
        {
            clock_reset_alarm();        
        }
        /* check if it is an exit message */ 
        else if (strcmp(message, "exit") == 0)
        {
            exit(0); 
        }
        /* not a legal message */ 
        else 
        {
            si_ui_show_error("unexpected message type"); 
        }
    }
}
示例#2
0
// This is the final process called by main()
// It is responsible for:
//   * Receiving and executing commands from the java GUI
//   * Killing off all processes when exiting the application
void uicommand_process(void) {
    int i;
    int current_person_id = 0;
    char message[SI_UI_MAX_MESSAGE_SIZE];
    int pid;
    while(1) {
        // Read a message from the GUI
        si_ui_receive(message);
        if(!strcmp(message, "new")) {
            // TODO:
            // * Check that we don't create too many persons
            // * fork and create a new person process (and
            //   record the new pid in person_pid[])
            if (current_person_id + 1 < MAX_N_PERSONS) {
                pid = fork();
                current_person_id++;
                if (!pid) {
                    person_process(current_person_id);
                } else {
                    person_pid[current_person_id] = pid;
                }
            }
        } else if (!strncmp(message, "test", 4)) {

            switch ((int) (message[5] - '0')) {
            case 0:
                for (i=0; i<MAX_N_PERSONS; i++) {
                    if (current_person_id + 1 < MAX_N_PERSONS) {
                        pid = fork();
                        current_person_id++;
                        if (!pid) {
                            person_process(current_person_id);
                        } else {
                            person_pid[current_person_id] = pid;
                        }
                    }
                }
            }
        } else if(!strcmp(message, "exit")) {
            // The code below sends the SIGINT signal to
            // all processes involved in this application
            // except for the uicommand process itself
            // (which is exited by calling exit())
            kill(uidraw_pid, SIGINT);
            kill(lift_pid, SIGINT);
            kill(liftmove_pid, SIGINT);
            kill(guard_pid, SIGINT);
            for(i=0; i < MAX_N_PERSONS; i++) {
                if(person_pid[i] > 0) {
                    kill(person_pid[i], SIGINT);
                }
            }
            exit(0);
        }
    }
}
示例#3
0
/*
There shall be one task, called user_task, which receives commands from the graphical user interface. This task shall create new person tasks. A new person task shall be created when the text string new is received. The task user_task shall also contain functionality for termination of the program. The program shall be terminated when the text string exit is received. 
*/
void user_task(void)
{
    /* set size of GUI window */ 
    si_ui_set_size(670, 700); 

	int n_persons = 0;
	
	/* message array */ 
    char message[SI_UI_MAX_MESSAGE_SIZE]; 

    while(1)
    {
        /* read a message */ 
        si_ui_receive(message); 
        /* check if it is a set time message */ 
        if (strncmp(message, "new", 3) == 0)
        {
		printf("blaba!!!!\n");
			if (n_persons == MAX_N_PERSONS)
			{
				si_ui_show_error("Failure to comply: Overpopulation!");
			} else {
			
				int id = n_persons++;
				si_task_create(passenger_task, &Passenger_Stack[id][STACK_SIZE-1], 17);
			
				/* send id message to created task */ 
				si_message_send((char *) &id, sizeof(int), id_to_task_id(id)); 
			}
        }
        /* check if it is an exit message */ 
        else if (strcmp(message, "exit") == 0)
        {
            exit(0); 
        }
        /* not a legal message */ 
        else 
        {
            si_ui_show_error("unexpected message type"); 
        }
    }
}
示例#4
0
/* exit_task: used for closing the program */ 
void exit_task(void)
{
    /* message array */ 
    char message[SI_UI_MAX_MESSAGE_SIZE]; 

    while(1)
    {
        /* read a message */ 
        si_ui_receive(message); 
        /* check if it is an exit message */ 
        if (strcmp(message, "exit") == 0)
        {
            exit(0); 
        }
        /* not a legal message */ 
        else 
        {
            si_ui_show_error("unexpected message type"); 
        }
    }
}
示例#5
0
/*
There shall be one task, called user_task, which receives commands from the graphical user interface. This task shall create new person tasks. A new person task shall be created when the text string new is received. The task user_task shall also contain functionality for termination of the program. The program shall be terminated when the text string exit is received. 
*/
void user_task(void)//TODO
{
    /* set size of GUI window */ 
    si_ui_set_size(670, 700); 

	int n_persons = 0;
	
	/* message array */ 
    char message[SI_UI_MAX_MESSAGE_SIZE]; 

    while(1)
    {
        /* read a message */ 
        si_ui_receive(message); 
        /* check if it is a set time message */ 
        if (strncmp(message, "new", 3) == 0)
        {
		/*printf("Spawning...\n");*/
			if (n_persons == MAX_N_PERSONS)
			{
				si_ui_show_error("Failure to comply: Overpopulation!");
			} else {
			
				int id = n_persons++;
                create_passenger(id, 17);
			}
        }
        /* check if it is an exit message */ 
        else if (strcmp(message, "exit") == 0)
        {
            exit(0); 
        }
        /* not a legal message */ 
        else 
        {
            si_ui_show_error("unexpected message type"); 
        }
    }
}
示例#6
0
/* set_task: reads messages from the user interface... */ 
void set_task(void)
{
    /* message array */ 
    char message[SI_UI_MAX_MESSAGE_SIZE]; 

    /* time read from user interface */ 
    int hours = 0, minutes = 0, seconds = 0; 

    /* set GUI size */ 
    si_ui_set_size(400, 200); 

    while(1)
    {
        /* read a message */ 
        si_ui_receive(message); 
        /* check if it is a set message */ 
        if (strncmp(message, "set:", 4) == 0)
        {
            time_from_set_message(message, &hours, &minutes, &seconds); 
            if (time_ok(hours, minutes, seconds))
            {
                set_time(hours, minutes, seconds); 
            }
            else
            {
                si_ui_show_error("Illegal value for hours, minutes or seconds"); 
            }
        }
        /* check if it is an alarm message */ 
	else if (strncmp(message, "alarm:", 6) == 0)
	{
	    time_from_alarm_message(message, &hours, &minutes, &seconds);
	    if (time_ok(hours, minutes, seconds))
            {
                set_alarm(hours, minutes, seconds);
				display_alarm_time(hours,minutes,seconds);
            }
            else
            {
                si_ui_show_error("Illegal value for hours, minutes or seconds"); 
            }
	}
	/* check if it is an reset message */ 
	else if (strncmp(message, "reset", 5) == 0)
	{	
		erase_alarm_time();
		erase_alarm_text();
		reset_alarm();
	}
	/* check if it is an exit message */ 
        else if (strcmp(message, "exit") == 0)
        {
            exit(0); 
        }
        /* not a legal message */ 
        else 
        {
            si_ui_show_error("unexpected message type"); 
        }
    }
}