📄 clerkcallbacki.cpp
字号:
#include "ClerkCallBackI.h"
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace Cluster;
ClerkCallBackI::ClerkCallBackI(map<string,string>* paths)
{
this->m_paths = paths;
}
void
ClerkCallBackI::reportState(Cluster::HeartBeatMessage& hbMsg, const ::Ice::Current&)
{
Ice::PropertiesPtr props = Ice::Application::communicator()->getProperties();
hbMsg.blockNum = this->m_paths->size();
hbMsg.ip = props->getProperty("Clerk.IP");
cout << hbMsg.ip << " still alive" << endl;
}
bool ClerkCallBackI::replicaCopy(const ::Cluster::ReplicaInfo& replicaInfo, const ::Ice::Current&)
{
//计时
clock_t start = clock();
try{
string ip = replicaInfo.clerkIP;
string blockID = replicaInfo.replicaID;
string fileName = this->m_paths[0][blockID];
if((_access(fileName.c_str(), 0)) == -1 )
throw "do replica fail: can not find given block";
ifstream in(fileName.c_str(),ios::in|ios::binary);
in.seekg(0,ios::end);
int fileSize = in.tellg();
in.seekg(0,ios::beg);
unsigned char* buffer = new unsigned char[fileSize];
in.read((char*)buffer,fileSize);
in.close();
::ClientClerk::BinaryData wData(buffer,buffer + fileSize);
delete []buffer;
string config = "FileIOManager:default -h " + ip + " -p 12807";
::ClientClerk::FileIOManagerPrx pFileIOManager =
::ClientClerk::FileIOManagerPrx::checkedCast(Ice::Application::communicator()->stringToProxy(config));
if(!pFileIOManager)
throw "do replica fails: : can not connect the dedicated node ...";
if(pFileIOManager->writeFile(blockID,wData)){
puts("do replica successfully...");
clock_t finish = clock();
double duration = ((double)(finish - start))/CLOCKS_PER_SEC;
cout<<"do replica cost: "<<duration<<" seconds"<<endl;
return true;
}
}
catch (const Ice::Exception &ex){
cerr << ex << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
return false;
}
bool ClerkCallBackI::replicaDelete(const std::string& replicaUID, const Ice::Current &)
{
string fileName = this->m_paths[0][replicaUID];
try{
if((_access(fileName.c_str(), 0)) == -1 )
throw "delete replica fail: can not find given block";
if(::remove(fileName.c_str()) == 0){
puts("delete replica successful");
return true;
}
else
throw "delete replica fail: unknown reason...";
}
catch(const char * msg){
cerr << msg << endl;
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -