Fix bugs in thread termination causing occasional pthread
[dcpomatic.git] / src / lib / update_checker.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 "update_checker.h"
22 #include "version.h"
23 #include "util.h"
24 #include <dcp/raw_convert.h>
25 #include <libcxml/cxml.h>
26 #include <curl/curl.h>
27 #include <boost/algorithm/string.hpp>
28 #include <string>
29 #include <iostream>
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 using dcp::raw_convert;
40
41 /** Singleton instance */
42 UpdateChecker* UpdateChecker::_instance = 0;
43
44 static size_t
45 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
46 {
47         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
48 }
49
50 /** Construct an UpdateChecker.  This sets things up and starts a thread to
51  *  do the work.
52  */
53 UpdateChecker::UpdateChecker ()
54         : _buffer (new char[BUFFER_SIZE])
55         , _offset (0)
56         , _curl (0)
57         , _state (NOT_RUN)
58         , _emits (0)
59         , _to_do (0)
60         , _terminate (false)
61 {
62         _curl = curl_easy_init ();
63
64         curl_easy_setopt (_curl, CURLOPT_URL, "https://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 = boost::thread (boost::bind (&UpdateChecker::thread, this));
77 #ifdef DCPOMATIC_LINUX
78         pthread_setname_np (_thread.native_handle(), "update-checker");
79 #endif
80 }
81
82 UpdateChecker::~UpdateChecker ()
83 {
84         boost::this_thread::disable_interruption dis;
85
86         {
87                 boost::mutex::scoped_lock lm (_process_mutex);
88                 _terminate = true;
89         }
90
91         _condition.notify_all ();
92         try {
93                 _thread.join ();
94         } catch (...) {}
95
96         curl_easy_cleanup (_curl);
97         delete[] _buffer;
98 }
99
100 /** Start running the update check */
101 void
102 UpdateChecker::run ()
103 {
104         boost::mutex::scoped_lock lm (_process_mutex);
105         _to_do++;
106         _condition.notify_one ();
107 }
108
109 void
110 UpdateChecker::thread ()
111 {
112         while (true) {
113                 /* Block until there is something to do */
114                 boost::mutex::scoped_lock lock (_process_mutex);
115                 while (_to_do == 0 && !_terminate) {
116                         _condition.wait (lock);
117                 }
118
119                 if (_terminate) {
120                         return;
121                 }
122
123                 --_to_do;
124                 lock.unlock ();
125
126                 try {
127                         _offset = 0;
128
129                         /* Perform the request */
130
131                         int r = curl_easy_perform (_curl);
132                         if (r != CURLE_OK) {
133                                 set_state (FAILED);
134                                 return;
135                         }
136
137                         /* Parse the reply */
138
139                         _buffer[_offset] = '\0';
140                         string s (_buffer);
141                         cxml::Document doc ("Update");
142                         doc.read_string (s);
143
144                         /* Read the current stable and test version numbers */
145
146                         string stable;
147                         string test;
148
149                         {
150                                 boost::mutex::scoped_lock lm (_data_mutex);
151                                 stable = doc.string_child ("Stable");
152                                 test = doc.string_child ("Test");
153                         }
154
155                         if (version_less_than (dcpomatic_version, stable)) {
156                                 _stable = stable;
157                         }
158
159                         if (!test.empty() && version_less_than (dcpomatic_version, test)) {
160                                 _test = test;
161                         }
162
163                         if (_stable || _test) {
164                                 set_state (YES);
165                         } else {
166                                 set_state (NO);
167                         }
168                 } catch (...) {
169                         set_state (FAILED);
170                 }
171         }
172 }
173
174 size_t
175 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
176 {
177         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
178         memcpy (_buffer + _offset, data, t);
179         _offset += t;
180         return t;
181 }
182
183 void
184 UpdateChecker::set_state (State s)
185 {
186         {
187                 boost::mutex::scoped_lock lm (_data_mutex);
188                 _state = s;
189                 _emits++;
190         }
191
192         emit (boost::bind (boost::ref (StateChanged)));
193 }
194
195 UpdateChecker *
196 UpdateChecker::instance ()
197 {
198         if (!_instance) {
199                 _instance = new UpdateChecker ();
200                 _instance->start ();
201         }
202
203         return _instance;
204 }
205
206 bool
207 UpdateChecker::version_less_than (string const & a, string const & b)
208 {
209         vector<string> ap;
210         split (ap, a, is_any_of ("."));
211         vector<string> bp;
212         split (bp, b, is_any_of ("."));
213
214         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
215
216         if (ap[0] != bp[0]) {
217                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
218         }
219
220         if (ap[1] != bp[1]) {
221                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
222         }
223         float am;
224         if (ends_with (ap[2], "devel")) {
225                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
226         } else {
227                 am = raw_convert<int> (ap[2]);
228         }
229
230         float bm;
231         if (ends_with (bp[2], "devel")) {
232                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
233         } else {
234                 bm = raw_convert<int> (bp[2]);
235         }
236
237         return am < bm;
238 }