Skip to content

Commit

Permalink
method added. Bug - replication in 4 machines
Browse files Browse the repository at this point in the history
  • Loading branch information
luisremis committed Nov 7, 2015
1 parent cda7329 commit d5d71d5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
53 changes: 53 additions & 0 deletions FileSystem.cpp
Expand Up @@ -101,6 +101,56 @@ void FileSystem::put(std::string localFile, std::string remoteFile)
//std::string destAddress = "localhost";
//put(destAddress, localFile, remoteFile);
}
void FileSystem::where(std::string remoteFile)
{
int fileKey = hashString(remoteFile);
int position = findPositionByKey(fileKey);

std::cout<< "Master Replica: " << virtualRing[position].ip_str << std::endl;
std::cout<< "Second Replica: " << virtualRing[ (position+1) % virtualRing.size()].ip_str << std::endl;
std::cout<< "Second Replica: " << virtualRing[ (position+2) % virtualRing.size()].ip_str << std::endl;
}

void FileSystem::checkFiles()
{
// hashing function to find the machine/s where to store the file;

std::vector<std::string> toremove;

filesLock.lock();

for ( auto &file : files)
{
int flag = false;
for (int i = 0; i < 3; ++i)
{
int position = findPositionByKey( hashString(file.filename) ) + i;
int nodePosition = ( position + virtualRing.size() ) % virtualRing.size();

if (virtualRing[nodePosition].key == myself.key)
{
flag = true;
}
}

if (!flag)
{
toremove.push_back(file.filename);
}
}

for (int i = 0; i < toremove.size(); ++i)
{
if ( deleteFile( toremove.at(i) ) )
{
logFile << toremove.at(i) << "deleted" << std::endl;
}
files.remove(toremove.at(i));
}

filesLock.unlock();

}

void FileSystem::get(std::string localFile, std::string remoteFile){
//https://gitlab-beta.engr.illinois.edu/remis2/MP3-FileSystem.git
Expand Down Expand Up @@ -320,6 +370,8 @@ std::string FileSystem::getFileList()
{
std::stringstream ss;

//checkFiles();

filesLock.lock();
for (std::list<FileEntry>::iterator it=files.begin(); it != files.end(); ++it)
{
Expand Down Expand Up @@ -777,6 +829,7 @@ void FileSystem::updateThread(){
myself.key = hashString( myself.ip_str );

while(true){

MemberUpdateMsg msg = membership.pullMsgFromFileSysQueue();

if(msg.type == MSG_JOIN){
Expand Down
3 changes: 2 additions & 1 deletion FileSystem.h
Expand Up @@ -127,7 +127,7 @@ class FileSystem

void updateThread();


void checkFiles();


public:
Expand Down Expand Up @@ -158,6 +158,7 @@ class FileSystem
void printVirtualRing();

void deleteFromFS(std::string remoteFile);
void where(std::string remoteFile);

};

Expand Down
10 changes: 8 additions & 2 deletions main.cpp
Expand Up @@ -47,7 +47,7 @@ void listeningCin(Membership* m, FileSystem* fs)
std::cout << "Exiting normally " << std::endl;
break;
}
else if (input.compare("clear") == 0)
else if (input.compare("clear") == 0 || input.compare("c") == 0)
{
system("clear");
}
Expand Down Expand Up @@ -100,7 +100,13 @@ void listeningCin(Membership* m, FileSystem* fs)
std::cin >> remotefile;
fs->deleteFromFS(remotefile);
}
else if (input.compare("stored") == 0)
else if (input.compare("list") == 0)
{
std::string remotefile;
std::cin >> remotefile;
fs->where(remotefile);
}
else if (input.compare("stored") == 0 || input.compare("s") == 0)
{
std::cout << "Local Files List: " << std::endl;
std::cout << fs->getFileList() << std::endl;
Expand Down

0 comments on commit d5d71d5

Please sign in to comment.