Add DiskWriterBackendResponse::write_to_nanomsg() and use it
authorCarl Hetherington <cth@carlh.net>
Thu, 26 Jan 2023 23:43:05 +0000 (00:43 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 29 Jan 2023 21:49:56 +0000 (22:49 +0100)
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
src/lib/disk_writer_messages.h
src/lib/ext.cc
src/tools/dcpomatic_disk_writer.cc

index e42e80ec628c27c463a7d6dc8bf63f4027c9e4d2..a862f2fc6f52b2756ac3d58520b6bf705cdba533 100644 (file)
 #include "dcpomatic_assert.h"
 #include "disk_writer_messages.h"
 #include "nanomsg.h"
+#include <dcp/raw_convert.h>
 
 
+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<int>(n.get_value_or("0")));
+               return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::raw_convert<int>(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<float>(progress.get_value_or("0")));
+               return DiskWriterBackEndResponse::format_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
        } else if (*s == DISK_WRITER_COPY_PROGRESS) {
                auto progress = nanomsg.receive(500);
-               return DiskWriterBackEndResponse::copy_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
+               return DiskWriterBackEndResponse::copy_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
        } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
                auto progress = nanomsg.receive(500);
-               return DiskWriterBackEndResponse::verify_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
+               return DiskWriterBackEndResponse::verify_progress(dcp::raw_convert<float>(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<string>(_progress) + "\n";
+                       break;
+               case Type::COPY_PROGRESS:
+                       message = String::compose("%1\n", DISK_WRITER_COPY_PROGRESS);
+                       message += dcp::raw_convert<string>(_progress) + "\n";
+                       break;
+               case Type::VERIFY_PROGRESS:
+                       message = String::compose("%1\n", DISK_WRITER_VERIFY_PROGRESS);
+                       message += dcp::raw_convert<string>(_progress) + "\n";
+                       break;
+       }
+
+
+       return nanomsg.send(message, timeout);
+}
+
+
index ff6a0b394196f620dcc0b400d52645205646ca9d..8bd1837a83946c6882d55d1b70bbe0ec3e901394 100644 (file)
@@ -137,6 +137,8 @@ public:
 
        static boost::optional<DiskWriterBackEndResponse> read_from_nanomsg(Nanomsg& nanomsg, int timeout);
 
+       bool write_to_nanomsg(Nanomsg& nanomsg, int timeout) const;
+
        Type type() const {
                return _type;
        }
index 49e63d6481c1c6300f5d6927aa4e6b0776fc80b5..29f44d90e8296cb7af98cbea7a1d97552901859e 100644 (file)
@@ -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<Nanomsg*>(context)->send(String::compose(DISK_WRITER_FORMAT_PROGRESS "\n%1\n", progress), SHORT_TIMEOUT);
+               DiskWriterBackEndResponse::format_progress(progress).write_to_nanomsg(*reinterpret_cast<Nanomsg*>(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);
        }
 }
 
index ef3bf2f776c86936b6ad443e26b3d717eca1d26b..5a10d75cbc7e2d8d1b2c2164e6a2a04e0f784ea8 100644 (file)
@@ -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);
                                }
                        });
        }