Skip to content

Commit 915b052

Browse files
committed
Handle failed connection more gracefully.
1 parent b12e870 commit 915b052

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

Studio/ShapeWorksMONAI/MonaiLabelJob.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ void MonaiLabelJob::setCurrentSampleNumber(int n) { sample_number_ = n; }
7373
void MonaiLabelJob::initializeClient() {
7474
SW_DEBUG("Initializing MONAI Client with server: {} tmp dir: {} client_id: {}", server_url_, tmp_dir_, client_id_);
7575
try {
76+
error_occurred_ = false;
77+
7678
py::module monai_label = py::module::import("MONAILabel");
7779
py::object monai_client_class = monai_label.attr("MONAILabelClient");
7880
py::str py_server_url(server_url_);
@@ -89,7 +91,7 @@ void MonaiLabelJob::initializeClient() {
8991
}
9092
model_name_ = getModelName(model_type_);
9193
models_available_[model_type_] = {model_name_};
92-
Q_EMIT triggerClientInitialized();
94+
triggerClientInitialized(!error_occurred_);
9395
} catch (std::exception &e) {
9496
SW_ERROR("Error importing MONAILabel or initializing MONAILabelClient: {}", e.what());
9597
return;
@@ -107,10 +109,13 @@ py::dict MonaiLabelJob::getInfo() {
107109
response = (*monai_client_).attr("info")();
108110
} catch (const py::error_already_set &e) {
109111
SW_ERROR("Python error: {}", e.what());
112+
error_occurred_ = true;
110113
} catch (const std::exception &e) {
111114
SW_ERROR("Exception occurred: {}", e.what());
115+
error_occurred_ = true;
112116
} catch (...) {
113117
SW_ERROR("An unknown error occurred!");
118+
error_occurred_ = true;
114119
}
115120
return response;
116121
}

Studio/ShapeWorksMONAI/MonaiLabelJob.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MonaiLabelJob : public Job {
7474

7575
Q_SIGNALS:
7676
void triggerUpdateView();
77-
void triggerClientInitialized();
77+
void triggerClientInitialized(bool success);
7878
void triggerUploadSampleCompleted();
7979
void triggerSegmentationCompleted();
8080
void triggerSubmitLabelCompleted();
@@ -108,5 +108,7 @@ class MonaiLabelJob : public Job {
108108

109109
QSharedPointer<Session> session_;
110110
ProjectHandle project_;
111+
112+
bool error_occurred_ = false;
111113
};
112114
} // namespace monailabel

Studio/ShapeWorksMONAI/MonaiLabelTool.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,23 @@ MonaiLabelTool::~MonaiLabelTool() {}
6868
//---------------------------------------------------------------------------
6969
void MonaiLabelTool::triggerUpdateView() { Q_EMIT update_view(); }
7070

71+
//---------------------------------------------------------------------------
72+
void MonaiLabelTool::set_connect_button() {
73+
Style::apply_normal_button_style(ui_->connectServerButton);
74+
ui_->connectServerButton->setText("Connect Server");
75+
ui_->connectServerButton->setIcon(QIcon(":/Studio/Images/connect.png"));
76+
ui_->connectServerButton->setEnabled(true);
77+
ui_->serverAddressField->setEnabled(true);
78+
}
79+
7180
//---------------------------------------------------------------------------
7281
void MonaiLabelTool::onConnectServer() {
7382
if (tool_is_running_ && monai_label_job_) {
7483
ui_->connectServerButton->setText("Disconnecting...");
7584
monai_label_job_->abort();
7685
shutdown();
7786
SW_STATUS("Server disconnected successfully.");
78-
Style::apply_normal_button_style(ui_->connectServerButton);
79-
ui_->connectServerButton->setText("Connect Server");
80-
ui_->connectServerButton->setIcon(QIcon(":/Studio/Images/connect.png"));
81-
ui_->connectServerButton->setEnabled(true);
82-
ui_->serverAddressField->setEnabled(true);
87+
set_connect_button();
8388
enable_actions();
8489
session_->get_project()->save();
8590
monai_label_job_ = nullptr;
@@ -118,7 +123,9 @@ void MonaiLabelTool::enable_actions() {
118123
ui_->runSegmentationButton->setEnabled(false);
119124
ui_->submitLabelButton->setEnabled(false);
120125
std::string feature_name = MonaiLabelUtils::getFeatureName(session_);
121-
if (!feature_name.empty()) session_->set_image_name(feature_name);
126+
if (!feature_name.empty()) {
127+
session_->set_image_name(feature_name);
128+
}
122129
}
123130

124131
//---------------------------------------------------------------------------
@@ -174,7 +181,12 @@ void MonaiLabelTool::runSegmentationTool() {
174181
}
175182

176183
//---------------------------------------------------------------------------
177-
void MonaiLabelTool::handleClientInitialized() {
184+
void MonaiLabelTool::handleClientInitialized(bool success) {
185+
if (!success) {
186+
SW_LOG("Connection failed");
187+
set_connect_button();
188+
return;
189+
}
178190
SW_LOG("✅ Connection successfully established to the server, continue with segmentation!");
179191
tool_is_running_ = true;
180192
if (session_->get_shapes().size() > 1) {

Studio/ShapeWorksMONAI/MonaiLabelTool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MonaiLabelTool : public QWidget {
5757
void triggerUpdateView();
5858
void handle_progress(int val, QString message);
5959
void handleSampleNumberChanged();
60-
void handleClientInitialized();
60+
void handleClientInitialized(bool success);
6161
void handleUploadSampleCompleted();
6262
void handleSegmentationCompleted();
6363
void handleSubmitLabelCompleted();
@@ -68,6 +68,9 @@ class MonaiLabelTool : public QWidget {
6868
void sampleChanged();
6969

7070
private:
71+
72+
void set_connect_button();
73+
7174
Preferences& preferences_;
7275
Ui_MonaiLabelTool* ui_;
7376
QSharedPointer<Session> session_;

0 commit comments

Comments
 (0)