📄 filesystem.cpp
字号:
#include "FileSystem.h"
#include <IceUtil/IceUtil.h>
#include <time.h>
static bool getStorageInformation(string fileName,::ClientMaster::FileObject& fileObj);
static bool setStorageInformation(string fileName,const ::ClientMaster::FileObject& fileObj);
static ::ClientMaster::desIPList getClusterNodes();
bool ls(){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
try {
ClientMaster::list list = pFileObjectManager->getFSList();
int size = list.size();
for(int i=0;i<size;i++){
cout<<list[i]<<endl;
}
return true;
}
catch (const Ice::Exception &ex) {
cerr << ex << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
return false;
}
bool mkdir(string folderName){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
try {
pFileObjectManager->createFolder(folderName);
return true;
}
catch (const Ice::Exception &ex) {
cerr << ex << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
return false;
}
bool rmdir(string folderName){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
try {
pFileObjectManager->deleteFolder(folderName);
return true;
}
catch (const Ice::Exception &ex) {
cerr << ex << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
return false;
}
bool rmfile(string fileName){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
try {
pFileObjectManager->deleteFile(fileName);
return true;
}
catch (const Ice::Exception &ex) {
cerr << ex << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
return false;
}
Handle* open(string fileName, int mode){
Handle* handle = new Handle;
switch(mode){
//open for read
case 0:
::getStorageInformation(fileName,handle->fileObj);
handle->curIOManager = NULL;
handle->curBlockIndex = "";
handle->curPos = 0;
break;
//open for write
case 1:
handle->desIPs = ::getClusterNodes();
handle->curIOManager = NULL;
handle->curBlockIndex = "";
handle->curPos = 0;
break;
default:
break;
}
return handle;
}
void seek(Handle* handle,int offset,int tag){
handle->curPos = offset;
}
int write(Handle* handle,unsigned char* data,int size){
::ClientClerk::BinaryData wData(data,data+size);
if(!handle->curIOManager || handle->curBlockIndex.length() < 5 ){
return -1;
}
try{
if(handle->curIOManager->writeFile(handle->curBlockIndex,wData)){
return size;
}
}
catch(const Ice::Exception &ex){
cerr << ex << endl;
}
return -1;
}
int read(Handle* handle,unsigned char* data,int size){
int blockID = handle->curPos/MAX_BUFFER + 1;
::ClientMaster::FileBlock fileBlock = handle->fileObj[blockID-1];
int ipN = fileBlock.IPs.size();
int timeN = 0;
while(timeN < ipN){
string ip = fileBlock.IPs[timeN];
string config = "FileIOManager:default -h " + ip + " -p 12807";
::ClientClerk::FileIOManagerPrx pFileIOManager =
::ClientClerk::FileIOManagerPrx::checkedCast(Ice::Application::communicator()->stringToProxy(config));
if(!pFileIOManager){
timeN++;
if(timeN == ipN)
throw "Read error: all dedicated node fail...";
continue;
}
::ClientClerk::BinaryData rData;
if(pFileIOManager->readFile(fileBlock.indexID,rData)){
int len = rData.size();
unsigned char* p = data;
for(int i=0;i<len;i++){
*p = rData[i];
p++;
}
handle->curPos += len;
return len;
}
timeN++;
if(timeN == ipN)
throw "Read error: all dedicated node fail...";
}
return 0;
}
bool close(Handle* handle){
handle->desIPs.clear();
handle->fileObj.clear();
delete handle;
handle = NULL;
return true;
}
void updateMasterFS(const string fileName,const ::ClientMaster::FileObject& fileObj){
::setStorageInformation(fileName,fileObj);
}
bool getStorageInformation(string fileName,::ClientMaster::FileObject& fileObj){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
pFileObjectManager->getFileObjectStorageInformation(fileName,fileObj);
return true;
}
bool setStorageInformation(string fileName,const ::ClientMaster::FileObject& fileObj){
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
if(!pFileObjectManager){
cerr <<"invalid proxy: FileObjectsManager"<< endl;
return false;
}
pFileObjectManager->setFileObjectStorageInformation(fileName,fileObj);
return true;
}
::ClientMaster::desIPList getClusterNodes(){
try{
::ClientMaster::FileObjectsManagerPrx pFileObjectManager =
::ClientMaster::FileObjectsManagerPrx::checkedCast(Ice::Application::communicator()->propertyToProxy("FileObjectsManager.Proxy"));
return pFileObjectManager->getStorageNodes();
}
catch(const Ice::Exception &ex){
cerr << ex << endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -