示例#1
0
void deployment_route(mg_connection* conn, const mg_request_info* request_info)
{
	char qid[16];
	int id;
	
	get_qsvar(request_info, "id", qid, sizeof(qid));
	
	mg_printf(conn, "%s", ajax_reply_start);	
	
	if (strcmp(qid, "undefined") == 0) 
	{
		mg_printf(conn, "{\"state\":\"error\"}");	
	} 
	else
	{
		istringstream iss(qid);
		iss >> id;
	
		PolypeerServer* server = PolypeerServer::getInstance();
		ServerData& data = server->getServerData();
		File* file = data.getFile(id);
		
		if (file == NULL)
		{
			mg_printf(conn, "{\"state\":\"error\"}");	
		}
		else
		{
			FileManager* fm = file->getFileManager();
			std::vector<std::vector<Entity*>* >* pp = file->getSortedHosts();
			
			// gestion affichage de la date de déploiement
			struct tm * timeinfo;
			time_t rawtime = (time_t)(file->getDate());
			char buffer[80];
			timeinfo = localtime(&rawtime);
			
			strftime (buffer,80,"%c",timeinfo);
			
			mg_printf(conn, "{\"id\":\"%i\", \"name\":\"%s\", \"date\":\"%s\", \"filename\":\"%s\", \"state\":\"%s\", \
				\"nbchunk\":%lu, \"chunksize\":%lu, \"size\":%lu, \"hosts\":["
				, id, file->getName().c_str(), buffer
				, fm->getFileName().c_str()
				, getStringFileState(file->getFileState()).c_str()
				, fm->getNumberChunk()
				, fm->getChunkSize(), fm->getFileSize()
			);
	
			for (vector<vector<Entity*>* >::const_iterator itZone = pp->begin(); itZone != pp->end(); ++itZone) 
			{
				for (vector<Entity*>::const_iterator it = (*itZone)->begin(); it != (*itZone)->end(); ++it)
				{
					mg_printf(conn, "{\"name\":\"%s\", \"ip\":\"%s\", \"host_state\":\"%s\", \"state\":\"%s\", \"current\":\"%i\", \"total\":\"%lu\"}"
						, (*it)->getName().c_str(), (*it)->getIP().c_str()
						, getStringHostState((*it)->getHostState()).c_str()
						, getStringHostDeployState((*it)->getDeploymentState(fm->getIdFile())->getCurrentState()).c_str()
						, (*it)->getDeploymentState(fm->getIdFile())->getCurrentIdChunk()
						, fm->getNumberChunk()
					);
			
					if ((it + 1) != (*itZone)->end()) 
					{
						mg_printf(conn, ",");
					}
				}
		
				if ((itZone + 1) != pp->end()) 
				{
					mg_printf(conn, ",");
				}
			}
	
			File::deleteSortedHost(pp); // Nécessaire, des allocations dynamiques sont faite lors du listing précédent
	
			mg_printf(conn, "]}");
		}
	}
}