CurlLoop.cpp 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>

#include "eigerapi/CurlLoop.h"
#include "eigerapi/EigerDefines.h"
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
29
#include "AutoMutex.h"
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

using namespace eigerapi;

// Constant
double const CurlLoop::FutureRequest::TIMEOUT = 15;

struct CURL_INIT
{
  CURL_INIT()
  {
    curl_global_init(CURL_GLOBAL_ALL);
  }
  ~CURL_INIT()
  {
    curl_global_cleanup();
  }
} global_init;


Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
49
// -------------------- CurlLoop::Request --------------------
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

CurlLoop::FutureRequest::FutureRequest(const std::string& url) :
  m_status(IDLE),
  m_cbk(NULL),
  m_url(url)
{
  if(pthread_mutex_init(&m_lock,NULL))
    THROW_EIGER_EXCEPTION("pthread_mutex_init","Can't initialize the lock");
  if(pthread_cond_init(&m_cond,NULL))
    THROW_EIGER_EXCEPTION("pthread_cond_init",
			  "Can't initialize the variable condition");
  m_handle = curl_easy_init();
  curl_easy_setopt(m_handle,CURLOPT_URL, url.c_str());
  curl_easy_setopt(m_handle, CURLOPT_PROXY, "");
#ifdef DEBUG
  curl_easy_setopt(m_handle, CURLOPT_VERBOSE, 1L);
#endif
}

CurlLoop::FutureRequest::~FutureRequest()
{
  curl_easy_cleanup(m_handle);
  delete m_cbk;
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
73

74
75
  pthread_cond_destroy(&m_cond);
  pthread_mutex_destroy(&m_lock);
76
77
}

Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
inline void CurlLoop::FutureRequest::handle_result(CURLcode result)
{
  Lock lock(&m_lock);
  if(m_status != FutureRequest::CANCEL)
    {
      switch(result)
	{
	case CURLE_OK:
	  m_status = FutureRequest::OK;
	  break;
	default: // error
	  m_status = FutureRequest::ERROR;
	  m_error_code = curl_easy_strerror(result);
	  break;
	}
    }
  pthread_cond_broadcast(&m_cond);
  if(m_cbk)
    _status_changed();
  _request_finished();
}

100
101
102
103
104
105
106
107
108
109
110
111
112
113
void CurlLoop::FutureRequest::wait(double timeout,bool lock_flag) const
{
  Lock lock(&m_lock,lock_flag);

  if(m_status == IDLE)		// weird init
    THROW_EIGER_EXCEPTION("weird initialization","status == IDLE");

  int retcode = 0;
  if(timeout >= 0.)
    {
      struct timeval now;
      struct timespec waitTimeout;
      gettimeofday(&now,NULL);
      waitTimeout.tv_sec = now.tv_sec + long(timeout);
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
114
115
      waitTimeout.tv_nsec = ((now.tv_usec * 1000) +
			     long((timeout - long(timeout)) * 1e9));
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      if(waitTimeout.tv_nsec >= 1000000000L) // Carry
	++waitTimeout.tv_sec,waitTimeout.tv_nsec -= 1000000000L;

      while(retcode != ETIMEDOUT && m_status == RUNNING)
	retcode = pthread_cond_timedwait(&m_cond,&m_lock,&waitTimeout);

      if(retcode == ETIMEDOUT)
	THROW_EIGER_EXCEPTION("wait_status","timeout");
    }
  else
    while(m_status == RUNNING)
      pthread_cond_wait(&m_cond,&m_lock);

  if(m_status == ERROR)
    THROW_EIGER_EXCEPTION("wait_status",m_error_code.c_str());
}

CurlLoop::FutureRequest::Status
CurlLoop::FutureRequest::get_status() const
{
  Lock lock(&m_lock);
  return m_status;
}

140
141
142
143
144
145
struct StatusChangedCallback {
  CurlLoop::FutureRequest::CallbackPtr cbk;
  CurlLoop::FutureRequest::Status status;
  std::string error;
  void fire() { cbk->status_changed(status, error); }
};
146
typedef std::unique_ptr<StatusChangedCallback> StatusChangedCallbackPtr;
147

Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
148
inline void CurlLoop::FutureRequest::_status_changed()
149
150
151
152
153
154
155
156
{
  StatusChangedCallback cbk{*m_cbk, m_status, m_error_code};
  if (!m_cbk_in_thread) {
    cbk.fire();
    return;
  }

#define ERROR(x)						\
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
157
  THROW_EIGER_EXCEPTION("CurlLoop::FutureRequest::_status_changed", x)
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

  StatusChangedCallbackPtr tcbk(new StatusChangedCallback(cbk));
  pthread_t thread;
  pthread_attr_t attr;
  if (pthread_attr_init(&attr))
    ERROR("could not init thread attr");
  else if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
    ERROR("could not set thread attr detached state");
  else if (pthread_create(&thread, &attr, _callback_thread_runFunc, tcbk.get()))
    ERROR("could not create thread");
  else
    tcbk.release();

#undef ERROR
}

void *CurlLoop::FutureRequest::_callback_thread_runFunc(void *data)
{
  StatusChangedCallbackPtr cbk((StatusChangedCallback *) data);
  cbk->fire();
  return NULL;
179
}
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429

void CurlLoop::FutureRequest::register_callback(CallbackPtr& cbk,
						bool in_thread)
{
  if (!cbk)
    THROW_EIGER_EXCEPTION("register_callback","invalid callback");

  Lock lock(&m_lock);
  delete m_cbk;
  m_cbk = new CallbackPtr(cbk);
  m_cbk_in_thread = in_thread;
  if(m_status != RUNNING)
    _status_changed();
}


// ----------------- CurlLoop::ActiveCurlRequest  ------------

struct CurlLoop::ActiveCurlRequest
{
  CurlReq req;
  CURLM *multi_handle;

  ActiveCurlRequest(CurlReq r, CURLM *mh)
    : req(r), multi_handle(mh)
  { curl_multi_add_handle(multi_handle, req->get_handle()); }

  ~ActiveCurlRequest()
  { curl_multi_remove_handle(multi_handle, req->get_handle()); };
};


// ------------------------ CurlLoop -------------------------

CurlLoop::CurlLoop() :
  m_multi_handle(NULL),
  m_quit(false),
  m_thread_id(0)
{
  if(pthread_mutex_init(&m_lock,NULL))
    THROW_EIGER_EXCEPTION("pthread_mutex_init","Can't initialize the lock");
  if(pthread_cond_init(&m_cond,NULL))
    THROW_EIGER_EXCEPTION("pthread_cond_init",
			  "Can't initialize the variable condition");

  m_multi_handle = curl_multi_init();

  if(pipe(m_pipes))
    THROW_EIGER_EXCEPTION("pipe","Can't create pipe");
  
  //set pipe none blocking
  fcntl(m_pipes[0],F_SETFL,O_NONBLOCK);
  fcntl(m_pipes[1],F_SETFL,O_NONBLOCK);

  pthread_create(&m_thread_id,NULL,_runFunc,this);
}

CurlLoop::~CurlLoop()
{
  quit();

  curl_multi_cleanup(m_multi_handle);

  pthread_cond_destroy(&m_cond);
  pthread_mutex_destroy(&m_lock);

}

void CurlLoop::quit()
{
  if(!m_thread_id) return;

  Lock alock(&m_lock);
  m_quit = true;
  write(m_pipes[1],"|",1);
  pthread_cond_broadcast(&m_cond);
  alock.unLock();

  if(m_thread_id > 0)
    pthread_join(m_thread_id,NULL);

  close(m_pipes[1]),close(m_pipes[0]);
  m_pipes[0] = m_pipes[1] = -1;
  m_thread_id = 0;
}

void CurlLoop::add_request(CurlReq new_request)
{
  Lock alock(&m_lock);

  m_new_requests.push_back(new_request);
  new_request->m_status = FutureRequest::RUNNING;

  if(write(m_pipes[1],"|",1) == -1 && errno != EAGAIN)
    THROW_EIGER_EXCEPTION("write into pipe","synchronization failed");

  pthread_cond_broadcast(&m_cond);
}

void CurlLoop::cancel_request(CurlReq request)
{
  {
    Lock alock(&m_lock);
    m_cancel_requests.push_back(request);
  }
  {
    Lock req_lock(&request->m_lock);
    request->m_status = FutureRequest::CANCEL;
  }
}
void* CurlLoop::_runFunc(void *curlloopPt)
{
  ((CurlLoop*)curlloopPt)->_run();
  return NULL;
}

inline void CurlLoop::_check_new_requests()
{
  ListRequests::iterator i, end = m_new_requests.end();
  for(i = m_new_requests.begin(); i != end; ++i)
    {
      const CurlReq& req = *i;
      ActReq act_req(new ActiveCurlRequest(req,m_multi_handle));
      MapRequests::value_type map_value(req->get_handle(),act_req);
      typedef std::pair<MapRequests::iterator,bool> InsertResult;
      InsertResult result = m_pending_requests.insert(map_value);
      if(!result.second)
	THROW_EIGER_EXCEPTION("CurlLoop::_check_new_requests",
			      "handle found in pending requests");
    }
  m_new_requests.clear();
}

inline bool CurlLoop::_wait_input_events()
{
  fd_set fdread;
  fd_set fdwrite;
  fd_set fdexcep;

  FD_ZERO(&fdread);
  FD_SET(m_pipes[0],&fdread);

  FD_ZERO(&fdwrite);
  FD_ZERO(&fdexcep);

  //get curl timeout
  struct timeval timeout{1,0};
  struct timeval* timeoutPt;
  long curl_timeout = -1;
  curl_multi_timeout(m_multi_handle,&curl_timeout);
  if(curl_timeout >=0)
    {
      timeout.tv_sec = curl_timeout / 1000;
      timeout.tv_usec = (curl_timeout % 1000) * 1000;
      timeoutPt = &timeout;
    }
  else
    timeoutPt = NULL;

  int max_fd = -1;
  CURLMcode mc = curl_multi_fdset(m_multi_handle, &fdread,&fdwrite,&fdexcep,
				  &max_fd);
  if(mc != CURLM_OK)
    THROW_EIGER_EXCEPTION("CurlLoop", "error in curl_multi_fdset");
  else if(max_fd == -1) // fds are not ready in curl, wait 100ms
    {
      usleep(100 * 1000);
      return true;
    }

  int ret = select(max_fd + 1,&fdread,&fdwrite,&fdexcep, timeoutPt);
  if(ret < 0)
    {
      if(errno != EINTR)
	THROW_EIGER_EXCEPTION("CurlLoop", "error in select");
      return false;
    }

  return true;
}

inline void CurlLoop::_remove_canceled_requests()
{
  ListRequests::iterator i, end = m_cancel_requests.end();
  for(i = m_cancel_requests.begin(); i != end; ++i)
    {
      const CurlReq& req = *i;
      MapRequests::iterator request = m_pending_requests.find(req->get_handle());
      if(request != m_pending_requests.end())
	m_pending_requests.erase(request);
    }
  m_cancel_requests.clear();
}

void CurlLoop::_run()
{
  Lock lock(&m_lock);
  while(!m_quit)
    {
      while(!m_quit && 
	    m_pending_requests.empty() && m_new_requests.empty())
	pthread_cond_wait(&m_cond,&m_lock);
      if(m_quit)
	break;

      //Add all new requests
      _check_new_requests();

      {
	Unlock u(lock);
	if (!_wait_input_events())
	  continue;
	
	// flush pipe
	char buffer[1024];
	if(read(m_pipes[0],buffer,sizeof(buffer)) == -1 && errno != EAGAIN)
	  THROW_EIGER_EXCEPTION("CurlLoop", "error reading from pipe");
	
	int nb_running;
	curl_multi_perform(m_multi_handle,&nb_running);
      }

      CURLMsg *msg;
      int msg_left;
      while((msg = curl_multi_info_read(m_multi_handle,&msg_left)))
	{
	  if(msg->msg != CURLMSG_DONE)
	    continue;

	  CURL *handle = msg->easy_handle;
	  MapRequests::iterator request = m_pending_requests.find(handle);
	  if(request == m_pending_requests.end())
	    continue;

	  CurlReq req = request->second->req;
	  m_pending_requests.erase(request);

	  Unlock u(lock);
	  req->handle_result(msg->data.result);
	}

      //Remove canceled request
      _remove_canceled_requests();
    }

  //cleanup
  m_pending_requests.clear();
}