summaryrefslogtreecommitdiff
path: root/src/lib/disk_writer_messages.cc
blob: 562e9e876711d9d08c9af9c1733d565fab496b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
    Copyright (C) 2023 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "dcpomatic_assert.h"
#include "disk_writer_messages.h"
#include "nanomsg.h"
#include <dcp/raw_convert.h>
#include <fmt/format.h>


using std::string;
using boost::optional;


boost::optional<DiskWriterBackEndResponse>
DiskWriterBackEndResponse::read_from_nanomsg(Nanomsg& nanomsg, int timeout)
{
	auto s = nanomsg.receive(timeout);
	if (!s) {
		return {};
	}
	if (*s == DISK_WRITER_OK) {
		return DiskWriterBackEndResponse::ok();
	} else if (*s == DISK_WRITER_ERROR) {
		auto const m = nanomsg.receive(500);
		auto const n = nanomsg.receive(500);
		auto const p = nanomsg.receive(500);
		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")));
	} else if (*s == DISK_WRITER_PONG) {
		return DiskWriterBackEndResponse::pong();
	} else if (*s == DISK_WRITER_FORMAT_PROGRESS) {
		auto progress = nanomsg.receive(500);
		return DiskWriterBackEndResponse::format_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
	} else if (*s == DISK_WRITER_COPY_PROGRESS) {
		auto progress = nanomsg.receive(500);
		return DiskWriterBackEndResponse::copy_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
	} else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
		auto progress = nanomsg.receive(500);
		return DiskWriterBackEndResponse::verify_progress(dcp::raw_convert<float>(progress.get_value_or("0")));
	} else {
		DCPOMATIC_ASSERT(false);
	}

	return {};
}


/** @return true if the message was sent, false if there was a timeout */
bool
DiskWriterBackEndResponse::write_to_nanomsg(Nanomsg& nanomsg, int timeout) const
{
	string message;

	switch (_type)
	{
		case Type::OK:
			message = fmt::format("{}\n", DISK_WRITER_OK);
			break;
		case Type::ERROR:
			message = fmt::format("{}\n{}\n{}\n{}\n", DISK_WRITER_ERROR, _error_message, _ext4_error_number, _platform_error_number);
			break;
		case Type::PONG:
			message = fmt::format("{}\n", DISK_WRITER_PONG);
			break;
		case Type::FORMAT_PROGRESS:
			message = fmt::format("{}\n", DISK_WRITER_FORMAT_PROGRESS);
			message += fmt::to_string(_progress) + "\n";
			break;
		case Type::COPY_PROGRESS:
			message = fmt::format("{}\n", DISK_WRITER_COPY_PROGRESS);
			message += fmt::to_string(_progress) + "\n";
			break;
		case Type::VERIFY_PROGRESS:
			message = fmt::format("{}\n", DISK_WRITER_VERIFY_PROGRESS);
			message += fmt::to_string(_progress) + "\n";
			break;
	}


	return nanomsg.send(message, timeout);
}