Example #1
0
static int unlock(void)
{
	int unlocked = 0;
	if(bandwidth_semaphore == -1)
	{
		get_sem(&bandwidth_semaphore, (key_t)(BANDWIDTH_SEMAPHORE_KEY) );
	}
	if(bandwidth_semaphore != -1)
	{
		unlocked = unlock_sem(bandwidth_semaphore);
	}
	return unlocked;
	
}
Example #2
0
static int player_loop(t_lemipc *lemipc)
{
  while (g_check)
    {
      printf("still Alive !\n");
      lock_sem(lemipc->game->sem_id);
      if (check_death(lemipc) == 1)
      	return (0);
      move_random(lemipc);
      usleep(600000);
      unlock_sem(lemipc->game->sem_id);
    }
  return (1);
}
Example #3
0
int	launch_game(t_lemipc *lemipc)
{
  signal(SIGINT, catch_signal);
  srand(time(NULL));
  lock_sem(lemipc->game->sem_id);
  init_player(lemipc);
  unlock_sem(lemipc->game->sem_id);
  player_loop(lemipc);
  lemipc->game->map[lemipc->player.pos.y][lemipc->player.pos.x] = 0;
  if (count_players(lemipc) == 0)
    {
      printf("Winner is team %d\n", lemipc->player.team);
      free_ipcs(lemipc);
    }
  return (0);
}
/*
 * Send a board update message to all connected clients
 *
 * Excludes the user with write acces if the parameter
 * excl_w is set to 1. If the blackbpard should be broadcasted to all
 * connected clients, excl_w shoul be 0.
 */
void broadcast_blackboard(char *blackboard, int bsem_id, int excl_w) {
    struct cl_entry *current;
    struct net_board *board;
    int ret;
    int length;

    /*
     * Prepare the blackboard message
     */
    lock_sem(bsem_id);
    length = strlen(blackboard);
    board = build_board(blackboard, length);
    unlock_sem(bsem_id);
    
    /*
     * Iterate over the complete client list
     * This locks the mutex of the client list
     */
    current = start_iteration();

    while (current != NULL) {
        /* Don't broadcast to the user with write access */
        if ((current == get_write_user()) && excl_w) {
            current = iteration_next();
            continue;
        }
        ret = send(current->cdata->sfd, board, sizeof(struct net_header) + length, 0);
        if (ret < 0) {
            perror("send");
        }
        /* Select next client in list */
        current = iteration_next();
    }
    
    /* Unlock mutex of client list */
    end_iteration();
    free(board);
    log_debug("broadcasting agent: blackboard sent to all connected clients");
}