Ejemplo n.º 1
0
void* orquestador(void* plat) {
    t_plataforma* plataforma = (t_plataforma*) plat;
    t_orquestador* self = orquestador_create(PUERTO_ORQUESTADOR, plataforma);

    t_socket_client* acceptClosure(t_socket_server* server) {
        t_socket_client* client = sockets_accept(server);

        t_mensaje* mensaje = mensaje_recibir(client);

        if (mensaje == NULL) {
            sockets_destroyClient(client);
            pthread_mutex_lock(&plataforma->logger_mutex);
            log_warning(plataforma->logger,
                        "%s:%d -> Orquestador - Error al recibir datos en el accept",
                        sockets_getIp(client->socket),
                        sockets_getPort(client->socket));
            pthread_mutex_unlock(&plataforma->logger_mutex);
            return NULL;
        }

        int tipo_mensaje = mensaje->type;

        mensaje_destroy(mensaje);

        switch (tipo_mensaje) {
        case M_HANDSHAKE_PERSONAJE:
            responder_handshake(client, plataforma->logger,
                                &plataforma->logger_mutex, "Orquestador");
            orquestador_guardar_personaje(self, client, plataforma);
            break;
        case M_HANDSHAKE_NIVEL:
            responder_handshake(client, plataforma->logger,
                                &plataforma->logger_mutex, "Orquestador");
            if (!procesar_handshake_nivel(self, client, plataforma)) {
                orquestador_send_error_message("Error al procesar el handshake",
                                               client);
                return NULL;
            }
            break;
        default:
            pthread_mutex_lock(&plataforma->logger_mutex);
            char* error_msg = string_from_format(
                                  "Tipo del mensaje recibido no válido tipo: %d",
                                  tipo_mensaje);
            log_warning(plataforma->logger, error_msg);
            mensaje_create_and_send(M_ERROR, string_duplicate(error_msg),
                                    strlen(error_msg) + 1, client);
            free(error_msg);
            pthread_mutex_unlock(&plataforma->logger_mutex);
            orquestador_send_error_message("Request desconocido", client);
            return NULL ;
        }

        return client;
    }
Ejemplo n.º 2
0
void* sockets_threadmain(void* arg) {
    int listenfd;
    struct graphhost_t* inst = arg;
    struct socketconn_t* conn;
    struct list_elem_t* elem;
    int maxfd;
    int fd;
    uint8_t ipccmd;

    fd_set readfds;
    fd_set writefds;
    fd_set errorfds;

    pthread_mutex_init(&inst->mutex, NULL);

    /* Create a list to store all the open connections */
    inst->connlist = list_create();
    inst->graphlist = list_create();

    /* Listen on a socket */
    listenfd = sockets_listen_int(inst->port, AF_INET, 0x00000000);
    if (listenfd == -1) {
        pthread_mutex_destroy(&inst->mutex);
        list_destroy(inst->graphlist);
        list_destroy(inst->connlist);
        pthread_exit(NULL);
        return NULL;
    }

    /* Set the running flag after we've finished initializing everything */
    inst->running = 1;

    while (1) {
        /* Clear the fdsets */
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&errorfds);

        /* Reset the maxfd */
        maxfd = listenfd;

        /* Add the file descriptors to the list */
        pthread_mutex_lock(&inst->mutex);
        for (elem = inst->connlist->start; elem != NULL; elem = elem->next) {
            conn = elem->data;
            fd = conn->fd;

            if (conn->orphan == 1) continue;

            if (maxfd < fd) maxfd = fd;
            if (conn->selectflags & SOCKET_READ) FD_SET(fd, &readfds);
            if (conn->selectflags & SOCKET_WRITE) FD_SET(fd, &writefds);
            if (conn->selectflags & SOCKET_ERROR) FD_SET(fd, &errorfds);
        }
        pthread_mutex_unlock(&inst->mutex);

        /* Select on the listener fd */
        FD_SET(listenfd, &readfds);

        /* ipcfd will recieve data when the thread needs to exit */
        FD_SET(inst->ipcfd_r, &readfds);

        /* Select on the file descrpitors */
        select(maxfd + 1, &readfds, &writefds, &errorfds, NULL);

        pthread_mutex_lock(&inst->mutex);
        for (elem = inst->connlist->start; elem != NULL; elem = elem->next) {
            conn = elem->data;
            fd = conn->fd;

            if (conn->orphan == 1) continue;

            if (FD_ISSET(fd, &readfds)) {
                /* Handle reading */
                sockets_readh(inst, inst->connlist, elem);
            }
            if (FD_ISSET(fd, &writefds)) {
                /* Handle writing */
                sockets_writeh(inst, inst->connlist, elem);
            }
            if (FD_ISSET(fd, &errorfds)) {
                /* Handle errors */
                sockets_close(inst->connlist, elem);
            }
        }

        /* Close all the file descriptors marked for closing */
        sockets_clear_orphans(inst->connlist);
        pthread_mutex_unlock(&inst->mutex);

        /* Check for listener condition */
        if (FD_ISSET(listenfd, &readfds)) {
            /* Accept connections */
            sockets_accept(inst->connlist, listenfd);
        }

        /* Handle IPC commands */
        if (FD_ISSET(inst->ipcfd_r, &readfds)) {
            read(inst->ipcfd_r, (char*)&ipccmd, 1);
            if (ipccmd == 'x') {
                break;
            }
        }
    }

    /* We're done, clear the running flag and clean up */
    inst->running = 0;
    pthread_mutex_lock(&inst->mutex);

    /* Mark all the open file descriptors for closing */
    for (elem = inst->connlist->start; elem != NULL; elem = elem->next) {
        sockets_close(inst->connlist, elem);
        /* We don't need to delete the element form the
           because we just delete all of them below. */
    }

    /* Actually close all the open file descriptors */
    sockets_clear_orphans(inst->connlist);

    /* Free the list of connections */
    list_destroy(inst->connlist);

    /* Close the listener file descriptor */
    close(listenfd);

    /* Clean up & free the global dataset list */
    for (elem = inst->graphlist->start; elem != NULL; elem = elem->next) {
        free(elem->data);
    }
    list_destroy(inst->graphlist);

    /* Destroy the mutex */
    pthread_mutex_unlock(&inst->mutex);
    pthread_mutex_destroy(&inst->mutex);

    pthread_exit(NULL);
    return NULL;
}
Ejemplo n.º 3
0
static int gdb_server(int port)
{
	int sock;
	int client;
	struct sockaddr_in addr;
	socklen_t len;
	int arg;
	struct gdb_data data;
	int i;

	sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (SOCKET_ISERR(sock)) {
		pr_error("gdb: can't create socket");
		return -1;
	}

	arg = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
		       (void *)&arg, sizeof(arg)) < 0)
		pr_error("gdb: warning: can't reuse socket address");

	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		printc_err("gdb: can't bind to port %d: %s\n",
			port, last_error());
		closesocket(sock);
		return -1;
	}

	if (listen(sock, 1) < 0) {
		pr_error("gdb: can't listen on socket");
		closesocket(sock);
		return -1;
	}

	printc("Bound to port %d. Now waiting for connection...\n", port);

	len = sizeof(addr);
	client = sockets_accept(sock, (struct sockaddr *)&addr, &len);
	if (SOCKET_ISERR(client)) {
		pr_error("gdb: failed to accept connection");
		closesocket(sock);
		return -1;
	}

	closesocket(sock);
	printc("Client connected from %s:%d\n",
	       inet_ntoa(addr.sin_addr), htons(addr.sin_port));

	gdb_init(&data, client);

	/* Put the hardware breakpoint setting into a known state. */
	printc("Clearing all breakpoints...\n");
	for (i = 0; i < device_default->max_breakpoints; i++)
		device_setbrk(device_default, i, 0, 0);

#ifdef DEBUG_GDB
	printc("starting GDB reader loop...\n");
#endif
	gdb_reader_loop(&data);
#ifdef DEBUG_GDB
	printc("... reader loop returned\n");
#endif
	closesocket(client);

	return data.error ? -1 : 0;
}