Rename TYPE_DEBUG_PLAYER to TYPE_DEBUG_VIDEO_VIEW.
[dcpomatic.git] / src / lib / copy_to_drive_job.cc
index b7bb5a60d26844334c4bcb383179dec484487056..0fb41f85642cb3a6fa26929376c4080f2ad51adb 100644 (file)
@@ -22,6 +22,7 @@
 #include "copy_to_drive_job.h"
 #include "compose.hpp"
 #include "exceptions.h"
+#include "dcpomatic_log.h"
 #include <dcp/raw_convert.h>
 #include <nanomsg/nn.h>
 #include <unistd.h>
@@ -37,6 +38,7 @@ using std::string;
 using std::cout;
 using std::min;
 using boost::shared_ptr;
+using boost::optional;
 using dcp::raw_convert;
 
 CopyToDriveJob::CopyToDriveJob (boost::filesystem::path dcp, Drive drive, Nanomsg& nanomsg)
@@ -63,30 +65,52 @@ CopyToDriveJob::json_name () const
 void
 CopyToDriveJob::run ()
 {
-       if (!_nanomsg.nonblocking_send(String::compose("W\n%1\n%2\n", _dcp.string(), _drive.internal_name()))) {
-               throw CopyError ("Could not communicate with writer process", 0);
+       LOG_DISK("Sending write request to disk writer for %1 %2", _dcp.string(), _drive.device());
+       if (!_nanomsg.send(String::compose(DISK_WRITER_WRITE "\n%1\n%2\n", _dcp.string(), _drive.device()), 2000)) {
+               throw CommunicationFailedError ();
        }
 
-       bool formatting = false;
+       enum State {
+               SETUP,
+               FORMAT,
+               COPY,
+               VERIFY
+       } state = SETUP;
+
        while (true) {
-               string s = _nanomsg.blocking_get ();
-               if (s == DISK_WRITER_OK) {
+               optional<string> s = _nanomsg.receive (10000);
+               if (!s) {
+                       continue;
+               }
+               if (*s == DISK_WRITER_OK) {
                        set_state (FINISHED_OK);
                        return;
-               } else if (s == DISK_WRITER_ERROR) {
-                       string const m = _nanomsg.blocking_get ();
-                       string const n = _nanomsg.blocking_get ();
-                       throw CopyError (m, raw_convert<int>(n));
-               } else if (s == DISK_WRITER_FORMATTING) {
-                       sub ("Formatting drive");
+               } else if (*s == DISK_WRITER_ERROR) {
+                       optional<string> const m = _nanomsg.receive (500);
+                       optional<string> const n = _nanomsg.receive (500);
+                       throw CopyError (m.get_value_or("Unknown"), raw_convert<int>(n.get_value_or("0")));
+               } else if (*s == DISK_WRITER_FORMATTING) {
+                       sub (_("Formatting drive"));
                        set_progress_unknown ();
-                       formatting = true;
-               } else if (s == DISK_WRITER_PROGRESS) {
-                       if (formatting) {
-                               sub ("Copying DCP");
-                               formatting = false;
+                       state = FORMAT;
+               } else if (*s == DISK_WRITER_COPY_PROGRESS) {
+                       if (state == FORMAT) {
+                               sub (_("Copying DCP"));
+                               state = COPY;
+                       }
+                       optional<string> progress = _nanomsg.receive (500);
+                       if (progress) {
+                               set_progress (raw_convert<float>(*progress));
+                       }
+               } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
+                       if (state == COPY) {
+                               sub (_("Verifying copied files"));
+                               state = VERIFY;
+                       }
+                       optional<string> progress = _nanomsg.receive (500);
+                       if (progress) {
+                               set_progress (raw_convert<float>(*progress));
                        }
-                       set_progress (raw_convert<float>(_nanomsg.blocking_get()));
                }
        }
 }