2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
24 #include "job_manager.h"
25 #include "json_server.h"
26 #include "transcode_job.h"
27 #include <dcp/raw_convert.h>
28 #include <boost/asio.hpp>
29 #include <boost/bind/bind.hpp>
30 #include <boost/thread.hpp>
35 using std::dynamic_pointer_cast;
37 using std::make_shared;
39 using std::shared_ptr;
41 using boost::asio::ip::tcp;
43 using dcp::raw_convert;
46 #define MAX_LENGTH 512
58 JSONServer::JSONServer (int port)
60 #ifdef DCPOMATIC_LINUX
61 auto t = new thread (boost::bind (&JSONServer::run, this, port));
62 pthread_setname_np (t->native_handle(), "json-server");
64 new thread (boost::bind (&JSONServer::run, this, port));
70 JSONServer::run (int port)
73 boost::asio::io_service io_service;
74 tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port));
77 auto s = make_shared<tcp::socket>(io_service);
93 JSONServer::handle (shared_ptr<tcp::socket> socket)
96 State state = AWAITING_G;
99 char data[MAX_LENGTH];
100 boost::system::error_code error;
101 size_t len = socket->read_some (boost::asio::buffer (data), error);
111 auto old_state = state;
125 state = AWAITING_SPACE;
135 request (url, socket);
144 if (state == old_state && state != READING_URL) {
155 split_get_request(string url)
158 AWAITING_QUESTION_MARK,
161 } state = AWAITING_QUESTION_MARK;
163 map<string, string> r;
166 for (size_t i = 0; i < url.length(); ++i) {
168 case AWAITING_QUESTION_MARK:
183 r.insert(make_pair(k, v));
193 if (state == VALUE) {
194 r.insert (make_pair (k, v));
202 JSONServer::request (string url, shared_ptr<tcp::socket> socket)
204 cout << "request: " << url << "\n";
206 auto r = split_get_request (url);
207 for (auto const& i: r) {
208 cout << i.first << " => " << i.second << "\n";
212 if (r.find ("action") != r.end ()) {
213 action = r["action"];
217 if (action == "status") {
219 auto jobs = JobManager::instance()->get();
221 json += "{ \"jobs\": [";
222 for (auto i = jobs.cbegin(); i != jobs.cend(); ++i) {
226 json += "\"dcp\": \"" + (*i)->film()->dcp_name() + "\", ";
229 json += "\"name\": \"" + (*i)->json_name() + "\", ";
230 if ((*i)->progress()) {
231 json += "\"progress\": " + raw_convert<string>((*i)->progress().get()) + ", ";
233 json += "\"progress\": unknown, ";
235 json += "\"status\": \"" + (*i)->json_status() + "\"";
240 if (j != jobs.end ()) {
247 string reply = "HTTP/1.1 200 OK\r\n"
248 "Content-Length: " + raw_convert<string>(json.length()) + "\r\n"
249 "Content-Type: application/json\r\n"
252 cout << "reply: " << json << "\n";
253 boost::asio::write (*socket, boost::asio::buffer(reply.c_str(), reply.length()));