コード例 #1
0
// process messages, send back a reply when a file successfully received
void FileProcessingCommunicator::processMessages(){
	try{
		while (true){
			Message msg = bq.deQ(); Header hdr = msg.getHdr();
			string addr = hdr.getAddress(), srcAddr = hdr.getSrcAddr(), srcPort = hdr.getSrcPort(), port = hdr.getPort(), cmd = hdr.getCmd(), content = hdr.getAttrib("filename");
			AbstractDispatcher& dispatcher = AbstractDispatcher::getInstance();
			Message msgReply; Header hdrReply;
			hdrReply.setAddr(srcAddr, srcPort);
			hdrReply.setSrcAddr(addr, port);
			if (content == "stop"){
				sout << "\n Stopping file processing communicator \n";
				break;
			}
			if (cmd == "get_file"){//download
				Folder folder; string destDir = folder.getUploadDir(); FileManager mgr;
				std::vector<std::string> files = mgr.searchFiles(destDir, "*.*", false);
				//Send all files in the upload directory
				for (string path : files){
					hdrReply.setTargetCommunicator("CLientEchoCommunicator");
					hdrReply.setCmd("send_file");
					hdrReply.setAttrib("filename", path);
					hdrReply.setAttrib("msg", "download_reply");
					msgReply.setHdr(hdrReply);
					dispatcher.postMessage(msgReply);
				}

			}
			else{//upload
				string src_file = hdr.getAttrib("src_file_path");
				FileInfo fileinfoDest(content); FileInfo fileinfoSrc(src_file); 				//string cmd = hdr.getAttrib("send_file_begin");		
				string printMsg = "\n Received File " + src_file + " of SIZE = " + std::to_string(fileinfoSrc.size()) + " from " + srcAddr + ":" + srcPort + " and saved at " + content + " with SIZE = " + std::to_string(fileinfoDest.size()) + " \n\n";
				sout << printMsg;
				hdrReply.setTargetCommunicator("CLientEchoCommunicator");
				string strMsg = "Response for received file " + Path::getName(src_file);
				hdrReply.setAttrib("msg", strMsg);
				hdrReply.setCmd("echo_msg");
				msgReply.setHdr(hdrReply);
				dispatcher.postMessage(msgReply);

			}
		}
	}catch (exception e){
		sout << e.what();
	}
}
コード例 #2
0
//String processing instance constructor
StringProcessor::StringProcessor(Message msg){
	try{
		request = msg;
		/*Timer timer;
		long long time_stamp = timer.getCurrentTime();
		request.getHdr().setAttrib("start_time", to_string(time_stamp));*/

		Header hdr = msg.getHdr();
		//srchDir = hdr.getAttrib("directory");
		Folder folder;
		srchDir = folder.getUploadDir();
		srchString = hdr.getAttrib("search_string");
		numThreads = atoi(hdr.getAttrib("no_of_threads").c_str());
		if (numThreads == 0){
			numThreads = 1;
		}
		FileManager mgr;
		std::vector<std::string> files = mgr.searchFiles(srchDir, "*.*", false);

		numFiles = 0;
		std::string exclude_exts[] = {  "zip", "exe", "jpg", "jpeg", "png", "JPG", "JPEG", "EXE" };//exclude extendions for string search

		for (auto file : files){
			std::string extension = Path::getExt(file);
			bool isSkip = false;
			for (auto ext : exclude_exts){
				if (ext == extension){
					isSkip = true;
					break;
				}
			}
			if (isSkip){
				continue;
			}
			SearchResult sr(file);
			bqIn.enQ(sr);
			numFiles++;
		}
	}
	catch (exception e){ sout << e.what(); }
}