Example #1
0
int 
main(int argc, char** argv)
{
	signal(SIGINT, shutdown_module);

	carmen_ipc_initialize(argc, argv);
	carmen_param_check_version(argv[0]);

	initialize_structures();

	read_parameters(argc, argv, car_config);

	initialize_ipc();

	subscribe_to_relevant_messages();

	carmen_ipc_dispatch();

	return 0;
}
Example #2
0
/**
 * Main function of the server application. It does a infinite loop unless a user exits the program.
 * @param[in] listener_socket File descriptor of the socket listening incoming connections.
 * @param[in] fifo            File descriptor of a temporary FIFO file.
 */
void
doServer(int listener_socket, int fifo) {
	int i, fdmax, newfd;
	players_list_s *players_list = NULL;
	games_list_s *games_list = NULL;
	threads_list_s *threads_list = NULL;
	pthread_mutex_t players_list_mutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_t games_list_mutex = PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_t threads_list_mutex = PTHREAD_MUTEX_INITIALIZER;
	fd_set base_rdfs, rdfs;
	sigset_t mask, oldmask;
	FD_ZERO(&base_rdfs);
	FD_SET(listener_socket, &base_rdfs);
	fdmax = max(listener_socket, fifo);
	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);
	sigprocmask(SIG_BLOCK, &mask, &oldmask);
	initialize_structures(&players_list, &games_list, &threads_list);
	printf("Four-in-a-line server started\n");
	while (work) {
		rdfs = base_rdfs;
		if (pselect(fdmax + 1, &rdfs, NULL, NULL, NULL, &oldmask) > 0) {
			for (i = 0; i <= fdmax; i++) {
				newfd = -1;
				if (FD_ISSET(i, &rdfs)) {
					if (i == listener_socket) {
						/* request from newly connected client */
						newfd = add_new_client(listener_socket);
						if (newfd > 0) {
							FD_SET(newfd, &base_rdfs);
							if (newfd > fdmax) {
								fdmax = newfd;
							}
							display_log(newfd);
							communicate(newfd, &base_rdfs, &players_list,
									&games_list, &threads_list,
									&players_list_mutex, &games_list_mutex,
									&threads_list_mutex);
						} else if (i == fifo) {
							/* trick to update base_rdfs set */
							char temp[1];
							bulk_read(fifo, temp, 1);
						}
					} else {
						/* request from already connected client */
						communicate(i, &base_rdfs, &players_list, &games_list,
								&threads_list, &players_list_mutex,
								&games_list_mutex, &threads_list_mutex);
					}
				}
			}
		} else {
			if (EINTR == errno)
				continue;
			ERR("pselect");
		}
	}
	pthread_mutex_destroy(&players_list_mutex);
	pthread_mutex_destroy(&games_list_mutex);
	pthread_mutex_destroy(&threads_list_mutex);
	destroy_players(players_list);
	destroy_games(games_list);
	destroy_threads(threads_list);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);
}