Skip to content

Commit

Permalink
Exit channel map loop when tiles are no longer required
Browse files Browse the repository at this point in the history
  • Loading branch information
pford committed Nov 20, 2024
1 parent 6c69d95 commit 5fca92d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Session/ChannelMapSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,25 @@ bool ChannelMapSettings::HasTile(int file_id, int tile) {
return _required_tiles[file_id].HasTile(tile);
}

bool ChannelMapSettings::HasTiles(int file_id, const std::vector<int>& tiles) {
std::unique_lock<std::mutex> lock(_file_mutexes[file_id]);
if (_required_tiles.find(file_id) == _required_tiles.end()) {
return false;
}
return _required_tiles[file_id].HasTiles(tiles);
}

void ChannelMapSettings::RemoveFile(int file_id) {
if (file_id == ALL_FILES) {
_file_mutexes.clear();
_channel_ranges.clear();
_required_tiles.clear();
_file_mutexes.clear();
} else {
_file_mutexes.erase(file_id);
std::unique_lock<std::mutex> lock(_file_mutexes[file_id]);
_channel_ranges.erase(file_id);
_required_tiles.erase(file_id);
lock.unlock();
_file_mutexes.erase(file_id);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Session/ChannelMapSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ struct RequiredTiles {
bool HasTile(int tile) {
return std::find(current_tiles.begin(), current_tiles.end(), tile) != current_tiles.end();
}

bool HasTiles(const std::vector<int>& tiles) {
// Returns true if any input tiles are in current tiles
for (auto tile : tiles) {
if (HasTile(tile)) {
return true;
}
}
return false;
}
};

class ChannelMapSettings {
Expand All @@ -58,6 +68,7 @@ class ChannelMapSettings {
bool IsInChannelRange(int file_id, int channel);
bool HasRequiredTiles(int file_id, const CARTA::AddRequiredTiles& required_tiles);
bool HasTile(int file_id, int tile);
bool HasTiles(int file_id, const std::vector<int>& tiles);

// Remove a file or all files from channel maps when closed in Session.
void RemoveFile(int file_id);
Expand Down
22 changes: 21 additions & 1 deletion src/Session/Session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void Session::WaitForTaskCancellation() {
}
_animation_object->CancelExecution();
}
if (_channel_map_settings) {
_channel_map_settings->RemoveFile(ALL_FILES);
}
}

void Session::ConnectCalled() {
Expand Down Expand Up @@ -766,6 +769,7 @@ void Session::OnSetImageChannels(const CARTA::SetImageChannels& message) {
int start_channel(message.channel_range().min());
int end_channel(message.channel_range().max());
int num_channel(frame->Depth());
std::vector<int> required_tiles = {message.required_tiles().tiles().begin(), message.required_tiles().tiles().end()};
bool skipped_channel(false);

// Use animation limits for flow control
Expand All @@ -782,7 +786,13 @@ void Session::OnSetImageChannels(const CARTA::SetImageChannels& message) {
continue;
}

// Do not check received channel if start channel, or have skipped channels causing gap
if (!ChannelMapTilesValid(file_id, required_tiles)) {
spdlog::debug(
"Cancel starting at channel {} in range {}-{}, tiles not in current tiles", chan, start_channel, end_channel);
break;
}

// Check received channel gap if not start channel, or have not skipped channels causing gap
if (chan > start_channel && !skipped_channel &&
_channel_map_received_channel.find(file_id) != _channel_map_received_channel.end()) {
int received_channel = _channel_map_received_channel[file_id];
Expand All @@ -793,10 +803,16 @@ void Session::OnSetImageChannels(const CARTA::SetImageChannels& message) {
received_channel = _channel_map_received_channel[file_id];
}

// Check valid channel/tiles again
if (!IsInChannelMapRange(file_id, chan)) {
spdlog::debug("Skip channel {} in range {}-{}, not in current range", chan, start_channel, end_channel);
continue;
}
if (!ChannelMapTilesValid(file_id, required_tiles)) {
spdlog::debug(
"Cancel starting at channel {} in range {}-{}, tiles not in current tiles", chan, start_channel, end_channel);
break;
}
}

auto start_time = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -2609,6 +2625,10 @@ bool Session::IsInChannelMapTiles(int file_id, int tile) {
return _channel_map_settings && _channel_map_settings->HasTile(file_id, tile);
}

bool Session::ChannelMapTilesValid(int file_id, const std::vector<int>& tiles) {
// Check if any tiles are in current channel map tiles for file id.
return _channel_map_settings && _channel_map_settings->HasTiles(file_id, tiles);
}
void Session::HandleChannelMapFlowControlEvt(CARTA::ChannelMapFlowControl& message) {
_channel_map_received_channel[message.file_id()] = message.received_channel();
}
1 change: 1 addition & 0 deletions src/Session/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class Session {
bool IsValidChannelMapTile(int file_id, int channel, int tile);
bool IsInChannelMapRange(int file_id, int channel);
bool IsInChannelMapTiles(int file_id, int tile);
bool ChannelMapTilesValid(int file_id, const std::vector<int>& tiles);

// uWebSockets
uWS::WebSocket<false, true, PerSocketData>* _socket;
Expand Down

0 comments on commit 5fca92d

Please sign in to comment.