Skip to content

Commit

Permalink
Fixed bug where the peer exits prematurely
Browse files Browse the repository at this point in the history
  • Loading branch information
lte678 committed Jan 5, 2024
1 parent 37579d9 commit 16c1646
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
56 changes: 32 additions & 24 deletions src/FileTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,39 @@ namespace fmerge {
}


Change Change::deserialize(std::istream& stream) {
std::optional<Change> Change::deserialize(std::istream& stream) {
Change ret{};
std::stringbuf buffer{};

stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.type = static_cast<ChangeType>(std::stoi(buffer.str()));
buffer.str("");
try {
stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.type = static_cast<ChangeType>(std::stoi(buffer.str()));
buffer.str("");

stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.earliest_change_time = std::stol(buffer.str());
buffer.str("");
stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.earliest_change_time = std::stol(buffer.str());
buffer.str("");

stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.latest_change_time = std::stol(buffer.str());
buffer.str("");
stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.latest_change_time = std::stol(buffer.str());
buffer.str("");

stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.file.type = static_cast<FileType>(std::stoi(buffer.str()));
buffer.str("");
stream.get(buffer, ',');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.file.type = static_cast<FileType>(std::stoi(buffer.str()));
buffer.str("");

stream.get(buffer, '\n');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.file.path = buffer.str();
stream.get(buffer, '\n');
stream.seekg(1, std::ios_base::cur); // Seek to after the delimiter
ret.file.path = buffer.str();

return ret;
return ret;
} catch(...) {
return std::nullopt;
}
}


Expand Down Expand Up @@ -504,13 +508,17 @@ namespace fmerge {
std::vector<Change> deserialize_changes(std::istream& stream) {
std::vector<Change> changes;

Change current_change{.type = ChangeType::Unknown};
std::optional<Change> current_change{std::nullopt};
while(true) {
current_change = Change::deserialize(stream);
if(current_change.type == ChangeType::TerminateList) {
if(!current_change.has_value()) {
LOG("[Error] Could not parse change in line " << (changes.size()+1) << std::endl);
exit(1);
}
if(current_change->type == ChangeType::TerminateList) {
return changes;
}
changes.push_back(current_change);
changes.push_back(*current_change);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/FileTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace fmerge {
friend bool operator==(const Change& lhs, const Change& rhs);

void serialize(std::ostream& stream) const;
static Change deserialize(std::istream& stream);
static std::optional<Change> deserialize(std::istream& stream);
};

void update_file_tree(shared_ptr<DirNode> base_node, std::string base_path, bool show_loading_bar = true);
Expand Down
12 changes: 6 additions & 6 deletions src/StateController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,12 @@ namespace fmerge {
state = State::SyncingFiles;
state_lock.unlock();
} else if(msg->get_payload().state == State::SyncingFiles) {
state_lock.lock();
peer_finished.store(true);

if(state.load() == State::Finished) {
state_lock.lock();
state = State::Exiting;
state_lock.unlock();
}
state_lock.unlock();
} else if(msg->get_payload().state == State::AwaitingVersion) {
// The user accepted the version difference at the peer
term()->cancel_prompt();
Expand Down Expand Up @@ -324,10 +323,8 @@ namespace fmerge {
term()->update_progress_bar(static_cast<float>(processed_change_count) / static_cast<float>(total_changes));
}
});

// This is where the file sync is performed
syncer->perform_sync();
// Notify our peer that we are done
c->send_message(std::make_shared<ExitingStateMessage>(state.load()));

term()->complete_progress_bar();

Expand All @@ -341,6 +338,9 @@ namespace fmerge {
state = State::Finished;
}
state_lock.unlock();

// Notify our peer that we are done
c->send_message(std::make_shared<ExitingStateMessage>(State::SyncingFiles));
}


Expand Down

0 comments on commit 16c1646

Please sign in to comment.