⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ls.cpp

📁 GiPS是一个面向数据密集型应用的分布式文件系统
💻 CPP
字号:
// ls.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "FileSystem.h"
#include <time.h>
#include <io.h>
#include <IceUtil/IceUtil.h>

//读
void readFile(string sourName, string desName){
	//计时
	clock_t start = clock();

	unsigned char* buffer = new unsigned char[MAX_BUFFER];
	Handle* handle = ::open(sourName,0);

	ofstream out(desName.c_str(),ios::out|ios::binary);
	int len = handle->fileObj.size();
	
	for(int i=0;i<len;i++){				
		int bufferSize = ::read(handle,buffer,MAX_BUFFER);
		if(bufferSize == 0){
			cerr<<"read file fails..."<<endl;
			return;
		}
		out.write((char*)buffer,bufferSize);
		cout<<"读取第"<<i+1<<"块"<<endl;
	}
	out.close();	
	cout<<"读取完毕"<<endl;

	::close(handle);
	delete []buffer;

	clock_t finish = clock();
	double duration = ((double)(finish - start))/CLOCKS_PER_SEC;
	cout<<"File read cost: "<<duration<<" seconds"<<endl;
}

//写
void writeFile(string sourName, string desName){
	//计时
	clock_t start = clock();

	if((_access(sourName.c_str(), 0)) == -1 ){
		throw "sourcefile does not exit!";
	}

	//打开文件
	ifstream in(sourName.c_str(),ios::in|ios::binary);
	in.seekg(0,ios::end);
	int fileSize = in.tellg();

	unsigned char* buffer = new unsigned char[MAX_BUFFER];
	Handle* handle = ::open(desName,1);

	bool flag = false;
	int ipN = handle->desIPs.size();
	int timeN = 0;
	while(timeN < ipN){
		//先将数据指针移到起始位置,可能是第一次读取,也可能是某次读取失败
		flag = false;
		in.seekg(0,ios::beg);
		handle->fileObj.clear();
		handle->curPos = 0;

		string ip = handle->desIPs[timeN];		
		timeN++;

		string config = "FileIOManager:default -h " + ip + " -p 12807";
		try{
			handle->curIOManager = ::ClientClerk::FileIOManagerPrx::checkedCast(Ice::Application::communicator()->stringToProxy(config));
		}
		catch(const Ice::Exception &ex){
			continue;
		}

		int tempPos = 0;
		double rate = 0.0;
		while(true){
			::ClientMaster::FileBlock block;

			int left = fileSize - tempPos;
			if(left <= 0){
				//写入成功,设置写入成功变量,并保证退出最外层循环
				flag = true;
				timeN = ipN;
				break;
			}

			//生成当前块的索引
			handle->curBlockIndex = IceUtil::generateUUID();
			if(left > MAX_BUFFER){
				in.read((char*)buffer,MAX_BUFFER);
				if(::write(handle,buffer,MAX_BUFFER) == MAX_BUFFER){
					block.blockID = handle->curPos/MAX_BUFFER + 1;
					block.indexID = handle->curBlockIndex;
					block.IPs.push_back(ip);
					handle->fileObj.push_back(block);
					handle->curPos += MAX_BUFFER; 
				}
				else{
					//写入失败,全部重新来过
					break;
				}
			}
			else{
				in.read((char*)buffer,left);
				if(::write(handle,buffer,left) == left){
					block.blockID = handle->curPos/MAX_BUFFER + 1;
					block.indexID = handle->curBlockIndex;
					block.IPs.push_back(ip);
					handle->fileObj.push_back(block);
					handle->curPos += left;
				}
				else{
					//写入失败,全部重新来过
					break;
				}
			}

			if(left > MAX_BUFFER)
				tempPos += MAX_BUFFER;
			else
				tempPos += left;			
				
			rate = (tempPos*1.0/fileSize)*100;
			cout<<sourName.c_str()<<" 传输了 "<<rate<<" % "<<endl;
		}
	}

	in.close();
	if(flag)
		cout<<sourName.c_str()<<" 传输完毕!"<<endl;
	else
		cout<<sourName.c_str()<<" 传输失败!"<<endl;	

	::updateMasterFS(desName,handle->fileObj);
	::close(handle);
	delete []buffer;

	clock_t finish = clock();
	double duration = ((double)(finish - start))/CLOCKS_PER_SEC;
	cout<<"File write cost: "<<duration<<" seconds"<<endl;
}


class App : virtual public Ice::Application {
public:
	virtual int run(int argc, char* argv[]) {
		int status = 0;
		try {
			while(true){
				cout<<"$:";
				string str;
				getline(cin, str);

				if(str.size() <= 0)
					continue;

				int pos = str.find(" ",0);
				if(pos > 0){
					string order = str.substr(0,pos);
					str.erase(0,pos+1);

					if(order == "mkdir"){
						int size = str.size();
						if(size <= 0 || str[0] != '/'){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}
						::mkdir(str);
					}
					else if(order == "rmdir"){
						int size = str.size();
						if(size <= 0 || str[0] != '/'){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}
						::rmdir(str);
					}
					else if(order == "read"){
						pos = str.find(" ",0);
						if(pos < 0){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}

						string sourFileName = str.substr(0,pos);
						if(sourFileName[0] != '/'){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}
						str.erase(0,pos+1);

						int size = str.size();
						if(size <= 0){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}

						::readFile(sourFileName,str);
					}
					else if(order == "write"){
						pos = str.find(" ",0);
						if(pos < 0){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}

						string sourFileName = str.substr(0,pos);
						str.erase(0,pos+1);
						
						int size = str.size();
						if(size <= 0 || str[0] != '/'){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}

						::writeFile(sourFileName,str);
					}
					else if(order == "rmfile"){
						int size = str.size();
						if(size <= 0 || str[0] != '/'){
							cerr<<"wrong usage! type help for more information..."<<endl;
							continue;
						}

						::rmfile(str);
					}
					else{
						cout<<"wrong usage! type help for more information..."<<endl;
					}
				}
				else{
					if(str == "help"){
						cout<<"all orders: "<<endl;
						cout<<"  ls"<<endl;
						cout<<"  mkdir  folderName"<<endl;
						cout<<"  rmdir  folderName"<<endl;
						cout<<"  write  sourceFileName desFileName"<<endl;
						cout<<"  read   sourceFileName desFileName"<<endl;
						cout<<"  rmfile fileName"<<endl;
					}
					else if(str == "ls"){
						::ls();
					}
					else if(str == "exit"){
						break;
					}
					else{
						cout<<"wrong usage! type help for more information..."<<endl;
					}
				}
				
			}
		} catch (const Ice::Exception &ex) {
			cerr << ex << endl;
			status = 1;
		} catch (const char * msg) {
			cerr << msg << endl;
			status = 1;
		}
		return status;
	}
};

int main(int argc, char * argv[]){
	App client;
	return client.main(argc, argv, "config.txt");	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -