Skip to content

Commit

Permalink
Add & integrate ProcessListWidget (#4499)
Browse files Browse the repository at this point in the history
This integrates ProcessListWidget into SessionSetupDialog, replacing/
moving functionality away from SessionSetupDialog. Then ProcessManager
is moved from TargetConfiguration.h into Connections.h, which results in
changes to orbitmainwindow, ConnectToLocalWidget and
ConnectToTargetDialog. One of the changes to ConnectToLocalWidget is,
that now a signal is emitted when a new process list is available.
  • Loading branch information
antonrohr authored Nov 29, 2022
1 parent fbfa63d commit 0578f48
Show file tree
Hide file tree
Showing 19 changed files with 720 additions and 425 deletions.
40 changes: 22 additions & 18 deletions src/OrbitQt/orbitmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ void OrbitMainWindow::OnSshConnectionError(std::error_code error) {
ORBIT_CHECK(std::holds_alternative<SshTarget>(target_configuration_));
const SshTarget& target = std::get<SshTarget>(target_configuration_);

target.GetProcessManager()->SetProcessListUpdateListener(nullptr);
target.GetConnection()->GetProcessManager()->SetProcessListUpdateListener(nullptr);

QString error_message =
QString("The connection to machine \"%1\" failed with error message: %2")
Expand All @@ -1693,7 +1693,7 @@ void OrbitMainWindow::OnLocalConnectionError(const QString& error_message) {
ORBIT_CHECK(std::holds_alternative<LocalTarget>(target_configuration_));
const LocalTarget& target = std::get<LocalTarget>(target_configuration_);

target.GetProcessManager()->SetProcessListUpdateListener(nullptr);
target.GetConnection()->GetProcessManager()->SetProcessListUpdateListener(nullptr);

OnConnectionError(error_message);
}
Expand All @@ -1706,18 +1706,19 @@ void OrbitMainWindow::SetTarget(const SshTarget& target) {
&OrbitMainWindow::OnSshConnectionError, Qt::UniqueConnection);

app_->SetGrpcChannel(connection->GetGrpcChannel());
app_->SetProcessManager(target.GetProcessManager());
app_->SetProcessManager(target.GetConnection()->GetProcessManager());
app_->SetTargetProcess(target.GetProcess());

target_label_->ChangeToSshTarget(target);

using ProcessInfo = orbit_grpc_protos::ProcessInfo;
target.GetProcessManager()->SetProcessListUpdateListener([&](std::vector<ProcessInfo> processes) {
// This lambda is called from a background-thread, so we use QMetaObject::invokeMethod
// to execute our logic on the main thread.
QMetaObject::invokeMethod(
this, [&, processes = std::move(processes)]() { OnProcessListUpdated(processes); });
});
target.GetConnection()->GetProcessManager()->SetProcessListUpdateListener(
[&](std::vector<ProcessInfo> processes) {
// This lambda is called from a background-thread, so we use QMetaObject::invokeMethod
// to execute our logic on the main thread.
QMetaObject::invokeMethod(
this, [&, processes = std::move(processes)]() { OnProcessListUpdated(processes); });
});

is_connected_ = true;
}
Expand All @@ -1730,18 +1731,19 @@ void OrbitMainWindow::SetTarget(const LocalTarget& target) {
&OrbitMainWindow::OnLocalConnectionError, Qt::UniqueConnection);

app_->SetGrpcChannel(connection->GetGrpcChannel());
app_->SetProcessManager(target.GetProcessManager());
app_->SetProcessManager(target.GetConnection()->GetProcessManager());
app_->SetTargetProcess(target.GetProcess());

target_label_->ChangeToLocalTarget(target);

using ProcessInfo = orbit_grpc_protos::ProcessInfo;
target.GetProcessManager()->SetProcessListUpdateListener([&](std::vector<ProcessInfo> processes) {
// This lambda is called from a background-thread, so we use QMetaObject::invokeMethod
// to execute our logic on the main thread.
QMetaObject::invokeMethod(
this, [&, processes = std::move(processes)]() { OnProcessListUpdated(processes); });
});
target.GetConnection()->GetProcessManager()->SetProcessListUpdateListener(
[&](std::vector<ProcessInfo> processes) {
// This lambda is called from a background-thread, so we use QMetaObject::invokeMethod
// to execute our logic on the main thread.
QMetaObject::invokeMethod(
this, [&, processes = std::move(processes)]() { OnProcessListUpdated(processes); });
});

is_connected_ = true;
}
Expand Down Expand Up @@ -1784,11 +1786,13 @@ void OrbitMainWindow::OnProcessListUpdated(
TargetConfiguration OrbitMainWindow::ClearTargetConfiguration() {
if (std::holds_alternative<SshTarget>(target_configuration_)) {
std::get<SshTarget>(target_configuration_)
.GetProcessManager()
.GetConnection()
->GetProcessManager()
->SetProcessListUpdateListener(nullptr);
} else if (std::holds_alternative<LocalTarget>(target_configuration_)) {
std::get<LocalTarget>(target_configuration_)
.GetProcessManager()
.GetConnection()
->GetProcessManager()
->SetProcessListUpdateListener(nullptr);
}
return std::move(target_configuration_);
Expand Down
4 changes: 4 additions & 0 deletions src/SessionSetup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_sources(
include/SessionSetup/OtherUserDialog.h
include/SessionSetup/ProcessItemModel.h
include/SessionSetup/ProcessLauncherWidget.h
include/SessionSetup/ProcessListWidget.h
include/SessionSetup/ServiceDeployManager.h
include/SessionSetup/SessionSetupDialog.h
include/SessionSetup/SessionSetupUtils.h
Expand All @@ -47,6 +48,8 @@ target_sources(
ProcessItemModel.cpp
ProcessLauncherWidget.cpp
ProcessLauncherWidget.ui
ProcessListWidget.cpp
ProcessListWidget.ui
ServiceDeployManager.cpp
SessionSetupDialog.cpp
SessionSetupDialog.ui
Expand Down Expand Up @@ -85,6 +88,7 @@ target_sources(
OrbitServiceInstanceTest.cpp
OtherUserDialogTest.cpp
ProcessItemModelTest.cpp
ProcessListWidgetTest.cpp
SessionSetupDialogTest.cpp
SessionSetupUtilsTest.cpp
TargetLabelTest.cpp)
Expand Down
72 changes: 53 additions & 19 deletions src/SessionSetup/ConnectToLocalWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,36 @@
#include <QMessageBox>
#include <QPushButton>
#include <QRadioButton>
#include <chrono>
#include <cstdint>
#include <memory>

#include "ClientFlags/ClientFlags.h"
#include "ClientServices/ProcessManager.h"
#include "GrpcProtos/process.pb.h"
#include "OrbitBase/Logging.h"
#include "OrbitBase/Result.h"
#include "SessionSetup/Connections.h"
#include "ui_ConnectToLocalWidget.h"

namespace orbit_session_setup {

namespace {
[[nodiscard]] std::shared_ptr<grpc::Channel> CreateLocalhostGrpcChannel() {
const std::string target{absl::StrFormat("127.0.0.1:%d", absl::GetFlag(FLAGS_grpc_port))};
return grpc::CreateCustomChannel(target, grpc::InsecureChannelCredentials(),
grpc::ChannelArguments());
}
} // namespace

// The destructor needs to be defined here because it needs to see the type
// `Ui::ConnectToLocalWidget`. The header file only contains a forward declaration.
ConnectToLocalWidget::~ConnectToLocalWidget() = default;

ConnectToLocalWidget::ConnectToLocalWidget(QWidget* parent)
: QWidget(parent),
ui_(std::make_unique<Ui::ConnectToLocalWidget>()),
local_connection_(
grpc::CreateCustomChannel(absl::StrFormat("127.0.0.1:%d", absl::GetFlag(FLAGS_grpc_port)),
grpc::InsecureChannelCredentials(), grpc::ChannelArguments()),
nullptr),
local_connection_(CreateLocalhostGrpcChannel(), nullptr),
check_connection_timer_(this) {
ui_->setupUi(this);

Expand All @@ -46,20 +54,29 @@ ConnectToLocalWidget::ConnectToLocalWidget(QWidget* parent)
QObject::connect(ui_->startOrbitServiceButton, &QPushButton::clicked, this,
&ConnectToLocalWidget::OnStartOrbitServiceButtonClicked);

QObject::connect(&check_connection_timer_, &QTimer::timeout, this, [this]() {
if (local_connection_.GetGrpcChannel()->GetState(true) == GRPC_CHANNEL_READY) {
ui_->statusLabel->setText("Connected to OrbitService");
emit Connected();
} else {
if (local_connection_.GetOrbitServiceInstance() == nullptr) {
ui_->statusLabel->setText("Waiting for OrbitService");
} else {
ui_->statusLabel->setText("Connecting to OrbitService ...");
}
emit Disconnected();
}
});
check_connection_timer_.start(250);
QObject::connect(&check_connection_timer_, &QTimer::timeout, this,
&ConnectToLocalWidget::CheckAndSignalConnection);
CheckAndSignalConnection();
check_connection_timer_.start(std::chrono::milliseconds{250});

SetupProcessListUpdater();

qRegisterMetaType<QVector<orbit_grpc_protos::ProcessInfo>>(
"QVector<orbit_grpc_protos::ProcessInfo>");
}

void ConnectToLocalWidget::CheckAndSignalConnection() {
if (local_connection_.GetGrpcChannel()->GetState(true) == GRPC_CHANNEL_READY) {
ui_->statusLabel->setText("Connected to OrbitService");
emit Connected();
return;
}
if (local_connection_.GetOrbitServiceInstance() == nullptr) {
ui_->statusLabel->setText("Waiting for OrbitService");
} else {
ui_->statusLabel->setText("Connecting to OrbitService ...");
}
emit Disconnected();
}

void ConnectToLocalWidget::SetOrbitServiceInstanceCreateFunction(
Expand All @@ -81,7 +98,9 @@ void ConnectToLocalWidget::OnStartOrbitServiceButtonClicked() {
return;
}

local_connection_.orbit_service_instance_ = std::move(orbit_service_instance_or_error.value());
local_connection_ = LocalConnection(CreateLocalhostGrpcChannel(),
std::move(orbit_service_instance_or_error.value()));
SetupProcessListUpdater();

QObject::connect(local_connection_.GetOrbitServiceInstance(),
&OrbitServiceInstance::ErrorOccurred, this, [this](const QString& message) {
Expand All @@ -91,6 +110,21 @@ void ConnectToLocalWidget::OnStartOrbitServiceButtonClicked() {
});
}

void ConnectToLocalWidget::SetConnection(LocalConnection&& connection) {
local_connection_ = std::move(connection);
SetupProcessListUpdater();
}

void ConnectToLocalWidget::SetupProcessListUpdater() {
local_connection_.GetProcessManager()->SetProcessListUpdateListener(
[self = QPointer<ConnectToLocalWidget>(this)](
std::vector<orbit_grpc_protos::ProcessInfo> process_list) {
if (self == nullptr) return;
emit self->ProcessListUpdated(
QVector<orbit_grpc_protos::ProcessInfo>(process_list.begin(), process_list.end()));
});
}

QRadioButton* ConnectToLocalWidget::GetRadioButton() { return ui_->radioButton; }

} // namespace orbit_session_setup
11 changes: 4 additions & 7 deletions src/SessionSetup/ConnectToTargetDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ void ConnectToTargetDialog::OnProcessListUpdate(
TryToFindProcessData(process_list, target_.process_name_or_path.toStdString());

if (matching_process != nullptr) {
process_manager_->SetProcessListUpdateListener(nullptr);
target_configuration_ =
orbit_session_setup::SshTarget(std::move(ssh_connection_.value()),
std::move(process_manager_), std::move(matching_process));
ssh_connection_->GetProcessManager()->SetProcessListUpdateListener(nullptr);
target_configuration_ = orbit_session_setup::SshTarget(std::move(ssh_connection_.value()),
std::move(matching_process));
accept();
}
}
Expand All @@ -112,9 +111,7 @@ ErrorMessageOr<void> ConnectToTargetDialog::DeployOrbitServiceAndSetupProcessMan
std::move(service_deploy_manager),
std::move(grpc_channel));

process_manager_ = orbit_client_services::ProcessManager::Create(
ssh_connection_.value().GetGrpcChannel(), absl::Milliseconds(1000));
process_manager_->SetProcessListUpdateListener(
ssh_connection_->GetProcessManager()->SetProcessListUpdateListener(
[dialog = QPointer<ConnectToTargetDialog>(this)](
std::vector<orbit_grpc_protos::ProcessInfo> process_list) {
if (dialog == nullptr) return;
Expand Down
2 changes: 1 addition & 1 deletion src/SessionSetup/ProcessItemModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ int ProcessItemModel::rowCount(const QModelIndex& parent) const {
return processes_.size();
}

void ProcessItemModel::SetProcesses(std::vector<ProcessInfo> new_processes) {
void ProcessItemModel::SetProcesses(QVector<ProcessInfo> new_processes) {
orbit_base::sort(new_processes.begin(), new_processes.end(), &ProcessInfo::pid);

auto old_iter = processes_.begin();
Expand Down
Loading

0 comments on commit 0578f48

Please sign in to comment.