ff6a0b394196f620dcc0b400d52645205646ca9d
[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
62 // the drive is being formatted, 40% done
63 #define DISK_WRITER_FORMAT_PROGRESS "F"
64 // 0.4\n
65
66 // data is being copied, 30% done
67 #define DISK_WRITER_COPY_PROGRESS "C"
68 // 0.3\n
69
70 // data is being verified, 60% done
71 #define DISK_WRITER_VERIFY_PROGRESS "V"
72 // 0.6\n
73
74
75 /* REQUEST TO QUIT */
76
77 // Front-end sends:
78 #define DISK_WRITER_QUIT "Q"
79
80
81 /* REQUEST TO UNMOUNT A DRIVE */
82
83 // Front-end sends:
84 #define DISK_WRITER_UNMOUNT "U"
85 // XML representation of Drive object to unmount
86
87 // Back-end responds:
88 // DISK_WRITER_OK
89 // or
90 // DISK_WRITER_ERROR
91
92
93 class DiskWriterBackEndResponse
94 {
95 public:
96         enum class Type {
97                 OK,
98                 ERROR,
99                 PONG,
100                 FORMAT_PROGRESS,
101                 COPY_PROGRESS,
102                 VERIFY_PROGRESS
103         };
104
105         static DiskWriterBackEndResponse ok() {
106                 return DiskWriterBackEndResponse(Type::OK);
107         }
108
109         static DiskWriterBackEndResponse error(std::string message, int number) {
110                 auto r = DiskWriterBackEndResponse(Type::ERROR);
111                 r._error_message = message;
112                 r._error_number = number;
113                 return r;
114         }
115
116         static DiskWriterBackEndResponse pong() {
117                 return DiskWriterBackEndResponse(Type::PONG);
118         }
119
120         static DiskWriterBackEndResponse format_progress(float p) {
121                 auto r = DiskWriterBackEndResponse(Type::FORMAT_PROGRESS);
122                 r._progress = p;
123                 return r;
124         }
125
126         static DiskWriterBackEndResponse copy_progress(float p) {
127                 auto r = DiskWriterBackEndResponse(Type::COPY_PROGRESS);
128                 r._progress = p;
129                 return r;
130         }
131
132         static DiskWriterBackEndResponse verify_progress(float p) {
133                 auto r = DiskWriterBackEndResponse(Type::VERIFY_PROGRESS);
134                 r._progress = p;
135                 return r;
136         }
137
138         static boost::optional<DiskWriterBackEndResponse> read_from_nanomsg(Nanomsg& nanomsg, int timeout);
139
140         Type type() const {
141                 return _type;
142         }
143
144         std::string error_message() const {
145                 return _error_message;
146         }
147
148         int error_number() const {
149                 return _error_number;
150         }
151
152         float progress() const {
153                 return _progress;
154         }
155
156 private:
157         DiskWriterBackEndResponse(Type type)
158                 : _type(type)
159         {}
160
161         Type _type;
162         std::string _error_message;
163         int _error_number = 0;
164         float _progress = 0;
165 };
166