X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Fjson_server.cc;h=9c1034a68bf6f26e41df3a15e0d040ead6044f4a;hp=1be3c7d0e9d894698534d84d6df4aaeaea265c30;hb=dd9be86db6cde0afa5da0d1d1ac43b42e05dca26;hpb=6bc83f72f12c8513a1e9e9b6fd880697a73f968f diff --git a/src/lib/json_server.cc b/src/lib/json_server.cc index 1be3c7d0e..9c1034a68 100644 --- a/src/lib/json_server.cc +++ b/src/lib/json_server.cc @@ -1,41 +1,44 @@ /* - 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 . */ -#include -#include -#include #include "json_server.h" #include "job_manager.h" #include "job.h" #include "util.h" #include "film.h" #include "transcode_job.h" +#include +#include +#include +#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::dynamic_pointer_cast; +using std::shared_ptr; +using std::dynamic_pointer_cast; using boost::asio::ip::tcp; +using dcp::raw_convert; #define MAX_LENGTH 512 @@ -49,7 +52,12 @@ enum State { JSONServer::JSONServer (int port) { +#ifdef DCPOMATIC_LINUX + thread* t = new thread (boost::bind (&JSONServer::run, this, port)); + pthread_setname_np (t->native_handle(), "json-server"); +#else new thread (boost::bind (&JSONServer::run, this, port)); +#endif } void @@ -139,55 +147,54 @@ 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; + + string json; if (action == "status") { - + list > jobs = JobManager::instance()->get (); - - json << "{ \"jobs\": ["; + + json += "{ \"jobs\": ["; for (list >::iterator i = jobs.begin(); i != jobs.end(); ++i) { - json << "{ "; + json += "{ "; if ((*i)->film()) { - json << "\"dcp\": \"" << (*i)->film()->dcp_name() << "\", "; + json += "\"dcp\": \"" + (*i)->film()->dcp_name() + "\", "; + } + + json += "\"name\": \"" + (*i)->json_name() + "\", "; + if ((*i)->progress ()) { + json += "\"progress\": " + raw_convert((*i)->progress().get()) + ", "; + } else { + json += "\"progress\": unknown, "; } - - json << "\"name\": \"" << (*i)->json_name() << "\", " - << "\"progress\": " << (*i)->progress () << ", " - << "\"status\": \"" << (*i)->json_status() << "\""; - json << " }"; - + json += "\"status\": \"" + (*i)->json_status() + "\""; + json += " }"; + list >::iterator j = i; ++j; if (j != jobs.end ()) { - json << ", "; + json += ", "; } } - json << "] }"; - - if (json.str().empty ()) { - json << "{ }"; - } + json += "] }"; } - - stringstream reply; - reply << "HTTP/1.1 200 OK\r\n" - << "Content-Length: " << json.str().length() << "\r\n" - << "Content-Type: application/json\r\n" - << "\r\n" - << json.str () << "\r\n"; - cout << "reply: " << json.str() << "\n"; - boost::asio::write (*socket, boost::asio::buffer (reply.str().c_str(), reply.str().length())); + + string reply = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + raw_convert(json.length()) + "\r\n" + "Content-Type: application/json\r\n" + "\r\n" + + json + "\r\n"; + cout << "reply: " << json << "\n"; + boost::asio::write (*socket, boost::asio::buffer (reply.c_str(), reply.length())); }