Propagate resolved ip address to the UI layer (both upload dialog and notification)

This commit is contained in:
David Kocik 2022-05-09 12:56:24 +02:00
parent bc167ce90b
commit 89e288dcf8
18 changed files with 84 additions and 28 deletions

View File

@ -1823,7 +1823,7 @@ void NotificationManager::push_upload_job_notification(int id, float filesize,
}
std::string text = PrintHostUploadNotification::get_upload_job_text(id, filename, host);
NotificationData data{ NotificationType::PrintHostUpload, NotificationLevel::ProgressBarNotificationLevel, 10, text };
push_notification_data(std::make_unique<NotificationManager::PrintHostUploadNotification>(data, m_id_provider, m_evt_handler, 0, id, filesize), 0);
push_notification_data(std::make_unique<NotificationManager::PrintHostUploadNotification>(data, m_id_provider, m_evt_handler, 0, id, filesize, filename, host), 0);
}
void NotificationManager::set_upload_job_notification_percentage(int id, const std::string& filename, const std::string& host, float percentage)
{
@ -1832,6 +1832,21 @@ void NotificationManager::set_upload_job_notification_percentage(int id, const s
PrintHostUploadNotification* phun = dynamic_cast<PrintHostUploadNotification*>(notification.get());
if (phun->compare_job_id(id)) {
phun->set_percentage(percentage);
if (phun->get_host() != host)
phun->set_host(host);
wxGetApp().plater()->get_current_canvas3D()->schedule_extra_frame(0);
break;
}
}
}
}
void NotificationManager::set_upload_job_notification_host(int id, const std::string& host)
{
for (std::unique_ptr<PopNotification>& notification : m_pop_notifications) {
if (notification->get_type() == NotificationType::PrintHostUpload) {
PrintHostUploadNotification* phun = dynamic_cast<PrintHostUploadNotification*>(notification.get());
if (phun->compare_job_id(id)) {
phun->set_host(host);
wxGetApp().plater()->get_current_canvas3D()->schedule_extra_frame(0);
break;
}
@ -1845,6 +1860,8 @@ void NotificationManager::upload_job_notification_show_canceled(int id, const st
PrintHostUploadNotification* phun = dynamic_cast<PrintHostUploadNotification*>(notification.get());
if (phun->compare_job_id(id)) {
phun->cancel();
if (phun->get_host() != host)
phun->set_host(host);
wxGetApp().plater()->get_current_canvas3D()->schedule_extra_frame(0);
break;
}
@ -1858,6 +1875,8 @@ void NotificationManager::upload_job_notification_show_error(int id, const std::
PrintHostUploadNotification* phun = dynamic_cast<PrintHostUploadNotification*>(notification.get());
if(phun->compare_job_id(id)) {
phun->error();
if (phun->get_host() != host)
phun->set_host(host);
wxGetApp().plater()->get_current_canvas3D()->schedule_extra_frame(0);
break;
}

View File

@ -203,6 +203,7 @@ public:
// print host upload
void push_upload_job_notification(int id, float filesize, const std::string& filename, const std::string& host, float percentage = 0);
void set_upload_job_notification_percentage(int id, const std::string& filename, const std::string& host, float percentage);
void set_upload_job_notification_host(int id, const std::string& host);
void upload_job_notification_show_canceled(int id, const std::string& filename, const std::string& host);
void upload_job_notification_show_error(int id, const std::string& filename, const std::string& host);
// Download App progress
@ -505,10 +506,12 @@ private:
PB_CANCELLED,
PB_COMPLETED
};
PrintHostUploadNotification(const NotificationData& n, NotificationIDProvider& id_provider, wxEvtHandler* evt_handler, float percentage, int job_id, float filesize)
PrintHostUploadNotification(const NotificationData& n, NotificationIDProvider& id_provider, wxEvtHandler* evt_handler, float percentage, int job_id, float filesize, const std::string& filename, const std::string& host)
:ProgressBarNotification(n, id_provider, evt_handler)
, m_job_id(job_id)
, m_file_size(filesize)
, m_filename(filename)
, m_host(host)
{
m_has_cancel_button = true;
set_percentage(percentage);
@ -519,6 +522,8 @@ private:
void error() { m_uj_state = UploadJobState::PB_ERROR; m_has_cancel_button = false; init(); }
bool compare_job_id(const int other_id) const { return m_job_id == other_id; }
bool compare_text(const std::string& text) const override { return false; }
void set_host(const std::string& host) { m_host = host; update({ NotificationType::PrintHostUpload, NotificationLevel::ProgressBarNotificationLevel, 10, get_upload_job_text(m_id, m_filename, m_host)}); }
std::string get_host() const { return m_host; }
protected:
void init() override;
void count_spaces() override;
@ -536,6 +541,8 @@ private:
float m_file_size;
long m_hover_time{ 0 };
UploadJobState m_uj_state{ UploadJobState::PB_PROGRESS };
std::string m_filename;
std::string m_host;
};
class SlicingProgressNotification : public ProgressBarNotification

View File

@ -190,6 +190,7 @@ void PrintHostSendDialog::EndModal(int ret)
wxDEFINE_EVENT(EVT_PRINTHOST_PROGRESS, PrintHostQueueDialog::Event);
wxDEFINE_EVENT(EVT_PRINTHOST_ERROR, PrintHostQueueDialog::Event);
wxDEFINE_EVENT(EVT_PRINTHOST_CANCEL, PrintHostQueueDialog::Event);
wxDEFINE_EVENT(EVT_PRINTHOST_RESOLVE, PrintHostQueueDialog::Event);
PrintHostQueueDialog::Event::Event(wxEventType eventType, int winid, size_t job_id)
: wxEvent(winid, eventType)
@ -218,6 +219,7 @@ PrintHostQueueDialog::PrintHostQueueDialog(wxWindow *parent)
, on_progress_evt(this, EVT_PRINTHOST_PROGRESS, &PrintHostQueueDialog::on_progress, this)
, on_error_evt(this, EVT_PRINTHOST_ERROR, &PrintHostQueueDialog::on_error, this)
, on_cancel_evt(this, EVT_PRINTHOST_CANCEL, &PrintHostQueueDialog::on_cancel, this)
, on_resolve_evt(this, EVT_PRINTHOST_RESOLVE, &PrintHostQueueDialog::on_resolve, this)
{
const auto em = GetTextExtent("m").x;
@ -450,6 +452,17 @@ void PrintHostQueueDialog::on_cancel(Event &evt)
wxGetApp().notification_manager()->upload_job_notification_show_canceled(evt.job_id + 1, boost::nowide::narrow(nm.GetString()), boost::nowide::narrow(hst.GetString()));
}
void PrintHostQueueDialog::on_resolve(Event& evt)
{
wxCHECK_RET(evt.job_id < (size_t)job_list->GetItemCount(), "Out of bounds access to job list");
// wxstring in event is called error, but it should contain new host string.
wxVariant hst(evt.error);
// todo: set variant
job_list->SetValue(hst,evt.job_id,COL_HOST);
wxGetApp().notification_manager()->set_upload_job_notification_host(evt.job_id + 1, boost::nowide::narrow(evt.error));
}
void PrintHostQueueDialog::get_active_jobs(std::vector<std::pair<std::string, std::string>>& ret)
{
int ic = job_list->GetItemCount();

View File

@ -108,6 +108,7 @@ private:
EventGuard on_progress_evt;
EventGuard on_error_evt;
EventGuard on_cancel_evt;
EventGuard on_resolve_evt;
JobState get_state(int idx);
void set_state(int idx, JobState);
@ -115,6 +116,7 @@ private:
void on_progress(Event&);
void on_error(Event&);
void on_cancel(Event&);
void on_resolve(Event&);
// This vector keep adress and filename of uploads. It is used when checking for running uploads during exit.
std::vector<std::pair<std::string, std::string>> upload_names;
void save_user_data(int);
@ -124,7 +126,7 @@ private:
wxDECLARE_EVENT(EVT_PRINTHOST_PROGRESS, PrintHostQueueDialog::Event);
wxDECLARE_EVENT(EVT_PRINTHOST_ERROR, PrintHostQueueDialog::Event);
wxDECLARE_EVENT(EVT_PRINTHOST_CANCEL, PrintHostQueueDialog::Event);
wxDECLARE_EVENT(EVT_PRINTHOST_RESOLVE, PrintHostQueueDialog::Event);
}}
#endif

View File

@ -92,7 +92,7 @@ wxString AstroBox::get_test_failed_msg (wxString &msg) const
% _utf8(L("Note: AstroBox version at least 1.1.0 is required."))).str());
}
bool AstroBox::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool AstroBox::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
const char *name = get_name();

View File

@ -23,7 +23,7 @@ public:
bool test(wxString &curl_msg) const override;
wxString get_test_ok_msg () const override;
wxString get_test_failed_msg (wxString &msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return true; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint; }

View File

@ -54,7 +54,7 @@ wxString Duet::get_test_failed_msg (wxString &msg) const
% std::string(msg.ToUTF8())).str());
}
bool Duet::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool Duet::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
wxString connect_msg;
auto connectionType = connect(connect_msg);

View File

@ -22,7 +22,7 @@ public:
bool test(wxString &curl_msg) const override;
wxString get_test_ok_msg() const override;
wxString get_test_failed_msg(wxString &msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint | PrintHostPostUploadAction::StartSimulation; }

View File

@ -76,7 +76,7 @@ wxString FlashAir::get_test_failed_msg (wxString &msg) const
% _utf8(L("Note: FlashAir with firmware 2.00.02 or newer and activated upload function is required."))).str());
}
bool FlashAir::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool FlashAir::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
const char *name = get_name();

View File

@ -23,7 +23,7 @@ public:
bool test(wxString &curl_msg) const override;
wxString get_test_ok_msg() const override;
wxString get_test_failed_msg(wxString &msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return {}; }

View File

@ -62,7 +62,7 @@ wxString MKS::get_test_failed_msg(wxString& msg) const
% std::string(msg.ToUTF8())).str());
}
bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
bool res = true;

View File

@ -22,7 +22,7 @@ public:
bool test(wxString& curl_msg) const override;
wxString get_test_ok_msg() const override;
wxString get_test_failed_msg(wxString& msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint; }

View File

@ -118,11 +118,11 @@ bool OctoPrint::test_with_resolved_ip(wxString &msg) const
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url;
auto http = Http::get(std::move(url));
auto http = Http::get(url);//std::move(url));
set_auth(http);
http
.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting version at %2% : %3%, HTTP %4%, body: `%5%`") % name % url % error % status % body;
res = false;
msg = format_error(body, error, status);
})
@ -226,10 +226,10 @@ wxString OctoPrint::get_test_failed_msg (wxString &msg) const
% _utf8(L("Note: OctoPrint version at least 1.1.0 is required."))).str());
}
bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
#ifndef WIN32
return upload_inner_with_host(upload_data, prorgess_fn, error_fn);
return upload_inner_with_host(upload_data, prorgess_fn, error_fn, resolve_fn);
#else
// decide what to do based on m_host - resolve hostname or upload to ip
std::vector<boost::asio::ip::address> resolved_addr;
@ -255,30 +255,32 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro
if (resolved_addr.empty()) {
// no resolved addresses - try system resolving
BOOST_LOG_TRIVIAL(error) << "PrusaSlicer failed to resolve hostname " << m_host << " into the IP address. Starting upload with system resolving.";
return upload_inner_with_host(upload_data, prorgess_fn, error_fn);
return upload_inner_with_host(upload_data, prorgess_fn, error_fn, resolve_fn);
} else if (resolved_addr.size() == 1) {
// one address resolved - upload there
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr.front());
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolve_fn, resolved_addr.front());
} else if (resolved_addr.size() == 2 && resolved_addr[0].is_v4() != resolved_addr[1].is_v4()) {
// there are just 2 addresses and 1 is ip_v4 and other is ip_v6
// try sending to both. (First without verbose, so user doesnt get error msg twice)
if (!upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr.front(), false))
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr.back());
if (!upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolve_fn, resolved_addr.front(), false))
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolve_fn, resolved_addr.back());
return true;
} else {
// There are multiple addresses - user needs to choose which to use.
size_t selected_index = resolved_addr.size();
IPListDialog dialog(nullptr, boost::nowide::widen(m_host), resolved_addr, selected_index);
if (dialog.ShowModal() == wxID_OK && selected_index < resolved_addr.size()) {
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr[selected_index]);
return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolve_fn, resolved_addr[selected_index]);
}
}
return false;
#endif // WIN32
}
#ifdef WIN32
bool OctoPrint::upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const boost::asio::ip::address& resolved_addr, bool verbose /*= true*/) const
bool OctoPrint::upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn, const boost::asio::ip::address& resolved_addr, bool verbose /*= true*/) const
{
resolve_fn(boost::nowide::widen(resolved_addr.to_string()));
// If test fails, test_msg_or_host_ip contains the error message.
// Otherwise on Windows it contains the resolved IP address of the host.
// Test_msg already contains resolved ip and will be cleared on start of test().
@ -337,7 +339,7 @@ bool OctoPrint::upload_inner_with_resolved_ip(PrintHostUpload upload_data, Progr
}
#endif //WIN32
bool OctoPrint::upload_inner_with_host(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool OctoPrint::upload_inner_with_host(PrintHostUpload upload_data, ProgressFn prorgess_fn, ResolveFn resolve_fn, ErrorFn error_fn) const
{
const char* name = get_name();
@ -372,6 +374,7 @@ bool OctoPrint::upload_inner_with_host(PrintHostUpload upload_data, ProgressFn p
// This new address returns in "test_msg_or_host_ip" variable.
// Solves troubles of uploades failing with name address.
// in original address (m_host) replace host for resolved ip
resolve_fn(test_msg_or_host_ip);
url = substitute_host(make_url("api/files/local"), GUI::into_u8(test_msg_or_host_ip));
BOOST_LOG_TRIVIAL(info) << "Upload address after ip resolve: " << url;
}

View File

@ -26,7 +26,7 @@ public:
bool test(wxString &curl_msg) const override;
wxString get_test_ok_msg () const override;
wxString get_test_failed_msg (wxString &msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return true; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint; }
@ -37,10 +37,10 @@ public:
protected:
virtual bool validate_version_text(const boost::optional<std::string> &version_text) const;
#ifdef WIN32
virtual bool upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const boost::asio::ip::address& resolved_addr, bool verbose = true) const;
virtual bool upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn, const boost::asio::ip::address& resolved_addr, bool verbose = true) const;
virtual bool test_with_resolved_ip(wxString& curl_msg) const;
#endif
virtual bool upload_inner_with_host(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const;
virtual bool upload_inner_with_host(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const;
private:
std::string m_host;

View File

@ -93,6 +93,7 @@ struct PrintHostJobQueue::priv
void emit_progress(int progress);
void emit_error(wxString error);
void emit_cancel(size_t id);
void emit_resolve(wxString host);
void start_bg_thread();
void stop_bg_thread();
void bg_thread_main();
@ -131,6 +132,13 @@ void PrintHostJobQueue::priv::emit_cancel(size_t id)
wxQueueEvent(queue_dialog, evt);
}
void PrintHostJobQueue::priv::emit_resolve(wxString host)
{
auto evt = new PrintHostQueueDialog::Event(GUI::EVT_PRINTHOST_RESOLVE, queue_dialog->GetId(), job_id, host);
wxQueueEvent(queue_dialog, evt);
}
void PrintHostJobQueue::priv::start_bg_thread()
{
if (bg_thread.joinable()) { return; }
@ -258,6 +266,9 @@ void PrintHostJobQueue::priv::perform_job(PrintHostJob the_job)
[this](Http::Progress progress, bool &cancel) { this->progress_fn(std::move(progress), cancel); },
[this](wxString error) {
emit_error(std::move(error));
},
[this](wxString host) {
emit_resolve(std::move(host));
}
);

View File

@ -43,13 +43,14 @@ public:
typedef Http::ProgressFn ProgressFn;
typedef std::function<void(wxString /* error */)> ErrorFn;
typedef ErrorFn ResolveFn;
virtual const char* get_name() const = 0;
virtual bool test(wxString &curl_msg) const = 0;
virtual wxString get_test_ok_msg () const = 0;
virtual wxString get_test_failed_msg (wxString &msg) const = 0;
virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const = 0;
virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const = 0;
virtual bool has_auto_discovery() const = 0;
virtual bool can_test() const = 0;
virtual PrintHostPostUploadActions get_post_upload_actions() const = 0;

View File

@ -92,7 +92,7 @@ wxString Repetier::get_test_failed_msg (wxString &msg) const
% _utf8(L("Note: Repetier version at least 0.90.0 is required."))).str());
}
bool Repetier::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const
bool Repetier::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const
{
const char *name = get_name();

View File

@ -23,7 +23,7 @@ public:
bool test(wxString &curl_msg) const override;
wxString get_test_ok_msg () const override;
wxString get_test_failed_msg (wxString &msg) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, ResolveFn resolve_fn) const override;
bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; }
PrintHostPostUploadActions get_post_upload_actions() const override { return PrintHostPostUploadAction::StartPrint; }