Supporters update.
[dcpomatic.git] / src / lib / disk_writer_messages.h
1 /*
2     Copyright (C) 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
22 #include <boost/optional.hpp>
23 #include <string>
24
25
26 class Nanomsg;
27
28
29 /* We have the front-end application dcpomatic2_disk and the back-end
30  * dcpomatic2_disk_writer.  The communication is line-based, separated
31  * by \n.
32  */
33
34 /* PING */
35
36 // Front-end sends:
37
38 #define DISK_WRITER_PING "P"
39
40 // Back-end responds
41
42 #define DISK_WRITER_PONG "O"
43
44 /* REQUEST TO WRITE DCP */
45
46 // Front-end sends:
47
48 #define DISK_WRITER_WRITE "W"
49 // DCP pathname
50 // Internal name of the drive to write to
51
52 // Back-end responds:
53
54 // everything is ok
55 #define DISK_WRITER_OK "D"
56
57 // there was an error
58 #define DISK_WRITER_ERROR "E"
59 // Error message
60 // Error number
61 // Additional error number (a platform-specific error from lwext4)
62
63 // the drive is being formatted, 40% done
64 #define DISK_WRITER_FORMAT_PROGRESS "F"
65 // 0.4\n
66
67 // data is being copied, 30% done
68 #define DISK_WRITER_COPY_PROGRESS "C"
69 // 0.3\n
70
71 // data is being verified, 60% done
72 #define DISK_WRITER_VERIFY_PROGRESS "V"
73 // 0.6\n
74
75
76 /* REQUEST TO QUIT */
77
78 // Front-end sends:
79 #define DISK_WRITER_QUIT "Q"
80
81
82 /* REQUEST TO UNMOUNT A DRIVE */
83
84 // Front-end sends:
85 #define DISK_WRITER_UNMOUNT "U"
86 // XML representation of Drive object to unmount
87
88 // Back-end responds:
89 // DISK_WRITER_OK
90 // or
91 // DISK_WRITER_ERROR
92
93
94 class DiskWriterBackEndResponse
95 {
96 public:
97         enum class Type {
98                 OK,
99                 ERROR,
100                 PONG,
101                 FORMAT_PROGRESS,
102                 COPY_PROGRESS,
103                 VERIFY_PROGRESS
104         };
105
106         static DiskWriterBackEndResponse ok() {
107                 return DiskWriterBackEndResponse(Type::OK);
108         }
109
110         static DiskWriterBackEndResponse error(std::string message, int ext4_number, int platform_number) {
111                 auto r = DiskWriterBackEndResponse(Type::ERROR);
112                 r._error_message = message;
113                 r._ext4_error_number = ext4_number;
114                 r._platform_error_number = platform_number;
115                 return r;
116         }
117
118         static DiskWriterBackEndResponse pong() {
119                 return DiskWriterBackEndResponse(Type::PONG);
120         }
121
122         static DiskWriterBackEndResponse format_progress(float p) {
123                 auto r = DiskWriterBackEndResponse(Type::FORMAT_PROGRESS);
124                 r._progress = p;
125                 return r;
126         }
127
128         static DiskWriterBackEndResponse copy_progress(float p) {
129                 auto r = DiskWriterBackEndResponse(Type::COPY_PROGRESS);
130                 r._progress = p;
131                 return r;
132         }
133
134         static DiskWriterBackEndResponse verify_progress(float p) {
135                 auto r = DiskWriterBackEndResponse(Type::VERIFY_PROGRESS);
136                 r._progress = p;
137                 return r;
138         }
139
140         static boost::optional<DiskWriterBackEndResponse> read_from_nanomsg(Nanomsg& nanomsg, int timeout);
141
142         bool write_to_nanomsg(Nanomsg& nanomsg, int timeout) const;
143
144         Type type() const {
145                 return _type;
146         }
147
148         std::string error_message() const {
149                 return _error_message;
150         }
151
152         int ext4_error_number() const {
153                 return _ext4_error_number;
154         }
155
156         int platform_error_number() const {
157                 return _platform_error_number;
158         }
159
160         float progress() const {
161                 return _progress;
162         }
163
164 private:
165         DiskWriterBackEndResponse(Type type)
166                 : _type(type)
167         {}
168
169         Type _type;
170         std::string _error_message;
171         int _ext4_error_number = 0;
172         int _platform_error_number = 0;
173         float _progress = 0;
174 };
175