X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Fjson_server.cc;h=6e53f9819c3fcddd2bc1c21324705037556ef5ef;hp=1be3c7d0e9d894698534d84d6df4aaeaea265c30;hb=5a5324ed3a381a86dfe0a6e3932c1d58fdcd596f;hpb=05654d0e1799746a9df3ccab040c92e0ed825cac diff --git a/src/lib/json_server.cc b/src/lib/json_server.cc index 1be3c7d0e..6e53f9819 100644 --- a/src/lib/json_server.cc +++ b/src/lib/json_server.cc @@ -1,19 +1,20 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2015 Carl Hetherington - This program is free software; you can redistribute it and/or modify + 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. - This program is distributed in the hope that it will be useful, + 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 this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ @@ -26,14 +27,16 @@ #include "util.h" #include "film.h" #include "transcode_job.h" +#include +#include using std::string; -using std::stringstream; using std::cout; using std::map; using std::list; using boost::thread; using boost::shared_ptr; +using boost::make_shared; using boost::dynamic_pointer_cast; using boost::asio::ip::tcp; @@ -60,7 +63,7 @@ try tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port)); while (true) { try { - shared_ptr s (new tcp::socket (io_service)); + shared_ptr s = make_shared (io_service); a.accept (*s); handle (s); } @@ -139,22 +142,22 @@ void JSONServer::request (string url, shared_ptr socket) { cout << "request: " << url << "\n"; - + map r = split_get_request (url); for (map::iterator i = r.begin(); i != r.end(); ++i) { cout << i->first << " => " << i->second << "\n"; } - + string action; if (r.find ("action") != r.end ()) { action = r["action"]; } - - stringstream json; + + SafeStringStream json; if (action == "status") { - + list > jobs = JobManager::instance()->get (); - + json << "{ \"jobs\": ["; for (list >::iterator i = jobs.begin(); i != jobs.end(); ++i) { @@ -163,12 +166,16 @@ JSONServer::request (string url, shared_ptr socket) if ((*i)->film()) { json << "\"dcp\": \"" << (*i)->film()->dcp_name() << "\", "; } - - json << "\"name\": \"" << (*i)->json_name() << "\", " - << "\"progress\": " << (*i)->progress () << ", " - << "\"status\": \"" << (*i)->json_status() << "\""; + + json << "\"name\": \"" << (*i)->json_name() << "\", "; + if ((*i)->progress ()) { + json << "\"progress\": " << (*i)->progress().get() << ", "; + } else { + json << "\"progress\": unknown, "; + } + json << "\"status\": \"" << (*i)->json_status() << "\""; json << " }"; - + list >::iterator j = i; ++j; if (j != jobs.end ()) { @@ -176,13 +183,13 @@ JSONServer::request (string url, shared_ptr socket) } } json << "] }"; - + if (json.str().empty ()) { json << "{ }"; } } - - stringstream reply; + + SafeStringStream reply; reply << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << json.str().length() << "\r\n" << "Content-Type: application/json\r\n"