X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjson_server.cc;h=8c626ad1f0e2186d48d205f050dfececbf6de0bc;hb=ff639b3cf30afcc097bfd21d39c8d15f466cadd6;hp=0006201e66c3b6a04bb52d01c4fa222ad55d3a37;hpb=a8a0dfd1b21de6c0facf965ab119833ff6f790bf;p=dcpomatic.git diff --git a/src/lib/json_server.cc b/src/lib/json_server.cc index 0006201e6..8c626ad1f 100644 --- a/src/lib/json_server.cc +++ b/src/lib/json_server.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014-2015 Carl Hetherington + Copyright (C) 2014-2021 Carl Hetherington This file is part of DCP-o-matic. @@ -18,28 +18,35 @@ */ -#include -#include -#include -#include "json_server.h" -#include "job_manager.h" -#include "job.h" -#include "util.h" + #include "film.h" +#include "job.h" +#include "job_manager.h" +#include "json_server.h" #include "transcode_job.h" +#include "util.h" +#include +#include +#include +#include #include -using std::string; + using std::cout; -using std::map; +using std::dynamic_pointer_cast; using std::list; -using boost::thread; -using boost::shared_ptr; -using boost::dynamic_pointer_cast; +using std::make_shared; +using std::map; +using std::shared_ptr; +using std::string; using boost::asio::ip::tcp; +using boost::thread; +using dcp::raw_convert; + #define MAX_LENGTH 512 + enum State { AWAITING_G, AWAITING_E, @@ -48,11 +55,18 @@ enum State { READING_URL, }; + JSONServer::JSONServer (int port) { +#ifdef DCPOMATIC_LINUX + auto 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 JSONServer::run (int port) try @@ -61,7 +75,7 @@ try tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port)); while (true) { try { - shared_ptr s (new tcp::socket (io_service)); + auto s = make_shared(io_service); a.accept (*s); handle (s); } @@ -75,6 +89,7 @@ catch (...) } + void JSONServer::handle (shared_ptr socket) { @@ -90,11 +105,11 @@ JSONServer::handle (shared_ptr socket) break; } - char* p = data; - char* e = data + len; + auto p = data; + auto e = data + len; while (p != e) { - State old_state = state; + auto old_state = state; switch (state) { case AWAITING_G: if (*p == 'G') { @@ -136,14 +151,15 @@ JSONServer::handle (shared_ptr socket) } } + 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"; + auto r = split_get_request (url); + for (auto const& i: r) { + cout << i.first << " => " << i.second << "\n"; } string action; @@ -151,48 +167,42 @@ JSONServer::request (string url, shared_ptr socket) action = r["action"]; } - SafeStringStream json; + string json; if (action == "status") { - list > jobs = JobManager::instance()->get (); + auto jobs = JobManager::instance()->get(); - json << "{ \"jobs\": ["; - for (list >::iterator i = jobs.begin(); i != jobs.end(); ++i) { - - json << "{ "; + json += "{ \"jobs\": ["; + for (auto i = jobs.cbegin(); i != jobs.cend(); ++i) { + 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\": " << (*i)->progress().get() << ", "; + json += "\"name\": \"" + (*i)->json_name() + "\", "; + if ((*i)->progress()) { + json += "\"progress\": " + raw_convert((*i)->progress().get()) + ", "; } else { - json << "\"progress\": unknown, "; + json += "\"progress\": unknown, "; } - json << "\"status\": \"" << (*i)->json_status() << "\""; - json << " }"; + json += "\"status\": \"" + (*i)->json_status() + "\""; + json += " }"; - list >::iterator j = i; + auto j = i; ++j; if (j != jobs.end ()) { - json << ", "; + json += ", "; } } - json << "] }"; - - if (json.str().empty ()) { - json << "{ }"; - } + json += "] }"; } - SafeStringStream 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())); }