void FileSystemRoute::handleErrorResponse(ServerEventArgs& evt) { // See if we have an html file with that error code. ofFile errorFile(_settings.getDocumentRoot() + "/" + ofToString(evt.getResponse().getStatus()) + ".html"); if (errorFile.exists()) { try { evt.getResponse().sendFile(errorFile.getAbsolutePath(), "text/html"); return; } catch (const Poco::FileNotFoundException& exc) { ofLogVerbose("FileSystemRoute::sendErrorResponse") << "Poco::FileNotFoundException: " << exc.code() << " " << exc.displayText(); } catch (const Poco::OpenFileException& exc) { ofLogVerbose("FileSystemRoute::sendErrorResponse") << "Poco::OpenFileException: " << exc.code() << " " << exc.displayText(); } catch (const Poco::Exception& exc) { ofLogVerbose("FileSystemRoute::sendErrorResponse") << "Exception: " << exc.code() << " " << exc.displayText(); } catch (const std::exception& exc) { ofLogVerbose("FileSystemRoute::sendErrorResponse") << "exception: " << exc.what(); } catch ( ... ) { ofLogVerbose("FileSystemRoute::sendErrorResponse") << "... Unknown exception."; } } // Will pass to BaseRoute_<>::sendResponse() if not already sent. }
int main(int argc, char **argv) { char *fich; /*Nombre del fichero con lista de centros*/ int salida = 0; /*Consumo en litros por minuto de gasolina*/ int espera = MAX_INT; /*Tiempo de espera minimo*/ int tanque; /*Estado del tanque al transcurrir el tiempo minimo de espera*/ pthread_t mensajero; /*Hilo que hace peticiones de gasolina*/ char flog[50] = "log_"; /*Nombre del archivo de log*/ /*Inicializa semaforos en 1*/ sem_init(&sem, 0, 1); sem_init(&semf, 0, 1); /*Si hubo error en la invocacion del porgrama*/ if (llamadaB(argc, argv, &nombre, &fich, &max, &gas, &salida) < 0) { return -1; } /*Arma el nombre del archivo de log*/ strcat(flog, nombre); strcat(flog, ".txt"); /*Abre el archivo de log*/ if ((out = fopen(flog, "w")) == NULL) { return errorFile(__LINE__); } fprintf(out, "Estado inicial: %d\n", gas); /*Revisa el fichero de centros, si hay error termina el programa.*/ if ((espera = analizar_fichero(fich)) < 0) { return -3; } /*Si comenzo vacio el tanque*/ if (gas == 0) { fprintf(out, "Tanque vacio: %d\n", tiempo); } /*Si comenzo lleno el tanque*/ if (gas == max) { fprintf(out, "Tanque full: %d\n", tiempo); } /*Mientras no pasen las 8 horas*/ while (tiempo < LIMITE) { ++tiempo; /*Duerme por un minuto (0.1 seg)*/ usleep(MINUTO); /*Wait para accesar a 'gas' y 'pet'*/ sem_wait(&sem); /*Si no se ha acabado la gasolina*/ if (gas - salida > 0) { gas -= salida; /*Si se acabo la gasolina*/ } else if (gas > 0) { gas = 0; sem_wait(&semf); fprintf(out, "Tanque vacio: %d\n", tiempo); sem_post(&semf); } /*El estado del tanque al transcurrir el tiempo minimo de espera*/ tanque = (gas - espera * salida); /*Si estara vacio para ese momento, le colocamos '0'*/ if (tanque < 0) { tanque = 0; } /*Si hay espacio suficiente en el inventario*/ while (max - tanque >= CARGA * pet) { /*Aumenta el numero de peticiones a hacer*/ ++pet; /*Crea un hilo que maneje la peticion*/ if (pthread_create(&mensajero, NULL, pedir_gas, NULL) != 0) { errorHilo(__LINE__); } } /*Signal para liberar 'gas' y 'pet'*/ sem_post(&sem); } return 0; }
/** * Analiza el fichero pasado en la llamada del programa. * @param fich nombre del fichero. * @return Si se hizo correctamente, se retorna la espera minima, sino 0 (false) para indicar falla. */ int analizar_fichero(char *fich) { FILE *fd = NULL; /*File descriptor del fichero de centros*/ char buffer[100]; /*Buffer de lectura para el archivo.*/ distr cent; /*Variable que representa un centro*/ char *nom, *DNS; /*Nombre y DNS de centro de distribucion*/ int puerto, respuesta; /*Puerto y tiempo de respuesta de centro de distribucion*/ int min_resp = MAX_INT; /*Tiempo de respuesta minimo de los centros de distribucion*/ int i; /*Variable de uso generico*/ /*Abre el archivo*/ if ((fd = fopen(fich, "r")) == NULL) { return errorFile(__LINE__); } /*Lee el archivo hasta el final*/ while (fscanf(fd, "%s", buffer) != EOF) { nom = strtok(buffer, "&"); DNS = strtok(NULL, "&"); puerto = atoi(strtok(NULL, "&")); /*Intenta conseguir una conexion con el servidor 5 veces, si no lo logra lo ignora*/ for (i = 0; i < 5; ++i) { /*Para conseguir el tiempo de respuesta de este servidor*/ if ((respuesta = conectar_centro('t', puerto, DNS)) >= 0) { break; } } /*Si no logro conectarse con el servidor*/ if (i == 5) { continue; } /*Si consegui un nuevo minimo tiempo de respuesta*/ if (respuesta < min_resp) { min_resp = respuesta; } if((cent = create_distr(nom, DNS, puerto, respuesta)) == NULL) { return -1; } if (!add(¢ros, cent)) { free(cent); } } /*Si no logro conectarse nunca*/ if (is_empty(centros)) { return 0; } return min_resp; }
void DlgErrorReport::removeFile() const { QFile::remove (errorFile ()); }