From 7bce98ec4c05081569cb66b68523e1466058ea76 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 27 Jan 2023 00:43:05 +0100 Subject: [PATCH] Add DiskWriterBackendResponse::write_to_nanomsg() and use it everywhere. In the process, use raw_convert() on both ends to avoid any locale-based problems with floating point number separators. --- src/lib/disk_writer_messages.cc | 47 +++++++++++++++++++++++++++--- src/lib/disk_writer_messages.h | 2 ++ src/lib/ext.cc | 14 ++++----- src/tools/dcpomatic_disk_writer.cc | 20 ++++++------- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/lib/disk_writer_messages.cc b/src/lib/disk_writer_messages.cc index e42e80ec6..a862f2fc6 100644 --- a/src/lib/disk_writer_messages.cc +++ b/src/lib/disk_writer_messages.cc @@ -22,8 +22,10 @@ #include "dcpomatic_assert.h" #include "disk_writer_messages.h" #include "nanomsg.h" +#include +using std::string; using boost::optional; @@ -39,18 +41,18 @@ DiskWriterBackEndResponse::read_from_nanomsg(Nanomsg& nanomsg, int timeout) } else if (*s == DISK_WRITER_ERROR) { auto const m = nanomsg.receive(500); auto const n = nanomsg.receive(500); - return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::locale_convert(n.get_value_or("0"))); + return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::raw_convert(n.get_value_or("0"))); } else if (*s == DISK_WRITER_PONG) { return DiskWriterBackEndResponse::pong(); } else if (*s == DISK_WRITER_FORMAT_PROGRESS) { auto progress = nanomsg.receive(500); - return DiskWriterBackEndResponse::format_progress(dcp::locale_convert(progress.get_value_or("0"))); + return DiskWriterBackEndResponse::format_progress(dcp::raw_convert(progress.get_value_or("0"))); } else if (*s == DISK_WRITER_COPY_PROGRESS) { auto progress = nanomsg.receive(500); - return DiskWriterBackEndResponse::copy_progress(dcp::locale_convert(progress.get_value_or("0"))); + return DiskWriterBackEndResponse::copy_progress(dcp::raw_convert(progress.get_value_or("0"))); } else if (*s == DISK_WRITER_VERIFY_PROGRESS) { auto progress = nanomsg.receive(500); - return DiskWriterBackEndResponse::verify_progress(dcp::locale_convert(progress.get_value_or("0"))); + return DiskWriterBackEndResponse::verify_progress(dcp::raw_convert(progress.get_value_or("0"))); } else { DCPOMATIC_ASSERT(false); } @@ -58,3 +60,40 @@ DiskWriterBackEndResponse::read_from_nanomsg(Nanomsg& nanomsg, int timeout) return {}; } + +/** @return true if the message was sent, false if there was a timeout */ +bool +DiskWriterBackEndResponse::write_to_nanomsg(Nanomsg& nanomsg, int timeout) const +{ + string message; + + switch (_type) + { + case Type::OK: + message = String::compose("%1\n", DISK_WRITER_OK); + break; + case Type::ERROR: + message = String::compose("%1\n%2\n%3\n", DISK_WRITER_ERROR, _error_message, _error_number); + break; + case Type::PONG: + message = String::compose("%1\n", DISK_WRITER_PONG); + break; + case Type::FORMAT_PROGRESS: + message = String::compose("%1\n", DISK_WRITER_FORMAT_PROGRESS); + message += dcp::raw_convert(_progress) + "\n"; + break; + case Type::COPY_PROGRESS: + message = String::compose("%1\n", DISK_WRITER_COPY_PROGRESS); + message += dcp::raw_convert(_progress) + "\n"; + break; + case Type::VERIFY_PROGRESS: + message = String::compose("%1\n", DISK_WRITER_VERIFY_PROGRESS); + message += dcp::raw_convert(_progress) + "\n"; + break; + } + + + return nanomsg.send(message, timeout); +} + + diff --git a/src/lib/disk_writer_messages.h b/src/lib/disk_writer_messages.h index ff6a0b394..8bd1837a8 100644 --- a/src/lib/disk_writer_messages.h +++ b/src/lib/disk_writer_messages.h @@ -137,6 +137,8 @@ public: static boost::optional read_from_nanomsg(Nanomsg& nanomsg, int timeout); + bool write_to_nanomsg(Nanomsg& nanomsg, int timeout) const; + Type type() const { return _type; } diff --git a/src/lib/ext.cc b/src/lib/ext.cc index 49e63d648..29f44d90e 100644 --- a/src/lib/ext.cc +++ b/src/lib/ext.cc @@ -153,7 +153,7 @@ write (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total ++progress_count; if ((progress_count % progress_frequency) == 0 && nanomsg) { - nanomsg->send(String::compose(DISK_WRITER_COPY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT); + DiskWriterBackEndResponse::copy_progress(1 - float(total_remaining) / total).write_to_nanomsg(*nanomsg, SHORT_TIMEOUT); } } @@ -194,7 +194,7 @@ read (boost::filesystem::path from, boost::filesystem::path to, uint64_t& total_ remaining -= this_time; total_remaining -= this_time; if (nanomsg) { - nanomsg->send(String::compose(DISK_WRITER_VERIFY_PROGRESS "\n%1\n", (1 - float(total_remaining) / total)), SHORT_TIMEOUT); + DiskWriterBackEndResponse::verify_progress(1 - float(total_remaining) / total).write_to_nanomsg(*nanomsg, SHORT_TIMEOUT); } } @@ -272,7 +272,7 @@ void format_progress (void* context, float progress) { if (context) { - reinterpret_cast(context)->send(String::compose(DISK_WRITER_FORMAT_PROGRESS "\n%1\n", progress), SHORT_TIMEOUT); + DiskWriterBackEndResponse::format_progress(progress).write_to_nanomsg(*reinterpret_cast(context), SHORT_TIMEOUT); } } @@ -419,7 +419,7 @@ try } ext4_device_unregister("ext4_fs"); - if (nanomsg && !nanomsg->send(DISK_WRITER_OK "\n", LONG_TIMEOUT)) { + if (nanomsg && !DiskWriterBackEndResponse::ok().write_to_nanomsg(*nanomsg, LONG_TIMEOUT)) { throw CommunicationFailedError (); } @@ -427,17 +427,17 @@ try } catch (CopyError& e) { LOG_DISK("CopyError (from write): %1 %2", e.message(), e.number().get_value_or(0)); if (nanomsg) { - nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number().get_value_or(0)), LONG_TIMEOUT); + DiskWriterBackEndResponse::error(e.message(), e.number().get_value_or(0)).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } } catch (VerifyError& e) { LOG_DISK("VerifyError (from write): %1 %2", e.message(), e.number()); if (nanomsg) { - nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n%2\n", e.message(), e.number()), LONG_TIMEOUT); + DiskWriterBackEndResponse::error(e.message(), e.number()).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } } catch (exception& e) { LOG_DISK("Exception (from write): %1", e.what()); if (nanomsg) { - nanomsg->send(String::compose(DISK_WRITER_ERROR "\n%1\n0\n", e.what()), LONG_TIMEOUT); + DiskWriterBackEndResponse::error(e.what(), 0).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } } diff --git a/src/tools/dcpomatic_disk_writer.cc b/src/tools/dcpomatic_disk_writer.cc index ef3bf2f77..5a10d75cb 100644 --- a/src/tools/dcpomatic_disk_writer.cc +++ b/src/tools/dcpomatic_disk_writer.cc @@ -164,7 +164,7 @@ try if (*s == DISK_WRITER_QUIT) { exit (EXIT_SUCCESS); } else if (*s == DISK_WRITER_PING) { - nanomsg->send(DISK_WRITER_PONG "\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::pong().write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } else if (*s == DISK_WRITER_UNMOUNT) { auto xml_head = nanomsg->receive (LONG_TIMEOUT); auto xml_body = nanomsg->receive (LONG_TIMEOUT); @@ -179,9 +179,9 @@ try bool const success = Drive(xml).unmount(); bool sent_reply = false; if (success) { - sent_reply = nanomsg->send(DISK_WRITER_OK "\n", LONG_TIMEOUT); + sent_reply = DiskWriterBackEndResponse::ok().write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } else { - sent_reply = nanomsg->send(DISK_WRITER_ERROR "\nCould not unmount drive\n1\n", LONG_TIMEOUT); + sent_reply = DiskWriterBackEndResponse::error("Could not unmount drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } if (!sent_reply) { LOG_DISK_NC("CommunicationFailedError in unmount_finished"); @@ -189,7 +189,7 @@ try } }, []() { - if (!nanomsg->send(DISK_WRITER_ERROR "\nCould not get permission to unmount drive\n1\n", LONG_TIMEOUT)) { + if (!DiskWriterBackEndResponse::error("Could not get permission to unmount drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT)) { LOG_DISK_NC("CommunicationFailedError in unmount_finished"); throw CommunicationFailedError (); } @@ -221,21 +221,21 @@ try #ifdef DCPOMATIC_OSX if (!starts_with(device, "/dev/disk")) { LOG_DISK ("Will not write to %1", device); - nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Refusing to write to this drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); return true; } #endif #ifdef DCPOMATIC_LINUX if (!starts_with(device, "/dev/sd") && !starts_with(device, "/dev/hd")) { LOG_DISK ("Will not write to %1", device); - nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Refusing to write to this drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); return true; } #endif #ifdef DCPOMATIC_WINDOWS if (!starts_with(device, "\\\\.\\PHYSICALDRIVE")) { LOG_DISK ("Will not write to %1", device); - nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Refusing to write to this drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); return true; } #endif @@ -251,12 +251,12 @@ try if (!on_drive_list) { LOG_DISK ("Will not write to %1 as it's not recognised as a drive", device); - nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Refusing to write to this drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); return true; } if (mounted) { LOG_DISK ("Will not write to %1 as it's mounted", device); - nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Refusing to write to this drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); return true; } @@ -286,7 +286,7 @@ try }, []() { if (nanomsg) { - nanomsg->send(DISK_WRITER_ERROR "\nCould not obtain authorization to write to the drive\n1\n", LONG_TIMEOUT); + DiskWriterBackEndResponse::error("Could not obtain authorization to write to the drive", 1).write_to_nanomsg(*nanomsg, LONG_TIMEOUT); } }); } -- 2.30.2