X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Fjson_server.cc;h=3f43a75eb8ee332a53d0a1bac1cb0e2ecc6e1dd0;hp=f4aa292d7779ca8cee77af2855b489242738d6fe;hb=HEAD;hpb=8e0ab80930d39a005f8dc7692f871e7fbe5e3723 diff --git a/src/lib/json_server.cc b/src/lib/json_server.cc index f4aa292d7..92d5c1171 100644 --- a/src/lib/json_server.cc +++ b/src/lib/json_server.cc @@ -1,44 +1,51 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2021 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 "job.h" +#include "job_manager.h" +#include "json_server.h" #include "transcode_job.h" +#include +#include +#include +#include +#include + -using std::string; -using std::stringstream; 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, @@ -47,20 +54,27 @@ 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 { boost::asio::io_service io_service; tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port)); - while (1) { + while (true) { try { - shared_ptr s (new tcp::socket (io_service)); + auto s = make_shared(io_service); a.accept (*s); handle (s); } @@ -74,13 +88,14 @@ catch (...) } + void JSONServer::handle (shared_ptr socket) { string url; State state = AWAITING_G; - while (1) { + while (true) { char data[MAX_LENGTH]; boost::system::error_code error; size_t len = socket->read_some (boost::asio::buffer (data), error); @@ -89,11 +104,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') { @@ -135,59 +150,105 @@ JSONServer::handle (shared_ptr socket) } } + +map +split_get_request(string url) +{ + enum { + AWAITING_QUESTION_MARK, + KEY, + VALUE + } state = AWAITING_QUESTION_MARK; + + map r; + string k; + string v; + for (size_t i = 0; i < url.length(); ++i) { + switch (state) { + case AWAITING_QUESTION_MARK: + if (url[i] == '?') { + state = KEY; + } + break; + case KEY: + if (url[i] == '=') { + v.clear(); + state = VALUE; + } else { + k += url[i]; + } + break; + case VALUE: + if (url[i] == '&') { + r.insert(make_pair(k, v)); + k.clear (); + state = KEY; + } else { + v += url[i]; + } + break; + } + } + + if (state == VALUE) { + r.insert (make_pair (k, v)); + } + + return r; +} + + 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; if (r.find ("action") != r.end ()) { action = r["action"]; } - - stringstream json; + + string json; if (action == "status") { - - list > jobs = JobManager::instance()->get (); - - json << "{ \"jobs\": ["; - for (list >::iterator i = jobs.begin(); i != jobs.end(); ++i) { - json << "{ "; + auto jobs = JobManager::instance()->get(); + + 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\": " + raw_convert((*i)->progress().get()) + ", "; + } else { + json += "\"progress\": unknown, "; } - - json << "\"name\": \"" << (*i)->json_name() << "\", " - << "\"progress\": " << (*i)->progress () << ", " - << "\"status\": \"" << (*i)->json_status() << "\""; - json << " }"; - - list >::iterator j = i; + json += "\"status\": \"" + (*i)->json_status() + "\""; + json += " }"; + + auto 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())); }