Don't start thread in constructor; tidy up thread in destructor (UpdateChecker).
[dcpomatic.git] / src / lib / update_checker.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "update_checker.h"
21 #include "version.h"
22 #include "safe_stringstream.h"
23 #include "config.h"
24 #include "util.h"
25 #include "raw_convert.h"
26 #include <libcxml/cxml.h>
27 #include <curl/curl.h>
28 #include <boost/algorithm/string.hpp>
29 #include <string>
30
31 #define BUFFER_SIZE 1024
32
33 using std::cout;
34 using std::min;
35 using std::string;
36 using std::vector;
37 using boost::is_any_of;
38 using boost::ends_with;
39
40 /** Singleton instance */
41 UpdateChecker* UpdateChecker::_instance = 0;
42
43 static size_t
44 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
45 {
46         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
47 }
48
49 /** Construct an UpdateChecker.  This sets things up and starts a thread to
50  *  do the work.
51  */
52 UpdateChecker::UpdateChecker ()
53         : _buffer (new char[BUFFER_SIZE])
54         , _offset (0)
55         , _curl (0)
56         , _state (NOT_RUN)
57         , _emits (0)
58         , _thread (0)
59         , _to_do (0)
60         , _terminate (false)
61 {
62         _curl = curl_easy_init ();
63
64         curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
65         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
66         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
67         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
68
69         string const agent = "dcpomatic/" + string (dcpomatic_version);
70         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
71 }
72
73 void
74 UpdateChecker::start ()
75 {
76         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
77 }
78
79 UpdateChecker::~UpdateChecker ()
80 {
81         {
82                 boost::mutex::scoped_lock lm (_process_mutex);
83                 _terminate = true;
84         }
85
86         _condition.notify_all ();
87         if (_thread) {
88                 _thread->join ();
89         }
90         delete _thread;
91
92         curl_easy_cleanup (_curl);
93         delete[] _buffer;
94 }
95
96 /** Start running the update check */
97 void
98 UpdateChecker::run ()
99 {
100         boost::mutex::scoped_lock lm (_process_mutex);
101         _to_do++;
102         _condition.notify_one ();
103 }
104
105 void
106 UpdateChecker::thread ()
107 {
108         while (true) {
109                 /* Block until there is something to do */
110                 boost::mutex::scoped_lock lock (_process_mutex);
111                 while (_to_do == 0 && !_terminate) {
112                         _condition.wait (lock);
113                 }
114
115                 if (_terminate) {
116                         return;
117                 }
118
119                 --_to_do;
120                 lock.unlock ();
121
122                 try {
123                         _offset = 0;
124
125                         /* Perform the request */
126
127                         int r = curl_easy_perform (_curl);
128                         if (r != CURLE_OK) {
129                                 set_state (FAILED);
130                                 return;
131                         }
132
133                         /* Parse the reply */
134
135                         _buffer[_offset] = '\0';
136                         string s (_buffer);
137                         cxml::Document doc ("Update");
138                         doc.read_string (s);
139
140                         /* Read the current stable and test version numbers */
141
142                         string stable;
143                         string test;
144
145                         {
146                                 boost::mutex::scoped_lock lm (_data_mutex);
147                                 stable = doc.string_child ("Stable");
148                                 test = doc.string_child ("Test");
149                         }
150
151                         if (version_less_than (dcpomatic_version, stable)) {
152                                 _stable = stable;
153                         }
154
155                         if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) {
156                                 _test = test;
157                         }
158
159                         if (_stable || _test) {
160                                 set_state (YES);
161                         } else {
162                                 set_state (NO);
163                         }
164                 } catch (...) {
165                         set_state (FAILED);
166                 }
167         }
168 }
169
170 size_t
171 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
172 {
173         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
174         memcpy (_buffer + _offset, data, t);
175         _offset += t;
176         return t;
177 }
178
179 void
180 UpdateChecker::set_state (State s)
181 {
182         {
183                 boost::mutex::scoped_lock lm (_data_mutex);
184                 _state = s;
185                 _emits++;
186         }
187
188         emit (boost::bind (boost::ref (StateChanged)));
189 }
190
191 UpdateChecker *
192 UpdateChecker::instance ()
193 {
194         if (!_instance) {
195                 _instance = new UpdateChecker ();
196                 _instance->start ();
197         }
198
199         return _instance;
200 }
201
202 bool
203 UpdateChecker::version_less_than (string const & a, string const & b)
204 {
205         vector<string> ap;
206         split (ap, a, is_any_of ("."));
207         vector<string> bp;
208         split (bp, b, is_any_of ("."));
209
210         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
211
212         if (ap[0] != bp[0]) {
213                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
214         }
215
216         if (ap[1] != bp[1]) {
217                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
218         }
219         float am;
220         if (ends_with (ap[2], "devel")) {
221                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
222         } else {
223                 am = raw_convert<int> (ap[2]);
224         }
225
226         float bm;
227         if (ends_with (bp[2], "devel")) {
228                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
229         } else {
230                 bm = raw_convert<int> (bp[2]);
231         }
232
233         return am < bm;
234 }