예제 #1
0
/*
 * 1 - Initialize company.
 * 2 - Update map.
 * 3 - Wake up planes.
 * 4 - Update planes.
 * 5 - Send map updates.
 */
void companyStart(Map* initialMap, Company* cmp) {
    company = cmp;
    sprintf(cmpSemName, "c%d", company->id);
    map = initialMap;
    initializeCompany();
    updateMap();
    S_POST("companyReady");// Tell the server that this company has been created.
    createInactivePlanesBitMask();
    do {
        S_WAIT(cmpSemName);
        log_debug("[Company %d] Started turn---------------->", company->id);
        updateMap();
        wakeUpPlanes();
        waitUntilPlanesReady();
        updateDestinations();
        updateServer();
        log_debug("[Company %d] Finished turn---------------->", company->id);
        S_POST("server");
    } while (HAS_ACTIVE_PLANES);
    CompanyUpdatePackage update;
    update.companyId = company->id;
    update.status = FALSE;
    log_warning("Company %d died", company->id);
    serializer_write(&update, company->id + 1, serverId, PACKAGE_TYPE_COMPANY_UPDATE);
    company_free(company, TRUE);
}
예제 #2
0
int openAndReadCompanyFile(char * path, company ** cmp, int index, graph * map) {

	FILE * file = fopen(path, "r");
	char * num = NULL;
	int airplaneQty = 0;
	int counter = 0;
	cmp[index] = NULL;
	char * str = NULL;

	if (file == NULL)
		return parserError(NULL, cmp[index], FILE_ERROR, file);

	num = readString(file, '\n');
	if (!isdigit(*num)) {
		free(num);
		return parserError(NULL, cmp[index], FILE_ERROR, file);
	}
	airplaneQty = atoi(num);
	cmp[index] = malloc(sizeof(company));

	if (cmp[index] == NULL)
		return parserError(NULL, cmp[index], MALLOC_ERROR, file);

	if (initializeCompany(cmp[index], airplaneQty) != EXIT_SUCCESS)
		return parserError(NULL, cmp[index], MALLOC_ERROR, file);

	if (readBlankLine(file) != EXIT_SUCCESS)
		return parserError(NULL, cmp[index], FILE_ERROR, file);

	for (counter = 0; counter < airplaneQty; counter++) {

		medList * list = NULL;

		int read = 0;

		str = readString(file, '\n');

		if (str == NULL)
			return parserError(NULL, cmp[index], FILE_ERROR, file);
		if (existsCity(str, map) == -1)
			return parserError(NULL, cmp[index], FILE_ERROR, file);

		if (addActualCity(cmp[index], str, counter) != EXIT_SUCCESS)
			return parserError(NULL, cmp[index], FILE_ERROR, file);

		list = createList();

		while ((read = fgetc(file)) != '\n' && read != EOF) {
			if (read != '\r') {
				char * medName = NULL;
				char * amount = NULL;

				fseek(file, -1, SEEK_CUR);

				medName = readString(file, ' ');
				amount = readString(file, '\n');

				if (medName == NULL || amount == NULL || !isdigit(*amount)) {
					if (amount != NULL)
						free(amount);
					if (medName != NULL)
						free(medName);
					if (list != NULL)
						freeMedList(list);
					return parserError(NULL, cmp[index], FILE_ERROR, file);
				}

				addMed(medName, atoi(amount), list);
				free(amount);
			}
		}
		addMedListToCompany(cmp[index], list, counter);
	}

	fclose(file);
	free(num);
	return EXIT_SUCCESS;
}