Esempio n. 1
0
void* thread_Cpu(void* id){

	int32_t thread_id = (void*) id;

	sock_t* socket_Planificador=create_client_socket(arch->ip_planificador,arch->puerto_planificador);
	int32_t resultado = connect_to_server(socket_Planificador);
	if(resultado == ERROR_OPERATION ) {
		log_error(loggerError, ANSI_COLOR_RED "CPU: %d - Error al conectar al planificador" ANSI_COLOR_RESET, thread_id);
		return NULL;
	}
	log_debug(loggerDebug, "Conectado con el planificador la cpu: %d", thread_id);

	/*Me guardo en mi list de sockets los fd*/
	t_sockets* sockets=list_get(socketsCPU, thread_id);
	sockets->socketPlanificador->fd = socket_Planificador->fd;

	sock_t* socket_Memoria=create_client_socket(arch->ip_memoria,arch->puerto_memoria);
	if(connect_to_server(socket_Memoria)!=SUCCESS_OPERATION){
		log_error(loggerError, ANSI_COLOR_RED "CPU: %d - No se puedo conectar con la memoria, se aborta el proceso" ANSI_COLOR_RESET, thread_id);
        return NULL;
	}
	log_debug(loggerDebug, "Conectado con la memoria cpu:%d", thread_id);
	sockets->socketMemoria->fd = socket_Memoria->fd;

	//enviarle al planificador NUEVA_CPU y su id;
	header_t* header_nueva_cpu = _create_header(NUEVA_CPU, sizeof(int32_t));
	int32_t enviado =_send_header (socket_Planificador, header_nueva_cpu);
	if (enviado !=SUCCESS_OPERATION){
		log_error(loggerError, ANSI_COLOR_RED "Se perdio la conexion con el Planificador de la cpu: %d" ANSI_COLOR_RESET, thread_id);
		return NULL;}
	enviado = _send_bytes(socket_Planificador, &thread_id,sizeof(int32_t));
	if (enviado !=SUCCESS_OPERATION){
		log_error(loggerError, ANSI_COLOR_RED "CPU: %d - Se perdio la conexion con el Planificador" ANSI_COLOR_RESET, thread_id);
		return NULL;}
	free(header_nueva_cpu);

	pthread_t CPUuse;
	int32_t resultado_uso = pthread_create(&CPUuse, NULL, thread_Use, (void*) thread_id );
	if (resultado_uso != 0) {
		log_error(loggerError, ANSI_COLOR_RED "Error al crear el hilo de uso de CPU número:%d"ANSI_COLOR_RESET, id);
		envioDie(thread_id);
        return NULL;
	}

	int32_t finalizar = 1;
	int32_t resul_Mensaje_Recibido;
	header_t* header_planificador = _create_empty_header() ;
	while (finalizar == 1)	{
		resul_Mensaje_Recibido = _receive_header(socket_Planificador, header_planificador);
		if (resul_Mensaje_Recibido !=SUCCESS_OPERATION ) {
			log_error(loggerError, ANSI_COLOR_RED "CPU_ID:%d - Se perdio la conexion con el Planificador" ANSI_COLOR_RESET, thread_id);
			finalizar = 0;
			break;
		}
		tipo_Cod_Operacion (thread_id,header_planificador);
	}
	log_info(loggerInfo, ANSI_COLOR_BOLDBLUE "CPU_ID:%d->Finaliza sus tareas, hilo concluido" ANSI_COLOR_RESET, thread_id);
	free(header_planificador);
	return NULL;
}
Esempio n. 2
0
void* thread_Use(void* thread_id){
	int32_t id = (void*)thread_id;
	tiempoAcumulado[id]=0;tiempoInicial[id]=0;tiempoFinal[id]=0;			//inicializo
	int32_t toAnterior;int32_t tfAnterior;int32_t porcentaje;				//defino variables necesarias
	sock_t* socketPlanificador=getSocketPlanificador(id);
	int32_t finalizar=1;
	while(finalizar){finalizar=obtengoSegundos();}
	while(TRUE){
		if(estado[id]==1){					//si esta ejecutando
			tiempoAcumulado[id]+=60-tiempoInicial[id];
		}
		porcentaje=tiempoAcumulado[id]*10/6;
		if(tiempoFinal[id]==tfAnterior && tiempoInicial[id]==toAnterior){
			if(estado[id]==1) {				//si en un minuto no cambio y esta ejecutando
				porcentaje=100;
				tiempoAcumulado[id]=60;
			}
			if(estado[id]==0) {				//si en un minuto no cambio y no esta ejecutando
				porcentaje=0;
				tiempoAcumulado[id]=0;
			}
		}
		log_debug(loggerDebug,ANSI_COLOR_BOLDGREEN"CPU:%d El porcentaje de uso es: %d%%(%d/60)" ANSI_COLOR_RESET,id, porcentaje, tiempoAcumulado[id]);
		tiempoAcumulado[id]=0;
		header_t* header_uso_cpu = _create_header(UTILIZACION_CPU, 2*sizeof(int32_t));
		int32_t enviado = _send_header (socketPlanificador, header_uso_cpu);
		if(enviado == ERROR_OPERATION ) exit(1);
		enviado = _send_bytes(socketPlanificador,&id,sizeof(int32_t));
		if(enviado == ERROR_OPERATION ) exit(1);
		enviado = _send_bytes(socketPlanificador,&porcentaje,sizeof(int32_t));
		if(enviado == ERROR_OPERATION ) exit(1);
		free(header_uso_cpu);
		toAnterior=tiempoInicial[id];tfAnterior=tiempoFinal[id];
		sleep(60);
	}
	return NULL;
}
Esempio n. 3
0
void recibir_operaciones_memoria(sock_t* socketMemoria) {

    int32_t finalizar = true;
    int32_t recibido;
    char* pedido_serializado;
    header_t* header = _create_empty_header();

    while(finalizar) {

        recibido = _receive_header(socketMemoria, header);

        if(recibido == ERROR_OPERATION) return;
        if(recibido == SOCKET_CLOSED) {
            socketMemoria = accept_connection(socketServidor);
            //set_operation_code(header, 5);
            header->cod_op=5;
        }

        switch(get_operation_code(header)) {

        case LEER_PAGINA: {
            pedido_serializado = malloc(get_message_size(header));
            recibido = _receive_bytes(socketMemoria, pedido_serializado, get_message_size(header));
            if(recibido == ERROR_OPERATION) return;
            t_pagina* pagina_pedida = deserializar_pedido(pedido_serializado);
            leer_pagina(pagina_pedida);
            sleep(arch->retardo);
            header_t* headerMemoria = _create_header(RESULTADO_OK, sizeof(int32_t)+pagina_pedida->tamanio_contenido);
            int32_t enviado = _send_header(socketMemoria, headerMemoria);
            enviado=_send_bytes(socketMemoria, &pagina_pedida->tamanio_contenido, sizeof(int32_t));
            enviado=_send_bytes(socketMemoria, pagina_pedida->contenido, pagina_pedida->tamanio_contenido);
            log_debug(loggerDebug, "Se leyeron :%d bytes", pagina_pedida->tamanio_contenido);
            if(enviado == ERROR_OPERATION) return;
            free(pagina_pedida->contenido);
            free(pagina_pedida);
            free(pedido_serializado);
            free(headerMemoria);
            break;
        }

        case ESCRIBIR_PAGINA: {
            pedido_serializado = malloc(get_message_size(header));
            recibido = _receive_bytes(socketMemoria, pedido_serializado, get_message_size(header));
            if(recibido == ERROR_OPERATION) return;
            t_pagina* pagina_pedida = deserializar_pedido(pedido_serializado);
            log_debug(loggerDebug, "Me llega a escribir pid: %d, pagina:%d, tam:%d, contenido:%s", pagina_pedida->PID, pagina_pedida->nro_pagina, pagina_pedida->tamanio_contenido, pagina_pedida->contenido);
            int32_t resultado= escribir_pagina(pagina_pedida);
            sleep(arch->retardo);
            if (resultado == RESULTADO_ERROR) {
                header_t* headerMemoria = _create_header(RESULTADO_ERROR, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                free(headerMemoria);
                return;
            } else if (resultado == RESULTADO_OK) {
                header_t* headerMemoria = _create_header(RESULTADO_OK, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                free(headerMemoria);
            }
            free(pedido_serializado);
            free(pagina_pedida);
            break;
        }

        case RESERVAR_ESPACIO: {
            t_pedido_memoria* pedido_memoria = malloc(sizeof(t_pedido_memoria));
            recibido = _receive_bytes(socketMemoria, &(pedido_memoria->pid), sizeof(int32_t));
            if (recibido == ERROR_OPERATION)return;

            recibido = _receive_bytes(socketMemoria, &(pedido_memoria->cantidad_paginas),sizeof(int32_t));
            if (recibido == ERROR_OPERATION)return;

            log_debug(loggerDebug, "Recibi este pedido: %d y cant paginas %d", pedido_memoria->pid, pedido_memoria->cantidad_paginas);

            int32_t operacionValida= reservarEspacio(pedido_memoria);
            sleep(arch->retardo);
            if (operacionValida == RESULTADO_ERROR) {
                header_t* headerMemoria = _create_header(RESULTADO_ERROR, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                log_debug(loggerDebug, "No se reservo el espacio solicitado");
                free(headerMemoria);

            } else if (operacionValida == RESULTADO_OK) {
                header_t* headerMemoria = _create_header(RESULTADO_OK, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                log_info(loggerInfo, ANSI_COLOR_GREEN"Se reservo el espacio solicitado" ANSI_COLOR_RESET);
                free(headerMemoria);
            }
            break;
        }

        case BORRAR_ESPACIO: {
            int32_t PID;
            recibido = _receive_bytes(socketMemoria, &(PID), sizeof(int32_t));
            if (recibido == ERROR_OPERATION)return;
            int32_t operacionValida= borrarEspacio(PID);
            sleep(arch->retardo);
            if (operacionValida == RESULTADO_ERROR) {
                header_t* headerMemoria = _create_header(RESULTADO_ERROR, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                log_debug(loggerDebug, "Finalizado erroneo");
                free(headerMemoria);
                return;
            } else if (operacionValida == RESULTADO_OK) {
                header_t* headerMemoria = _create_header(RESULTADO_OK, 0);
                int32_t enviado = _send_header(socketMemoria, headerMemoria);
                if(enviado == ERROR_OPERATION) return;
                log_debug(loggerDebug, "Finalizado correcto, se envia:%d", RESULTADO_OK);
                free(headerMemoria);
            }
            break;
        }

        case RECONEXION : {
            log_info(loggerInfo, ANSI_COLOR_BOLDYELLOW "Se conecto una nueva instancia de la memoria" ANSI_COLOR_RESET);
            break;
        }

        default: {
            log_error(loggerError, ANSI_COLOR_BOLDRED "Se recibió un codigo de operación no valido"ANSI_COLOR_RESET);
        }

        }
    }
    free(header);
}
Esempio n. 4
0
static bool_t CC_CALL _http_callback(cc_event_base_t *base, cc_event_t *e, pvoid_t args)
{
	cckit_http_t *h = (cckit_http_t*)e->args[0];
	if (h == NULL) {
		cc_event_delete(base, e);
		return FALSE;
	}

	if (CC_ISSET_BIT(CC_EVENT_DISCONNECT, e->events)) {
		h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTP_SOCKET_CONNECT_FAILED);
		_cckit_destroy_http_request(&h);
		return FALSE;
	}


	if (CC_ISSET_BIT(CC_EVENT_CONNECT, e->events)) {
        
#ifdef CC_OPENSSL_HTTPS
        if (h->address->scheme == CC_SCHEME_HTTPS) {
            h->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
            if (h->ssl_ctx == NULL) {
                h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTPS_ERROR);
                return _cckit_destroy_http_request(&h);
            }
            
            h->ssl = SSL_new(h->ssl_ctx);
            if (h->ssl == NULL) {
                h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTPS_ERROR);
                return _cckit_destroy_http_request(&h);
            }
            
            if (SSL_set_fd(h->ssl, (int)e->io_handle) == 0) {
                h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTPS_ERROR);
                return _cckit_destroy_http_request(&h);
            }
            /*
            RAND_poll();
            while (RAND_status() == 0) {
                unsigned short rand_ret = rand() % 65536;
                RAND_seed(&rand_ret, sizeof(rand_ret));
            }*/
            sys_set_socket_nonblock(e->io_handle, FALSE);

            if (SSL_connect(h->ssl) != 1) {
                h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTPS_ERROR);
                return _cckit_destroy_http_request(&h);
            }
            sys_set_socket_nonblock(e->io_handle, TRUE);
        }
#endif
        if (_send_header(e, h) == FALSE) {
            _cckit_destroy_http_request(&h);
            return FALSE;
        }
		return TRUE;
	}

	if (CC_ISSET_BIT(CC_EVENT_READ, e->events)) {
		int32_t len = 0;
		cckit_data_buffer_t *b = cckit_data_buffer_push((cckit_data_buffer_t**)&e->args[2]);
		if (b == NULL) {
			h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTP_OUT_OF_MEMORY);
			_cckit_destroy_http_request(&h);
			return FALSE;
		}
#ifdef CC_OPENSSL_HTTPS
        if (h->address->scheme == CC_SCHEME_HTTPS) {
            len = (int32_t)SSL_read(h->ssl, (char*)(b->buff + b->length), CC_IO_BUFFER_SIZE - b->length);
        } else
#endif
        {
            len = (int32_t)recv(e->io_handle, (char*)(b->buff + b->length), CC_IO_BUFFER_SIZE - b->length, 0);
        }
        
		if (len <= 0) {
			h->cb.cb_success && h->cb.cb_success(h->cb.args);
			_cckit_destroy_http_request(&h);
			return FALSE;
		}

		/**/
		b->length += len;
		if (h->valid == FALSE) {
			if (_request_header(h, b) == FALSE) {
				h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTP_HTTP_ERROR);
				_cckit_destroy_http_request(&h);
				return FALSE;
			}
			/**/
			if (h->valid == FALSE) {
				return TRUE;
			}

			if(h->cb.cb_header && h->cb.cb_header(h, h->response) == FALSE) {
				_cckit_destroy_http_request(&h);
				return FALSE;
			}
			/**/
			if (b->length <= 0) {
				if(h->content_length <= 0) {
					h->cb.cb_success && h->cb.cb_success(h->cb.args);
					_cckit_destroy_http_request(&h);
					return FALSE;
				}
				return TRUE;
			}
		}
		/**/
		h->length += b->length;
		if(h->cb.cb_read && h->cb.cb_read(h->cb.args, b->buff, b->length) == FALSE) {
			_cckit_destroy_http_request(&h);
			return FALSE;
		}

		if (h->length >= h->content_length) {
			h->cb.cb_success && h->cb.cb_success(h->cb.args);
			_cckit_destroy_http_request(&h);
			return FALSE;
		}

		b->length = 0;
		cckit_data_buffer_pop((cckit_data_buffer_t**)&e->args[2]);
		return TRUE;
	}

	if (CC_ISSET_BIT(CC_EVENT_TIMEOUT, e->events)) {
		h->cb.cb_error && h->cb.cb_error(h->cb.args, CCKIT_HTTP_SOCKET_TIMEOUT);
		_cckit_destroy_http_request(&h);
		return FALSE;
	}

	return TRUE;
}