Exemplo n.º 1
0
/**
 * Return a target set of target_able monsters.
 */
struct point_set *target_get_monsters(int mode, monster_predicate pred)
{
	int y, x;
	int min_y, min_x, max_y, max_x;
	struct point_set *targets = point_set_new(TS_INITIAL_SIZE);

	/* Get the current panel */
	get_panel(&min_y, &min_x, &max_y, &max_x);

	/* Scan for targets */
	for (y = min_y; y < max_y; y++) {
		for (x = min_x; x < max_x; x++) {
			struct loc grid = loc(x, y);

			/* Check bounds */
			if (!square_in_bounds_fully(cave, grid)) continue;

			/* Require "interesting" contents */
			if (!target_accept(y, x)) continue;

			/* Special mode */
			if (mode & (TARGET_KILL)) {
				struct monster *mon = square_monster(cave, grid);

				/* Must contain a monster */
				if (mon == NULL) continue;

				/* Must be a targettable monster */
				if (!target_able(mon)) continue;

				/* Must be the right sort of monster */
				if (pred && !pred(mon)) continue;
			}

			/* Save the location */
			add_to_point_set(targets, grid);
		}
	}

	sort(targets->pts, point_set_size(targets), sizeof(*(targets->pts)),
		 cmp_distance);
	return targets;
}
Exemplo n.º 2
0
/**
 * Return a target set of target_able monsters.
 */
struct point_set *target_get_monsters(int mode)
{
	int y, x;
	int min_y, min_x, max_y, max_x;
	struct point_set *targets = point_set_new(TS_INITIAL_SIZE);

	/* Get the current panel */
	get_panel(&min_y, &min_x, &max_y, &max_x);

	/* Scan for targets */
	for (y = min_y; y < max_y; y++) {
		for (x = min_x; x < max_x; x++) {
			/* Check bounds */
			if (!square_in_bounds_fully(cave, y, x)) continue;

			/* Require "interesting" contents */
			if (!target_accept(y, x)) continue;

			/* Special mode */
			if (mode & (TARGET_KILL)) {
				/* Must contain a monster */
				if (!(cave->squares[y][x].mon > 0)) continue;

				/* Must be a targettable monster */
			 	if (!target_able(square_monster(cave, y, x))) continue;
			}

			/* Save the location */
			add_to_point_set(targets, y, x);
		}
	}

	sort(targets->pts, point_set_size(targets), sizeof(*(targets->pts)),
		 cmp_distance);
	return targets;
}
/**
 * return 0 if normal case
 * return plus value if non critical error occur
 * return minus value if critical error occur
 */
static int targetServerHandler(void)
{
	int err;
	struct msg_target_t log;
	struct target *target;

	target = target_ctor();
	if (target == NULL) {
		LOGW("(target == NULL) no more target can connected\n");
		return 1;
	}

	err = target_accept(target, manager.target_server_socket);
	if (err == 0) {
		/* send config message to target process */
		log.type = MSG_OPTION;
		log.length = snprintf(log.data, sizeof(log.data), "%llu",
				      prof_session.conf.use_features0) + 1;
		if (target_send_msg(target, &log) != 0)
			LOGE("fail to send data to target %p\n", target);

		/* send current instrument maps */
		send_maps_inst_msg_to(target);

		// make event fd
		target->event_fd = eventfd(EFD_CLOEXEC, EFD_NONBLOCK);
		if (target->event_fd == -1) {
			// fail to make event fd
			LOGE("fail to make event fd for target[%p]\n", target);
			goto TARGET_CONNECT_FAIL;
		}

		target->handler =
			ecore_main_fd_handler_add(target->event_fd,
						  ECORE_FD_READ,
						  target_event_cb,
						  (void *)target,
						  NULL, NULL);
		if (!target->handler) {
			LOGE("fail to add event fd for target[%p]\n", target);
			goto TARGET_CONNECT_FAIL;
		}

		// make recv thread for target
		if (makeRecvThread(target) != 0) {
			// fail to make recv thread
			LOGE("fail to make recv thread for target[%p]\n",
			     target);
			ecore_main_fd_handler_del(target->handler);
			goto TARGET_CONNECT_FAIL;
		}

		dec_apps_to_run();

		if ((manager.app_launch_timerfd > 0) && (get_apps_to_run() == 0)) {
			if (stop_app_launch_timer())
				LOGE("cannot stop app launch timer\n");
		}

		LOGI("target connected target[%p](running %d target)\n",
		     target, target_cnt_get() + 1);

		target_cnt_set(target_cnt_get() + 1);
		return 0;
	} else {
		// accept error
		LOGE("Failed to accept at target server socket\n");
	}

 TARGET_CONNECT_FAIL:
	if (target_cnt_get() == 0) {
		// if this connection is main connection
		return -1;
	} else {
		// if this connection is not main connection then ignore process by error
		target_dtor(target);
		return 1;
	}
}