X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fjson_server.cc;h=dd56b3124fea46550a2921ea96e813dd4a71f213;hb=3339d3bce70afe9ae2ca10e9fcfc4b54b748fbf4;hp=9c1034a68bf6f26e41df3a15e0d040ead6044f4a;hpb=00762c2d9a4240d016150cd7555aee3dad8542ae;p=dcpomatic.git diff --git a/src/lib/json_server.cc b/src/lib/json_server.cc index 9c1034a68..dd56b3124 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,6 +18,7 @@ */ + #include "json_server.h" #include "job_manager.h" #include "job.h" @@ -30,6 +31,7 @@ #include #include + using std::string; using std::cout; using std::map; @@ -40,8 +42,10 @@ using std::dynamic_pointer_cast; using boost::asio::ip::tcp; using dcp::raw_convert; + #define MAX_LENGTH 512 + enum State { AWAITING_G, AWAITING_E, @@ -50,16 +54,18 @@ enum State { READING_URL, }; + JSONServer::JSONServer (int port) { #ifdef DCPOMATIC_LINUX - thread* t = new thread (boost::bind (&JSONServer::run, this, port)); + 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 @@ -82,6 +88,7 @@ catch (...) } + void JSONServer::handle (shared_ptr socket) { @@ -97,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') { @@ -143,14 +150,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; @@ -161,11 +169,10 @@ JSONServer::request (string url, shared_ptr socket) 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) { - + for (auto i = jobs.cbegin(); i != jobs.cend(); ++i) { json += "{ "; if ((*i)->film()) { @@ -173,7 +180,7 @@ JSONServer::request (string url, shared_ptr socket) } json += "\"name\": \"" + (*i)->json_name() + "\", "; - if ((*i)->progress ()) { + if ((*i)->progress()) { json += "\"progress\": " + raw_convert((*i)->progress().get()) + ", "; } else { json += "\"progress\": unknown, "; @@ -181,7 +188,7 @@ JSONServer::request (string url, shared_ptr socket) json += "\"status\": \"" + (*i)->json_status() + "\""; json += " }"; - list >::iterator j = i; + auto j = i; ++j; if (j != jobs.end ()) { json += ", "; @@ -196,5 +203,5 @@ JSONServer::request (string url, shared_ptr socket) "\r\n" + json + "\r\n"; cout << "reply: " << json << "\n"; - boost::asio::write (*socket, boost::asio::buffer (reply.c_str(), reply.length())); + boost::asio::write (*socket, boost::asio::buffer(reply.c_str(), reply.length())); }