Missed update to private test repo version.
[dcpomatic.git] / src / lib / json_server.cc
1 /*
2     Copyright (C) 2014-2021 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
22 #include "film.h"
23 #include "job.h"
24 #include "job_manager.h"
25 #include "json_server.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
34 using std::cout;
35 using std::dynamic_pointer_cast;
36 using std::list;
37 using std::make_shared;
38 using std::map;
39 using std::shared_ptr;
40 using std::string;
41 using boost::asio::ip::tcp;
42 using boost::thread;
43 using dcp::raw_convert;
44
45
46 #define MAX_LENGTH 512
47
48
49 enum State {
50         AWAITING_G,
51         AWAITING_E,
52         AWAITING_T,
53         AWAITING_SPACE,
54         READING_URL,
55 };
56
57
58 JSONServer::JSONServer (int port)
59 {
60 #ifdef DCPOMATIC_LINUX
61         auto t = new thread (boost::bind (&JSONServer::run, this, port));
62         pthread_setname_np (t->native_handle(), "json-server");
63 #else
64         new thread (boost::bind (&JSONServer::run, this, port));
65 #endif
66 }
67
68
69 void
70 JSONServer::run (int port)
71 try
72 {
73         boost::asio::io_service io_service;
74         tcp::acceptor a (io_service, tcp::endpoint (tcp::v4 (), port));
75         while (true) {
76                 try {
77                         auto s = make_shared<tcp::socket>(io_service);
78                         a.accept (*s);
79                         handle (s);
80                 }
81                 catch (...) {
82
83                 }
84         }
85 }
86 catch (...)
87 {
88
89 }
90
91
92 void
93 JSONServer::handle (shared_ptr<tcp::socket> socket)
94 {
95         string url;
96         State state = AWAITING_G;
97
98         while (true) {
99                 char data[MAX_LENGTH];
100                 boost::system::error_code error;
101                 size_t len = socket->read_some (boost::asio::buffer (data), error);
102                 if (error) {
103                         cout << "error.\n";
104                         break;
105                 }
106
107                 auto p = data;
108                 auto e = data + len;
109                 while (p != e) {
110
111                         auto old_state = state;
112                         switch (state) {
113                         case AWAITING_G:
114                                 if (*p == 'G') {
115                                         state = AWAITING_E;
116                                 }
117                                 break;
118                         case AWAITING_E:
119                                 if (*p == 'E') {
120                                         state = AWAITING_T;
121                                 }
122                                 break;
123                         case AWAITING_T:
124                                 if (*p == 'T') {
125                                         state = AWAITING_SPACE;
126                                 }
127                                 break;
128                         case AWAITING_SPACE:
129                                 if (*p == ' ') {
130                                         state = READING_URL;
131                                 }
132                                 break;
133                         case READING_URL:
134                                 if (*p == ' ') {
135                                         request (url, socket);
136                                         state = AWAITING_G;
137                                         url = "";
138                                 } else {
139                                         url += *p;
140                                 }
141                                 break;
142                         }
143
144                         if (state == old_state && state != READING_URL) {
145                                 state = AWAITING_G;
146                         }
147
148                         ++p;
149                 }
150         }
151 }
152
153
154 map<string, string>
155 split_get_request(string url)
156 {
157         enum {
158                 AWAITING_QUESTION_MARK,
159                 KEY,
160                 VALUE
161         } state = AWAITING_QUESTION_MARK;
162
163         map<string, string> r;
164         string k;
165         string v;
166         for (size_t i = 0; i < url.length(); ++i) {
167                 switch (state) {
168                 case AWAITING_QUESTION_MARK:
169                         if (url[i] == '?') {
170                                 state = KEY;
171                         }
172                         break;
173                 case KEY:
174                         if (url[i] == '=') {
175                                 v.clear();
176                                 state = VALUE;
177                         } else {
178                                 k += url[i];
179                         }
180                         break;
181                 case VALUE:
182                         if (url[i] == '&') {
183                                 r.insert(make_pair(k, v));
184                                 k.clear ();
185                                 state = KEY;
186                         } else {
187                                 v += url[i];
188                         }
189                         break;
190                 }
191         }
192
193         if (state == VALUE) {
194                 r.insert (make_pair (k, v));
195         }
196
197         return r;
198 }
199
200
201 void
202 JSONServer::request (string url, shared_ptr<tcp::socket> socket)
203 {
204         cout << "request: " << url << "\n";
205
206         auto r = split_get_request (url);
207         for (auto const& i: r) {
208                 cout << i.first << " => " << i.second << "\n";
209         }
210
211         string action;
212         if (r.find ("action") != r.end ()) {
213                 action = r["action"];
214         }
215
216         string json;
217         if (action == "status") {
218
219                 auto jobs = JobManager::instance()->get();
220
221                 json += "{ \"jobs\": [";
222                 for (auto i = jobs.cbegin(); i != jobs.cend(); ++i) {
223                         json += "{ ";
224
225                         if ((*i)->film()) {
226                                 json += "\"dcp\": \"" + (*i)->film()->dcp_name() + "\", ";
227                         }
228
229                         json += "\"name\": \"" + (*i)->json_name() + "\", ";
230                         if ((*i)->progress()) {
231                                 json += "\"progress\": " + raw_convert<string>((*i)->progress().get()) + ", ";
232                         } else {
233                                 json += "\"progress\": unknown, ";
234                         }
235                         json += "\"status\": \"" + (*i)->json_status() + "\"";
236                         json += " }";
237
238                         auto j = i;
239                         ++j;
240                         if (j != jobs.end ()) {
241                                 json += ", ";
242                         }
243                 }
244                 json += "] }";
245         }
246
247         string reply = "HTTP/1.1 200 OK\r\n"
248                 "Content-Length: " + raw_convert<string>(json.length()) + "\r\n"
249                 "Content-Type: application/json\r\n"
250                 "\r\n"
251                 + json + "\r\n";
252         cout << "reply: " << json << "\n";
253         boost::asio::write (*socket, boost::asio::buffer(reply.c_str(), reply.length()));
254 }