示例#1
0
int handle_event_idle(int event, int order_table[N_FLOORS][N_BUTTONS]) {
	int next_state = IDLE;
	switch (event) {

			case EMERGENCY_BUTTON:
				activate_emergency(order_table);
				next_state = EMERGENCY;
				break;

			case GO_UP:
				close_door();
				change_speed(S_STOP, S_UP);
				next_state = GOING_UP;
				break;

			case GO_DOWN:
				close_door();
				change_speed(S_STOP, S_DOWN);
				next_state = GOING_DOWN;
				break;
			
			case STOP:
				next_state = IDLE;
				break;
			
			case OBSTRUCTION:
				activate_emergency(order_table);
				next_state = EMERGENCY;
				break;
			
		}
	return next_state;
}
示例#2
0
//Each elevator is a while loop. 
//Check the global list and if it’s empty, block on the condition variable for blocking elevators. 
//When the elevator gets a person to service, it moves to the appropriate flo0or and opens its door. 
//It puts itself into the person’s e field, then signals the person and blocks until the person wakes it up. 
//When it wakes up, it goes to the person’s destination floor, opens its door, signals the person and blocks. 
//When the person wakes it up, it closes its door and re-executes its while loop.
//• Your elevator’s should call open door(), close door() from move to floor() appropriately. 
//All sleeping and printing should be done in elevator skeleton.c. 
//Thus, your final program that you hand in should not do any sleeping or any printing.
void *elevator(void *arg)
{
    Person *person_in_transit;
    for(;;)
    {

        pthread_mutex_lock(((Elevator*)arg)->es->lock);
        while (!((Queue*)((Elevator*)arg)->es->v)->count)
            pthread_cond_wait(((Queue*)((Elevator*)arg)->es->v)->cond, ((Elevator*)arg)->es->lock);
            if(!dll_empty(((Queue*)((Elevator*)arg)->es->v)->passengers)){
             person_in_transit = (Person*)jval_v(dll_val(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers)));
             dll_delete_node(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers));
            }
        // unlock the critial section.
        pthread_mutex_unlock(((Elevator*)arg)->es->lock);
       //move the elevator.
        --((Queue*)((Elevator*)arg)->es->v)->count;
        person_in_transit->from == ((Elevator*)arg)->onfloor?:move_to_floor(((Elevator*)arg), person_in_transit->from);
        //open the door
        open_door(((Elevator*)arg));
        //add the people to the elevator
        person_in_transit->e = ((Elevator*)arg); 
        //signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // have the elevator wait
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close door 
        close_door(((Elevator*)arg));
        // elevator moves
        move_to_floor(person_in_transit->e,person_in_transit->to);
        // open the door once move has completed.
        open_door(((Elevator*)arg));
        // signal the person
        pthread_mutex_lock(person_in_transit->lock);
        pthread_cond_signal(person_in_transit->cond);
        pthread_mutex_unlock(person_in_transit->lock);
        // block the elevator
        pthread_mutex_lock(((Elevator*)arg)->lock);
        pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock);
        pthread_mutex_unlock(((Elevator*)arg)->lock);
        // close the door
        close_door(((Elevator*)arg));
        // restart. 
    }
}
示例#3
0
void do_close( char_data* ch, char* argument )
{
  obj_data*         obj;
  exit_data*       door;
  thing_data*     thing;

  if( ( thing = one_thing( ch, argument, "close",
    (thing_array*) &ch->in_room->exits, ch->array,
    &ch->contents ) ) == NULL )
    return;

  if( ( obj = object( thing ) ) != NULL ) {
    include_closed = FALSE;
    close_object( ch, obj );
    include_closed = TRUE;
    return;
    }

  if( ( door = exit( thing ) ) != NULL ) {
    close_door( ch, door );   
    return;
    }

  send( ch, "%s isn't closable.\n\r", thing );
}
示例#4
0
文件: knockd.c 项目: bhuvi8/knock
void reload(int signum)
{
	PMList *lp;
	opendoor_t *door;

	vprint("Re-reading config file: %s\n", o_cfg);
	logprint("Re-reading config file: %s\n", o_cfg);

	for(lp = doors; lp; lp = lp->next) {
		door = (opendoor_t*)lp->data;
		close_door(door);
	}
	list_free(doors);

	parseconfig(o_cfg);

	vprint("Closing and re-opening log file: %s\n", o_logfile);
	logprint("Closing and re-opening log file: %s\n", o_logfile);

	/* close and re-open the log file */
	if(logfd) {
		fclose(logfd);
	}
	logfd = fopen(o_logfile, "a");
	if(logfd == NULL) {
		perror("warning: cannot open logfile");
	}

	return;
}
示例#5
0
static void goto_level(int position)
{
    current_position = position;
    levels[position].state = 0;
    open_door(position);
    close_door(position);
}
示例#6
0
int handle_event_stop_going_down(int event, int order_table[N_FLOORS][N_BUTTONS]) {
	int next_state = STOP_GOING_DOWN;	
	switch (event) {

			case EMERGENCY_BUTTON:
				activate_emergency(order_table);
				next_state = EMERGENCY;
				break;

			case GO_UP:
				if(timer_elapsed() > DOOR_DELAY){
					close_door();
					change_speed(S_STOP, S_UP);
					next_state = GOING_UP;
				}else{
					next_state = STOP_GOING_UP;
				}
				break;

			case GO_DOWN:
				if(timer_elapsed() > DOOR_DELAY){
					close_door();
					change_speed(S_STOP, S_DOWN);
					next_state = GOING_DOWN;
				}else{
					next_state = STOP_GOING_DOWN;
				}
				break;
			
			case STOP:
				if(timer_elapsed() > DOOR_DELAY){
					close_door();
					next_state = IDLE;
				}else{
					next_state = STOP_GOING_DOWN;
				}
				break;
			
			case OBSTRUCTION:
				timer_set();
				next_state = STOP_GOING_DOWN;
				break;
			
		}
		return next_state;
}
示例#7
0
文件: knockd.c 项目: airwoflgh/knock
/* Remove a one time sequence from the corresponding file (after a successful
 * knock attempt)
 */
int disable_used_one_time_sequence(opendoor_t *door)
{
	long pos = get_current_one_time_sequence_position(door);
	if(pos >= 0) {
		if(fseek(door->one_time_sequences_fd, pos, SEEK_SET) < 0) {
			fprintf(stderr, "error while disabling used one time sequence for door %s --> disabling the door\n", door->name);
			logprint("error while disabling used one time sequence for door %s --> disabling the door\n", door->name);
			close_door(door);
			return(1);
		}
		if(fputc('#', door->one_time_sequences_fd) == EOF) {
			fprintf(stderr, "error while disabling used one time sequence for door %s --> disabling the door\n", door->name);
			logprint("error while disabling used one time sequence for door %s --> disabling the door\n", door->name);
			close_door(door);
			return(1);
		}
	}
	return(0);
}
示例#8
0
文件: knockd.c 项目: airwoflgh/knock
/* Read a new sequence from the one time sequences file and update the door.
 */
int get_new_one_time_sequence(opendoor_t *door)
{
	rewind(door->one_time_sequences_fd);
	if(get_next_one_time_sequence(door) < 0) {
		/* disable the door by removing it from the doors list if there are no sequences anymore */
		fprintf(stderr, "no more sequences left in the one time sequences file for door %s --> disabling the door\n", door->name);
		logprint("no more sequences left in the one time sequences file for door %s --> disabling the door\n", door->name);
		close_door(door);
		return(1);
	}
	dprint_sequence(door, "new sequence for door %s: ", door->name);

	return(0);
}
示例#9
0
static QCC_BOOL parse(alljoyn_busattachment bus, alljoyn_observer observer, char* input)
{
    char* cmd;
    char* arg;
    char* argend;

    /* ignore leading whitespace */
    for (cmd = input; *cmd && isspace(*cmd); ++cmd) {
    }
    if (!*cmd) {
        return QCC_TRUE;
    }

    /* look for an argument */
    for (arg = cmd + 1; *arg && isspace(*arg); ++arg) {
    }
    for (argend = arg; *argend && !isspace(*argend); ++argend) {
    }
    *argend = '\0';

    switch (*cmd) {
    case 'q':
        return QCC_FALSE;

    case 'l':
        list_doors(bus, observer);
        break;

    case 'o':
        open_door(bus, observer, arg);
        break;

    case 'c':
        close_door(bus, observer, arg);
        break;

    case 'k':
        knock_and_run(bus, observer, arg);
        break;

    case 'h':
    default:
        help();
        break;
    }

    return QCC_TRUE;
}
示例#10
0
void close_men()
{
	object room;

	if(!objectp( room = find_object(query("outroom")) ))
		room = load_object(query("outroom"));
	if(objectp(room))
	{
		if (  is_open() ) 
		{
			close_door();
			message("vision", query("door_name")+"吱吱呀呀地自己合上了。\n", this_object());
			room->close_door();
			message("vision", query("door_name")+"吱吱呀呀地自己合上了。\n", room);
		}
	}
}
示例#11
0
void
compute_closer_floors (void)
{
  size_t i;

  for (i = 0; i < closer_floor_nmemb; i++) {
    struct closer_floor *c = &closer_floor[i];
    if (c->pressed && ! c->unresponsive) {
      if (! c->noise) {
        alert_guards (&c->p);
        play_sample (closer_floor_sample, &c->p, -1);
        c->noise = true;
      }
      close_door (c->p.l, c->event);
    } else c->noise = false;
  }
}
示例#12
0
int close_men()
{
	object room;

	room = find_object(query("restroom"));
	if(!objectp(room)) room = load_object(query("restroom"));
	if(objectp(room))
	{
		if (  is_open() ) 
		{
			close_door();
			message("vision", query("door_name")+"吱吱呀呀地自己合上了。\n", this_object());
			room->close_door();
			message("vision", query("door_name")+"吱吱呀呀地自己合上了。\n", room);
		}
	}
	else message("vision", "发现错误,请通知巫师解决。.\n", room);
}
示例#13
0
文件: knockd.c 项目: airwoflgh/knock
void reload(int signum)
{
	PMList *lp;
	opendoor_t *door;
	int res_cfg;

	vprint("Re-reading config file: %s\n", o_cfg);
	logprint("Re-reading config file: %s\n", o_cfg);

	for(lp = doors; lp; lp = lp->next) {
		door = (opendoor_t*)lp->data;
		close_door(door);
	}
	list_free(doors);

	res_cfg = parseconfig(o_cfg);

	vprint("Closing log file: %s\n", o_logfile);
	logprint("Closing log file: %s\n", o_logfile);

	/* close the log file */
	if(logfd) {
		fclose(logfd);
	}

	if(res_cfg) {
		exit(1);
	}

	vprint("Re-opening log file: %s\n", o_logfile);
	logprint("Re-opening log file: %s\n", o_logfile);

	/* re-open the log file */
	logfd = fopen(o_logfile, "a");
	if(logfd == NULL) {
		perror("warning: cannot open logfile");
	}

	/* Fix issue #2 by regenerating the PCAP filter post config file re-read */
	generate_pcap_filter();

	return;
}
示例#14
0
int main(int argc, char *argv[]) {
	/* variables for serial communication */
	int fd, c;
	struct termios current_io, new_io;
	char buf[1024];
	char devfile[1024];
	int baudrate = DEFAULT_BAUDRATE;

	bzero(buf, sizeof(buf));
	strncpy(devfile, DEFAULT_DEVICE, sizeof(devfile));
	for (c = 1; c < argc; c++) {
		if (1 == sscanf(argv[c], "--devfile=%s", devfile)) {
			continue;
		}
	}

	fd = open(devfile, O_RDWR | O_NOCTTY);
	if (fd < 0) {
		perror(devfile);
		exit(-1);
	}

	tcgetattr(fd, &current_io);
	bzero(&new_io, sizeof(new_io));
	new_io.c_cflag = baudrate | CRTSCTS | CS8 | CLOCAL | CREAD;
	new_io.c_iflag = IGNPAR | ICRNL;
	new_io.c_oflag = 0;
	new_io.c_lflag = ICANON;
	new_io.c_cc[VEOF] = 4;
	new_io.c_cc[VMIN] = 1;

	tcflush(fd, TCIFLUSH);
	tcsetattr(fd, TCSANOW, &new_io);

	for (c = 1; c < argc; c++) {
		if (strcmp(argv[c], "--close") == 0)
		{
			close_door(fd);
			break;
		}

		if (strcmp(argv[c], "--open") == 0)
		{
			open_door(fd);
			break;
		}
	}

	{
		int state = get_door_state(fd);
		if(state == IS_OPENED)
		{
			puts("opened");
		}

		if(state == IS_CLOSED)
		{
			puts("closed");
		}
	}

	/* restore current io settings */
	tcsetattr(fd, TCSANOW, &current_io);

	/* close serial communication */
	close(fd);

	fflush(stdout);
	return 0;
}
示例#15
0
文件: game.cpp 项目: smarmy/HellRogue
void _select_direction()
{
    const std::string move_keys = "lkjhyubn12346789";
    int state = statestack_top();

    char key = io::getchar();

    if (char_in(key, move_keys))
    {
        if (state == STATE_CLOSE_DOOR)
        {
            int x, y;
            x = player.pos.x;
            y = player.pos.y;

            compute_xy_by_direction(direction_from_key(key), &x, &y);

            tile_t* tile = get_tile_at(current_dungeon, x, y);
            if (tile->id == TILE_DOOR_OPEN)
            {
                if (place_free(current_dungeon, x, y)
                        && count_items_at(current_dungeon, x, y) == 0)
                {
                    close_door(current_dungeon, &player, x, y);
                    player.ap -= player.speed;
                }
                else
                {
                    append_msg_log("The door is blocked!");
                }
            }
            else if (tile->id == TILE_DOOR_CLOSED)
            {
                append_msg_log("That door is already closed.");
            }
            else
            {
                append_msg_log("There is no door there!");
            }
            statestack_pop();
        }
        else if (state == STATE_CHAT)
        {
            int x = player.pos.x;
            int y = player.pos.y;

            compute_xy_by_direction(direction_from_key(key), &x, &y);
            creature_t* creature = get_creature_at(current_dungeon, x, y);

            if (creature)
            {
                chat(creature);
            }
            else
            {
                append_msg_log("There is no one there to talk to!");
            }

            statestack_pop();
        }

        return;
    }

    if (key == '\x1B')
    {
        append_msg_log("Nevermind.");
        statestack_pop();
    }
}
示例#16
0
int do_close (string str)
{
  close_door   ("east");
  lock_door ("east");
  return 1;
}
示例#17
0
void door_routine(void)
//This function opens the door and exits when the door is locked
{
	//variables for this function
	char pass[PASS_LENGTH],pass_2[PASS_LENGTH];
	int x,y,exit=0,loop; //x,y,temp are used to check return values from passcode_func. exit is used to hold infinite loop till door closed
	
	//initialisation
	for(loop=0;loop<PASS_LENGTH;++loop)
	{
		pass[loop]=PASS_NULL;
		pass_2[loop]=PASS_NULL; 
	}
	
	//first, open the door
	open_door();
	Write("OPEN");

	while(exit!=1) //function will only exit when door closed
	{
		if(key_flag==1)
		{
			if(key_pressed==KEY_CHANGE)
			{
				while(scan()==1); //debounce
				key_flag=0;
				Write("Chan"); 		//indicate that they've entered pass change mode
				delay_10ms(150); 	//delay before blanking screen
				Write("    ");
				
				//passcode change mode
				x=passcode_entering(pass,30);
				if(x==KEY_ENTER) //only continue if passcode_func exited due to enter being pressed
				{
					Write("    ");
					y=passcode_entering(pass_2,30);
					if(y==KEY_ENTER)
					{
						if(check_arrays(pass,pass_2))
						{
							pass_write(pass);
							Write("done"); 		//say 'done to indicate password changed.
						}
						else
						{
							Write("Err ");
						}
						delay_10ms(150); //1.5 second delay   >>Could change this to "wait for enter pressed."
					}
				}
				
				//clear values stored
				for(loop=0;loop<PASS_LENGTH;++loop)
				{
					pass[loop]=PASS_NULL;
					pass_2[loop]=PASS_NULL;
				}
				Write("OPEN");
			
			}
			
			if(key_pressed==KEY_ENTER)
			{
				while(scan()==1); //debounce
				key_flag=0;
				
				//now close the door
				close_door();
				Write("Shut");
				exit=1; //door has been closed, function can now exit.
			}
			
			if(key_pressed!=KEY_ENTER&&key_pressed!=KEY_CHANGE) //just to handle/clear random numeric key presses whilst door is open
			{
				while(scan()==1); //debounce
				key_flag=0;
			}
		}
	}//end of while loop
}
示例#18
0
void *elevator(void *arg){
    Elevator *e = (Elevator *) arg; 
    Dllist item, next, pickup;
    Person *p;
    int direction = 1;
    pickup = new_dllist();

    while(1){

        if(e -> onfloor >= top) direction = -1;
        else if(e -> onfloor <= 1) direction = 1;
        //printf("\tElevator[%i] on floor %i going %s:\n",
        //        e -> id, e -> onfloor, direction == 1 ? "up": "down"); 

        /* pick people up */
        pthread_mutex_lock(lock);

        item = dll_first(people);
        while(!dll_empty(people) && item != dll_nil(people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            //printf("\t\tShould I get %s %s going from %i to %i? ",
            //        p -> fname, p -> lname, p -> from, p -> to);

            if(e -> onfloor == p -> from){
                if(p -> to > e -> onfloor && direction == 1 || 
                   p -> to < e -> onfloor && direction == -1){
                    dll_append(pickup, item -> val);
                    dll_delete_node(item);
                    //printf("yes!\n");

                }
                //else printf("no!\n");
            }
            //else printf("no!\n");

            item = next;
        }

        pthread_mutex_unlock(lock);

        item = dll_first(pickup);
        while(!dll_empty(pickup) && item != dll_nil(pickup)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(!e -> door_open) open_door(e);
            pthread_mutex_lock(p -> lock);

            p -> e = e;

            pthread_cond_signal(p -> cond);
            pthread_mutex_lock(e -> lock);
            pthread_mutex_unlock(p -> lock);

            pthread_cond_wait(e -> cond, e -> lock);
            pthread_mutex_unlock(e -> lock);

            dll_delete_node(item);
            item = next;
        }
        if(e -> door_open) close_door(e);

        move_to_floor(e, e -> onfloor + direction);

        /* drop people off */
        item = dll_first(e -> people);
        while(!dll_empty(e -> people) && item != dll_nil(e -> people)){
            next = dll_next(item);
            p = (Person *) item -> val.v;

            if(p -> to == e -> onfloor){
               if(!e -> door_open) open_door(e);
                
               pthread_mutex_lock(p -> lock);
               pthread_cond_signal(p -> cond);
               pthread_mutex_lock(e -> lock);
               pthread_mutex_unlock(p -> lock);

               pthread_cond_wait(e -> cond, e -> lock);
               pthread_mutex_unlock(e -> lock);
            }

            item = next;
        }
        //if(e -> door_open) close_door(e);

    }

    return NULL;
}