示例#1
0
void producer(){
	
	int currentLine = 1;
	int currentFile = 0;
	
	while(currentFile<FILEAMOUNT)
	{
		
		char* fractalResult=getFractal(files[currentFile], currentLine);
		currentLine++;
		printf("after get fractal: %s",fractalResult);
		if(eof==true){
			currentFile++;
			eof=false;
		}
		
		if(fractalResult[0]!="#"){
			printf("just before sem wait \n");
			sem_wait(&empty); 
			pthread_mutex_lock(&mutexFractal);
			
			int index;
			for(int i =0;i<10;i++){
				if(fractal_get_height(slots[i])<0){
					index=i;
				}
			}
			char * name= strtok(fractalResult," ");
			char * width = strtok(NULL," ");
			char * height = strtok(NULL," ");
			char * a = strtok(NULL," ");
			char * b = strtok(NULL," ");
			
			slots[index]= fractal_new(atoi(width), atoi(height), atof(a), atof(b), name);
		
			pthread_mutex_unlock(&mutexFractal);
			sem_post(&full);
		}
	}
	printf("After while");
	noMoreFiles=true;
	
}
void DBManager::fillRCTXRequest(int jid, char **sz, int &len)
{
	try{
		int iTmp = 0;

		FractalMeta fract = getFractal(jid); // triggers a temp lock
		if(fract.jobID < 0){
			*sz = NULL;
			len = 0;
			return;
		}

		// layout: [jid:4][paramLen:4][params][poutLen:4][pOut][logLen:4][log][artifacts]
		// size: 20+|params| + |pOut| + |log| + |histo|

		std::string basePath = concat(DirectoryManager::getSingleton()->getRootDirectory()+"renders/", jid);

		// pOut
		std::string paramsOut = ParamsFile::readAllFile(basePath + ".info");

		// log
		std::fstream fp;
		std::string log = "";
		fp.open(std::string(basePath + ".log").c_str(), std::ios::in);
		if(fp.is_open()){
			std::string line;
			while(getline(fp,line))
			{
				log += line + "\n";
			}
		}

		// params----
		std::string params = ParamsFile::readAllFile(basePath + ".job");

		len = 16 + paramsOut.length() + params.length() + log.length();

		// artifacts
		char *artifactData = nullptr;
		int artifactLen = ArtifactDistributer::getSingleton()->getArtifactData(fract, &artifactData);
		len += artifactLen;


		*sz = new char[len];
		char *ptr = *sz;

		iTmp =htonl(jid);
		memcpy(ptr, &iTmp, 4);
		ptr += 4;

		iTmp =htonl(params.length());
		memcpy(ptr, &iTmp, 4);
		ptr += 4;
		memcpy(ptr, params.c_str(), params.length());
		ptr += params.length();

		iTmp =htonl(paramsOut.length());
		memcpy(ptr, &iTmp, 4);
		ptr += 4;
		memcpy(ptr, paramsOut.c_str(), paramsOut.length());
		ptr += paramsOut.length();

		iTmp =htonl(log.length());
		memcpy(ptr, &iTmp, 4);
		ptr += 4;
		memcpy(ptr, log.c_str(), log.length());
		ptr += log.length();

		memcpy(ptr, artifactData, artifactLen);
		ptr += artifactLen;

	}catch(std::bad_alloc &e){
		std::cerr << "Out of memory\n";
		exit(0);
	}
}
void DBManager::fillMDUDRequest(int jid, char **sz, int &len)
{
	try{
		int iTmp = 0;

		FractalMeta fract = getFractal(jid); // triggers a temp lock
		if(fract.jobID < 0){
			*sz = nullptr;
			len = 0;
			return;
		}

		// layout: [jid:4][uid:4][titleLen:4][title][authorLen:4][author][status:4][thumbDtaLen:4][thumbDta]
		// size: 24+|title| + |author| + |imgData|

		// open image
		FILE *fp = ArtifactDistributer::getSingleton()->getFractalThumbnail(fract);
		if(fp == nullptr){ // no image
			len = 24 + fract.name.length() + fract.author.length();
			*sz = new char[len];
			memset(*sz + len - 4, 0, 4); // copy last four bytes as zero length of imgDta
		}else{
			fseek(fp, 0L, SEEK_END);
			int dtaSz = ftell(fp);
			fseek(fp, 0L, SEEK_SET);
			len = 24 + fract.name.length() + fract.author.length() + dtaSz;
			*sz = new char[len];

			iTmp = htonl(dtaSz);
			memcpy(*sz + len - 4 - dtaSz, &iTmp, 4); // copy last four bytes as zero length of imgDta
			char *ptr = *sz + len - dtaSz;
			for(int i=0; i<dtaSz; i++){
				char byte;
				fread(&byte, 1, 1, fp);
				*ptr = byte;
				ptr ++;
			}
			fclose(fp);
		}

		// now copy rest of data
		char *ptr = *sz;

		iTmp =htonl(fract.jobID);
		memcpy(ptr, &iTmp, 4);
		ptr += 4;

		iTmp =htonl(fract.userID);
		memcpy(ptr, &iTmp, 4);
		ptr += 4;

		iTmp =htonl(fract.name.length());
		memcpy(ptr, &iTmp, 4);
		ptr += 4;
		memcpy(ptr, fract.name.c_str(), fract.name.length());
		ptr += fract.name.length();

		iTmp = htonl(fract.author.length());
		memcpy(ptr, &iTmp, 4);
		ptr += 4;
		memcpy(ptr, fract.author.c_str(), fract.author.length());
		ptr += fract.author.length();

		iTmp = htonl(fract.status);
		memcpy(ptr, &iTmp, 4);

	}catch(std::bad_alloc &e){
		std::cerr << "Out of memory\n";
		exit(0);
	}
}