Make terminate_threads() less likely to leave _threads containing invalid pointers.
[dcpomatic.git] / src / lib / json_server.cc
index 0006201e66c3b6a04bb52d01c4fa222ad55d3a37..3f43a75eb8ee332a53d0a1bac1cb0e2ecc6e1dd0 100644 (file)
 
 */
 
-#include <boost/asio.hpp>
-#include <boost/bind.hpp>
-#include <boost/thread.hpp>
 #include "json_server.h"
 #include "job_manager.h"
 #include "job.h"
 #include "util.h"
 #include "film.h"
 #include "transcode_job.h"
+#include <dcp/raw_convert.h>
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/thread.hpp>
 #include <iostream>
 
 using std::string;
@@ -37,6 +38,7 @@ using boost::thread;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
 using boost::asio::ip::tcp;
+using dcp::raw_convert;
 
 #define MAX_LENGTH 512
 
@@ -50,7 +52,10 @@ enum State {
 
 JSONServer::JSONServer (int port)
 {
-       new thread (boost::bind (&JSONServer::run, this, port));
+       thread* t = new thread (boost::bind (&JSONServer::run, this, port));
+#ifdef DCPOMATIC_LINUX
+       pthread_setname_np (t->native_handle(), "json-server");
+#endif
 }
 
 void
@@ -151,48 +156,43 @@ JSONServer::request (string url, shared_ptr<tcp::socket> socket)
                action = r["action"];
        }
 
-       SafeStringStream json;
+       string json;
        if (action == "status") {
 
                list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
 
-               json << "{ \"jobs\": [";
+               json += "{ \"jobs\": [";
                for (list<shared_ptr<Job> >::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() << "\", ";
+                       json += "\"name\": \"" + (*i)->json_name() + "\", ";
                        if ((*i)->progress ()) {
-                               json << "\"progress\": " << (*i)->progress().get() << ", ";
+                               json += "\"progress\": " + raw_convert<string>((*i)->progress().get()) + ", ";
                        } else {
-                               json << "\"progress\": unknown, ";
+                               json += "\"progress\": unknown, ";
                        }
-                       json << "\"status\": \"" << (*i)->json_status() << "\"";
-                       json << " }";
+                       json += "\"status\": \"" + (*i)->json_status() + "\"";
+                       json += " }";
 
                        list<shared_ptr<Job> >::iterator 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<string>(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()));
 }