Ejemplo n.º 1
0
int main(){
	int ret = kill_robot();
	
	switch(ret){
	case -2:
		printf("WARNING: invalid contents in PID_FILE\n");
		break;
	case -1:
		printf("Existing project failed to close and had to be killed.\n");
		break;
	case 0:
		printf("No existing program is running.\n");
		break;
	case 1:
		printf("An existing program was running and shut down cleanly.\n");
		break;
	default:
		return 0;
	}
	
	return 0;
}
Ejemplo n.º 2
0
void
process_robots ()
{
	int i, ret, rfd;
	struct pollfd *pfd;
	result_t result;
	char buf[STD_BUF];
	struct robot *robot;
	int to_talk;

	for (i = 0; i < max_robots; i++)
		fds[i].events = POLLIN | POLLOUT;

	do {
		to_talk = 0;
		rfd = -1;
		for (i = 0; i < max_robots; i++) {
			if (fds[i].fd != -1) {
				to_talk++;
				rfd = fds[i].fd;
			}
		}

		if (to_talk == 0) {
			if (winner)
				ndprintf(stdout, "[GAME] Winner found\n");
			else
				ndprintf(stdout, "[GAME] Ended - No winner\n");
			exit(EXIT_SUCCESS);
		}
		else if (to_talk == 1)
			winner = 1;

		poll(fds, max_robots, 10);
		for (i = 0; i < max_robots; i++) {
			robot = all_robots[i];
			pfd = &fds[i];
			if (pfd->fd == -1) // Dead robot
				continue;
			if (pfd->events == 0)
				to_talk--;

			if (pfd->revents & (POLLERR | POLLHUP)) { /* Error or disconnected robot -> kill */
				close(pfd->fd);
				pfd->fd = -1;
				kill_robot(robot);
				continue;
			}
			if (robot->damage >= 100) {
				sockwrite(pfd->fd, DEAD, "Sorry!");
				close(pfd->fd);
				pfd->fd = -1;
				continue;
			}

			if (!(pfd->revents & POLLIN))
				continue;

			if (!(pfd->revents & POLLOUT)) {
				close(pfd->fd);
				pfd->fd = -1;
				kill_robot(robot);
				continue;
			}

			ret = read(pfd->fd, buf, STD_BUF);
			switch (ret) {
				case -1:
					close(pfd->fd);
					pfd->fd = -1;
					kill_robot(robot);
					break;
				case 0:
					abort ();
				default:
					buf[ret] = '\0';
					result = execute_cmd(robot, buf);
					if (result.error) {
						sockwrite(pfd->fd, ERROR, "Violation of the protocol!\n");
						close(pfd->fd);
						pfd->fd = -1;
						kill_robot(robot);
					}
					else {
						if (result.cycle)
							pfd->events = 0;
						if (winner && pfd->fd == rfd)
							sockwrite(pfd->fd, END, "%d", result.result);
						else
							sockwrite(pfd->fd, OK, "%d", result.result);
					}
					break;
			}
		}
	} while (to_talk && !timer);
}
Ejemplo n.º 3
0
void close_kill_robot(struct pollfd *pfd, struct robot *robot)
{
	close(pfd->fd);
	pfd->fd = -1;
	kill_robot(robot);
}