211d0c846e0a0167abc1f4b62514564b1ebc49ba
[dcpomatic.git] / src / lib / json_server.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "json_server.h"
22 #include "job_manager.h"
23 #include "job.h"
24 #include "util.h"
25 #include "film.h"
26 #include "transcode_job.h"
27 #include <dcp/raw_convert.h>
28 #include <boost/asio.hpp>
29 #include <boost/bind/bind.hpp>
30 #include <boost/thread.hpp>
31 #include <iostream>
32
33 using std::string;
34 using std::cout;
35 using std::map;
36 using std::list;
37 using boost::thread;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::asio::ip::tcp;
41 using dcp::raw_convert;
42
43 #define MAX_LENGTH 512
44
45 enum State {
46         AWAITING_G,
47         AWAITING_E,
48         AWAITING_T,
49         AWAITING_SPACE,
50         READING_URL,
51 };
52
53 JSONServer::JSONServer (int port)
54 {
55 #ifdef DCPOMATIC_LINUX
56         thread* t = new thread (boost::bind (&JSONServer::run, this, port));
57         pthread_setname_np (t->native_handle(), "json-server");
58 #else
59         new thread (boost::bind (&JSONServer::run, this, port));
60 #endif
61 }
62
63 void
64 JSONServer::run (int port)
65 try
66 {
67         boost::asio::io_service io_service;
68         tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port));
69         while (true) {
70                 try {
71                         shared_ptr<tcp::socket> s (new tcp::socket (io_service));
72                         a.accept (*s);
73                         handle (s);
74                 }
75                 catch (...) {
76
77                 }
78         }
79 }
80 catch (...)
81 {
82
83 }
84
85 void
86 JSONServer::handle (shared_ptr<tcp::socket> socket)
87 {
88         string url;
89         State state = AWAITING_G;
90
91         while (true) {
92                 char data[MAX_LENGTH];
93                 boost::system::error_code error;
94                 size_t len = socket->read_some (boost::asio::buffer (data), error);
95                 if (error) {
96                         cout << "error.\n";
97                         break;
98                 }
99
100                 char* p = data;
101                 char* e = data + len;
102                 while (p != e) {
103
104                         State old_state = state;
105                         switch (state) {
106                         case AWAITING_G:
107                                 if (*p == 'G') {
108                                         state = AWAITING_E;
109                                 }
110                                 break;
111                         case AWAITING_E:
112                                 if (*p == 'E') {
113                                         state = AWAITING_T;
114                                 }
115                                 break;
116                         case AWAITING_T:
117                                 if (*p == 'T') {
118                                         state = AWAITING_SPACE;
119                                 }
120                                 break;
121                         case AWAITING_SPACE:
122                                 if (*p == ' ') {
123                                         state = READING_URL;
124                                 }
125                                 break;
126                         case READING_URL:
127                                 if (*p == ' ') {
128                                         request (url, socket);
129                                         state = AWAITING_G;
130                                         url = "";
131                                 } else {
132                                         url += *p;
133                                 }
134                                 break;
135                         }
136
137                         if (state == old_state && state != READING_URL) {
138                                 state = AWAITING_G;
139                         }
140
141                         ++p;
142                 }
143         }
144 }
145
146 void
147 JSONServer::request (string url, shared_ptr<tcp::socket> socket)
148 {
149         cout << "request: " << url << "\n";
150
151         map<string, string> r = split_get_request (url);
152         for (map<string, string>::iterator i = r.begin(); i != r.end(); ++i) {
153                 cout << i->first << " => " << i->second << "\n";
154         }
155
156         string action;
157         if (r.find ("action") != r.end ()) {
158                 action = r["action"];
159         }
160
161         string json;
162         if (action == "status") {
163
164                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
165
166                 json += "{ \"jobs\": [";
167                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
168
169                         json += "{ ";
170
171                         if ((*i)->film()) {
172                                 json += "\"dcp\": \"" + (*i)->film()->dcp_name() + "\", ";
173                         }
174
175                         json += "\"name\": \"" + (*i)->json_name() + "\", ";
176                         if ((*i)->progress ()) {
177                                 json += "\"progress\": " + raw_convert<string>((*i)->progress().get()) + ", ";
178                         } else {
179                                 json += "\"progress\": unknown, ";
180                         }
181                         json += "\"status\": \"" + (*i)->json_status() + "\"";
182                         json += " }";
183
184                         list<shared_ptr<Job> >::iterator j = i;
185                         ++j;
186                         if (j != jobs.end ()) {
187                                 json += ", ";
188                         }
189                 }
190                 json += "] }";
191         }
192
193         string reply = "HTTP/1.1 200 OK\r\n"
194                 "Content-Length: " + raw_convert<string>(json.length()) + "\r\n"
195                 "Content-Type: application/json\r\n"
196                 "\r\n"
197                 + json + "\r\n";
198         cout << "reply: " << json << "\n";
199         boost::asio::write (*socket, boost::asio::buffer (reply.c_str(), reply.length()));
200 }