2 Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "dcpomatic_assert.h"
23 #include "disk_writer_messages.h"
25 #include <dcp/raw_convert.h>
29 using boost::optional;
32 boost::optional<DiskWriterBackEndResponse>
33 DiskWriterBackEndResponse::read_from_nanomsg(Nanomsg& nanomsg, int timeout)
35 auto s = nanomsg.receive(timeout);
39 if (*s == DISK_WRITER_OK) {
40 return DiskWriterBackEndResponse::ok();
41 } else if (*s == DISK_WRITER_ERROR) {
42 auto const m = nanomsg.receive(500);
43 auto const n = nanomsg.receive(500);
44 auto const p = nanomsg.receive(500);
45 return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::raw_convert<int>(n.get_value_or("0")), dcp::raw_convert<int>(p.get_value_or("0")));
46 } else if (*s == DISK_WRITER_PONG) {
47 return DiskWriterBackEndResponse::pong();
48 } else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
49 auto progress = nanomsg.receive(500);
50 return DiskWriterBackEndResponse::format_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
51 } else if (*s == DISK_WRITER_COPY_PROGRESS) {
52 auto progress = nanomsg.receive(500);
53 return DiskWriterBackEndResponse::copy_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
54 } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
55 auto progress = nanomsg.receive(500);
56 return DiskWriterBackEndResponse::verify_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
58 DCPOMATIC_ASSERT(false);
65 /** @return true if the message was sent, false if there was a timeout */
67 DiskWriterBackEndResponse::write_to_nanomsg(Nanomsg& nanomsg, int timeout) const
74 message = String::compose("%1\n", DISK_WRITER_OK);
77 message = String::compose("%1\n%2\n%3\n%4\n", DISK_WRITER_ERROR, _error_message, _ext4_error_number, _platform_error_number);
80 message = String::compose("%1\n", DISK_WRITER_PONG);
82 case Type::FORMAT_PROGRESS:
83 message = String::compose("%1\n", DISK_WRITER_FORMAT_PROGRESS);
84 message += dcp::raw_convert<string>(_progress) + "\n";
86 case Type::COPY_PROGRESS:
87 message = String::compose("%1\n", DISK_WRITER_COPY_PROGRESS);
88 message += dcp::raw_convert<string>(_progress) + "\n";
90 case Type::VERIFY_PROGRESS:
91 message = String::compose("%1\n", DISK_WRITER_VERIFY_PROGRESS);
92 message += dcp::raw_convert<string>(_progress) + "\n";
97 return nanomsg.send(message, timeout);