C++11 tidying.
[dcpomatic.git] / src / lib / copy_to_drive_job.cc
1 /*
2     Copyright (C) 2019-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "disk_writer_messages.h"
22 #include "copy_to_drive_job.h"
23 #include "compose.hpp"
24 #include "exceptions.h"
25 #include "dcpomatic_log.h"
26 #include <dcp/raw_convert.h>
27 #include <nanomsg/nn.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <iostream>
34
35 #include "i18n.h"
36
37 using std::string;
38 using std::cout;
39 using std::min;
40 using std::shared_ptr;
41 using boost::optional;
42 using dcp::raw_convert;
43
44 CopyToDriveJob::CopyToDriveJob (boost::filesystem::path dcp, Drive drive, Nanomsg& nanomsg)
45         : Job (shared_ptr<Film>())
46         , _dcp (dcp)
47         , _drive (drive)
48         , _nanomsg (nanomsg)
49 {
50
51 }
52
53 string
54 CopyToDriveJob::name () const
55 {
56         return String::compose (_("Copying %1\nto %2"), _dcp.filename().string(), _drive.description());
57 }
58
59 string
60 CopyToDriveJob::json_name () const
61 {
62         return N_("copy");
63 }
64
65 void
66 CopyToDriveJob::run ()
67 {
68         LOG_DISK("Sending write request to disk writer for %1 %2", _dcp.string(), _drive.device());
69         if (!_nanomsg.send(String::compose(DISK_WRITER_WRITE "\n%1\n%2\n", _dcp.string(), _drive.device()), 2000)) {
70                 LOG_DISK_NC("Failed to send write request.");
71                 throw CommunicationFailedError ();
72         }
73
74         enum State {
75                 SETUP,
76                 FORMAT,
77                 COPY,
78                 VERIFY
79         } state = SETUP;
80
81         while (true) {
82                 optional<string> s = _nanomsg.receive (10000);
83                 if (!s) {
84                         continue;
85                 }
86                 if (*s == DISK_WRITER_OK) {
87                         set_state (FINISHED_OK);
88                         return;
89                 } else if (*s == DISK_WRITER_ERROR) {
90                         auto const m = _nanomsg.receive (500);
91                         auto const n = _nanomsg.receive (500);
92                         throw CopyError (m.get_value_or("Unknown"), raw_convert<int>(n.get_value_or("0")));
93                 } else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
94                         if (state == SETUP) {
95                                 sub (_("Formatting drive"));
96                                 state = FORMAT;
97                         }
98                         auto progress = _nanomsg.receive (500);
99                         if (progress) {
100                                 set_progress (raw_convert<float>(*progress));
101                         }
102                 } else if (*s == DISK_WRITER_COPY_PROGRESS) {
103                         if (state == FORMAT) {
104                                 sub (_("Copying DCP"));
105                                 state = COPY;
106                         }
107                         auto progress = _nanomsg.receive (500);
108                         if (progress) {
109                                 set_progress (raw_convert<float>(*progress));
110                         }
111                 } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
112                         if (state == COPY) {
113                                 sub (_("Verifying copied files"));
114                                 state = VERIFY;
115                         }
116                         auto progress = _nanomsg.receive (500);
117                         if (progress) {
118                                 set_progress (raw_convert<float>(*progress));
119                         }
120                 }
121         }
122 }