Add disk writer tool.
[dcpomatic.git] / src / lib / nanomsg.cc
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 #include "nanomsg.h"
22 #include "dcpomatic_log.h"
23 #include <nanomsg/nn.h>
24 #include <nanomsg/pair.h>
25 #include <stdexcept>
26 #include <cerrno>
27
28 using std::string;
29 using std::runtime_error;
30 using boost::optional;
31
32 #define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
33
34 Nanomsg::Nanomsg (bool server)
35 {
36         _socket = nn_socket (AF_SP, NN_PAIR);
37         if (_socket < 0) {
38                 throw runtime_error("Could not set up nanomsg socket");
39         }
40         if (server) {
41                 if (nn_bind(_socket, NANOMSG_URL) < 0) {
42                         throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
43                 }
44         } else {
45                 if (nn_connect(_socket, NANOMSG_URL) < 0) {
46                         throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
47                 }
48         }
49 }
50
51 void
52 Nanomsg::blocking_send (string s)
53 {
54         int const r = nn_send (_socket, s.c_str(), s.length(), 0);
55         if (r < 0) {
56                 throw runtime_error(String::compose("Could not send to nanomsg socket (%1)", errno));
57         } else if (r != int(s.length())) {
58                 throw runtime_error("Could not send to nanomsg socket (message too big)");
59         }
60 }
61
62 bool
63 Nanomsg::nonblocking_send (string s)
64 {
65         int const r = nn_send (_socket, s.c_str(), s.length(), NN_DONTWAIT);
66         if (r < 0) {
67                 if (errno == EAGAIN) {
68                         return false;
69                 }
70                 throw runtime_error(String::compose("Could not send to nanomsg socket (%1)", errno));
71         } else if (r != int(s.length())) {
72                 throw runtime_error("Could not send to nanomsg socket (message too big)");
73         }
74
75         return true;
76 }
77
78 optional<string>
79 Nanomsg::get_from_pending ()
80 {
81         if (_pending.empty()) {
82                 return optional<string>();
83         }
84
85         string const l = _pending.back();
86         _pending.pop_back();
87         return l;
88 }
89
90 void
91 Nanomsg::recv_and_parse (bool blocking)
92 {
93         char* buf = 0;
94         int const received = nn_recv (_socket, &buf, NN_MSG, blocking ? 0 : NN_DONTWAIT);
95         if (received < 0)
96         {
97                 if (!blocking && errno == EAGAIN) {
98                         return;
99                 }
100
101                 throw runtime_error ("Could not communicate with subprocess");
102         }
103
104         char* p = buf;
105         for (int i = 0; i < received; ++i) {
106                 if (*p == '\n') {
107                         _pending.push_front (_current);
108                         _current = "";
109                 } else {
110                         _current += *p;
111                 }
112                 ++p;
113         }
114         nn_freemsg (buf);
115 }
116
117 string
118 Nanomsg::blocking_get ()
119 {
120         optional<string> l = get_from_pending ();
121         if (l) {
122                 return *l;
123         }
124
125         recv_and_parse (true);
126
127         l = get_from_pending ();
128         if (!l) {
129                 throw runtime_error ("Could not communicate with subprocess");
130         }
131
132         return *l;
133 }
134
135 optional<string>
136 Nanomsg::nonblocking_get ()
137 {
138         optional<string> l = get_from_pending ();
139         if (l) {
140                 return *l;
141         }
142
143         recv_and_parse (false);
144         return get_from_pending ();
145 }