e42e80ec628c27c463a7d6dc8bf63f4027c9e4d2
[dcpomatic.git] / src / lib / disk_writer_messages.cc
1 /*
2     Copyright (C) 2023 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
22 #include "dcpomatic_assert.h"
23 #include "disk_writer_messages.h"
24 #include "nanomsg.h"
25
26
27 using boost::optional;
28
29
30 boost::optional<DiskWriterBackEndResponse>
31 DiskWriterBackEndResponse::read_from_nanomsg(Nanomsg& nanomsg, int timeout)
32 {
33         auto s = nanomsg.receive(timeout);
34         if (!s) {
35                 return {};
36         }
37         if (*s == DISK_WRITER_OK) {
38                 return DiskWriterBackEndResponse::ok();
39         } else if (*s == DISK_WRITER_ERROR) {
40                 auto const m = nanomsg.receive(500);
41                 auto const n = nanomsg.receive(500);
42                 return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::locale_convert<int>(n.get_value_or("0")));
43         } else if (*s == DISK_WRITER_PONG) {
44                 return DiskWriterBackEndResponse::pong();
45         } else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
46                 auto progress = nanomsg.receive(500);
47                 return DiskWriterBackEndResponse::format_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
48         } else if (*s == DISK_WRITER_COPY_PROGRESS) {
49                 auto progress = nanomsg.receive(500);
50                 return DiskWriterBackEndResponse::copy_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
51         } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
52                 auto progress = nanomsg.receive(500);
53                 return DiskWriterBackEndResponse::verify_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
54         } else {
55                 DCPOMATIC_ASSERT(false);
56         }
57
58         return {};
59 }
60