2 Copyright (C) 2014-2015 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/>.
21 #include <boost/asio.hpp>
22 #include <boost/bind.hpp>
23 #include <boost/thread.hpp>
24 #include "json_server.h"
25 #include "job_manager.h"
29 #include "transcode_job.h"
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using boost::asio::ip::tcp;
41 #define MAX_LENGTH 512
51 JSONServer::JSONServer (int port)
53 new thread (boost::bind (&JSONServer::run, this, port));
57 JSONServer::run (int port)
60 boost::asio::io_service io_service;
61 tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port));
64 shared_ptr<tcp::socket> s (new tcp::socket (io_service));
79 JSONServer::handle (shared_ptr<tcp::socket> socket)
82 State state = AWAITING_G;
85 char data[MAX_LENGTH];
86 boost::system::error_code error;
87 size_t len = socket->read_some (boost::asio::buffer (data), error);
97 State old_state = state;
111 state = AWAITING_SPACE;
121 request (url, socket);
130 if (state == old_state && state != READING_URL) {
140 JSONServer::request (string url, shared_ptr<tcp::socket> socket)
142 cout << "request: " << url << "\n";
144 map<string, string> r = split_get_request (url);
145 for (map<string, string>::iterator i = r.begin(); i != r.end(); ++i) {
146 cout << i->first << " => " << i->second << "\n";
150 if (r.find ("action") != r.end ()) {
151 action = r["action"];
154 SafeStringStream json;
155 if (action == "status") {
157 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
159 json << "{ \"jobs\": [";
160 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
165 json << "\"dcp\": \"" << (*i)->film()->dcp_name() << "\", ";
168 json << "\"name\": \"" << (*i)->json_name() << "\", ";
169 if ((*i)->progress ()) {
170 json << "\"progress\": " << (*i)->progress().get() << ", ";
172 json << "\"progress\": unknown, ";
174 json << "\"status\": \"" << (*i)->json_status() << "\"";
177 list<shared_ptr<Job> >::iterator j = i;
179 if (j != jobs.end ()) {
185 if (json.str().empty ()) {
190 SafeStringStream reply;
191 reply << "HTTP/1.1 200 OK\r\n"
192 << "Content-Length: " << json.str().length() << "\r\n"
193 << "Content-Type: application/json\r\n"
195 << json.str () << "\r\n";
196 cout << "reply: " << json.str() << "\n";
197 boost::asio::write (*socket, boost::asio::buffer (reply.str().c_str(), reply.str().length()));