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 return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::raw_convert<int>(n.get_value_or("0")));
45 } else if (*s == DISK_WRITER_PONG) {
46 return DiskWriterBackEndResponse::pong();
47 } else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
48 auto progress = nanomsg.receive(500);
49 return DiskWriterBackEndResponse::format_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
50 } else if (*s == DISK_WRITER_COPY_PROGRESS) {
51 auto progress = nanomsg.receive(500);
52 return DiskWriterBackEndResponse::copy_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
53 } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
54 auto progress = nanomsg.receive(500);
55 return DiskWriterBackEndResponse::verify_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
57 DCPOMATIC_ASSERT(false);
64 /** @return true if the message was sent, false if there was a timeout */
66 DiskWriterBackEndResponse::write_to_nanomsg(Nanomsg& nanomsg, int timeout) const
73 message = String::compose("%1\n", DISK_WRITER_OK);
76 message = String::compose("%1\n%2\n%3\n", DISK_WRITER_ERROR, _error_message, _error_number);
79 message = String::compose("%1\n", DISK_WRITER_PONG);
81 case Type::FORMAT_PROGRESS:
82 message = String::compose("%1\n", DISK_WRITER_FORMAT_PROGRESS);
83 message += dcp::raw_convert<string>(_progress) + "\n";
85 case Type::COPY_PROGRESS:
86 message = String::compose("%1\n", DISK_WRITER_COPY_PROGRESS);
87 message += dcp::raw_convert<string>(_progress) + "\n";
89 case Type::VERIFY_PROGRESS:
90 message = String::compose("%1\n", DISK_WRITER_VERIFY_PROGRESS);
91 message += dcp::raw_convert<string>(_progress) + "\n";
96 return nanomsg.send(message, timeout);