Add DiskWriterBackEndResponse.
authorCarl Hetherington <cth@carlh.net>
Wed, 11 Jan 2023 15:31:56 +0000 (16:31 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 29 Jan 2023 19:58:25 +0000 (20:58 +0100)
src/lib/disk_writer_messages.cc [new file with mode: 0644]
src/lib/disk_writer_messages.h
src/lib/wscript

diff --git a/src/lib/disk_writer_messages.cc b/src/lib/disk_writer_messages.cc
new file mode 100644 (file)
index 0000000..e42e80e
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+    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"
+
+
+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);
+               return DiskWriterBackEndResponse::error(m.get_value_or(""), dcp::locale_convert<int>(n.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::locale_convert<float>(progress.get_value_or("0")));
+       } else if (*s == DISK_WRITER_COPY_PROGRESS) {
+               auto progress = nanomsg.receive(500);
+               return DiskWriterBackEndResponse::copy_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
+       } else if (*s == DISK_WRITER_VERIFY_PROGRESS) {
+               auto progress = nanomsg.receive(500);
+               return DiskWriterBackEndResponse::verify_progress(dcp::locale_convert<float>(progress.get_value_or("0")));
+       } else {
+               DCPOMATIC_ASSERT(false);
+       }
+
+       return {};
+}
+
index ab86f083e57d2d45d47627fae090720731028415..ff6a0b394196f620dcc0b400d52645205646ca9d 100644 (file)
 
 */
 
+
+#include <boost/optional.hpp>
+#include <string>
+
+
+class Nanomsg;
+
+
 /* We have the front-end application dcpomatic2_disk and the back-end
  * dcpomatic2_disk_writer.  The communication is line-based, separated
  * by \n.
 // or
 // DISK_WRITER_ERROR
 
+
+class DiskWriterBackEndResponse
+{
+public:
+       enum class Type {
+               OK,
+               ERROR,
+               PONG,
+               FORMAT_PROGRESS,
+               COPY_PROGRESS,
+               VERIFY_PROGRESS
+       };
+
+       static DiskWriterBackEndResponse ok() {
+               return DiskWriterBackEndResponse(Type::OK);
+       }
+
+       static DiskWriterBackEndResponse error(std::string message, int number) {
+               auto r = DiskWriterBackEndResponse(Type::ERROR);
+               r._error_message = message;
+               r._error_number = number;
+               return r;
+       }
+
+       static DiskWriterBackEndResponse pong() {
+               return DiskWriterBackEndResponse(Type::PONG);
+       }
+
+       static DiskWriterBackEndResponse format_progress(float p) {
+               auto r = DiskWriterBackEndResponse(Type::FORMAT_PROGRESS);
+               r._progress = p;
+               return r;
+       }
+
+       static DiskWriterBackEndResponse copy_progress(float p) {
+               auto r = DiskWriterBackEndResponse(Type::COPY_PROGRESS);
+               r._progress = p;
+               return r;
+       }
+
+       static DiskWriterBackEndResponse verify_progress(float p) {
+               auto r = DiskWriterBackEndResponse(Type::VERIFY_PROGRESS);
+               r._progress = p;
+               return r;
+       }
+
+       static boost::optional<DiskWriterBackEndResponse> read_from_nanomsg(Nanomsg& nanomsg, int timeout);
+
+       Type type() const {
+               return _type;
+       }
+
+       std::string error_message() const {
+               return _error_message;
+       }
+
+       int error_number() const {
+               return _error_number;
+       }
+
+       float progress() const {
+               return _progress;
+       }
+
+private:
+       DiskWriterBackEndResponse(Type type)
+               : _type(type)
+       {}
+
+       Type _type;
+       std::string _error_message;
+       int _error_number = 0;
+       float _progress = 0;
+};
+
index 43ccead77669a74137113402ac2165d456c0e904..ad0bcf0dfca37c8598bca3765889d9c3424a4be2 100644 (file)
@@ -232,7 +232,7 @@ def build(bld):
     obj.source = sources + ' version.cc'
 
     if bld.env.ENABLE_DISK:
-        obj.source += ' copy_to_drive_job.cc ext.cc nanomsg.cc'
+        obj.source += ' copy_to_drive_job.cc disk_writer_messages.cc ext.cc nanomsg.cc'
         obj.uselib += ' LWEXT4 NANOMSG'
         if bld.env.TARGET_LINUX:
             obj.uselib += ' POLKIT'