bes  Updated for version 3.20.8
CurlHandlePool.h
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of the BES
4 
5 // Copyright (c) 2018 OPeNDAP, Inc.
6 // Author: James Gallagher<jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23 
24 #ifndef _HandlePool_h
25 #define _HandlePool_h 1
26 
27 #include <string>
28 #include <vector>
29 
30 #include <pthread.h>
31 
32 #include <curl/curl.h>
33 
34 namespace dmrpp {
35 
36 class Chunk;
37 
41 class Lock {
42 private:
43  pthread_mutex_t &m_mutex;
44 
45  Lock();
46 
47  Lock(const Lock &rhs);
48 
49 public:
50  Lock(pthread_mutex_t &lock);
51 
52  virtual ~Lock();
53 };
54 
63  bool d_in_use;
64  std::string d_url;
65  Chunk *d_chunk;
66  char d_errbuf[CURL_ERROR_SIZE];
67  CURL *d_handle;
68  curl_slist *d_request_headers;
69 
70  friend class CurlHandlePool;
71 
72 public:
74 
76 
77  void read_data();
78 };
79 
93 private:
94  unsigned int d_max_easy_handles;
95  std::vector<dmrpp_easy_handle *> d_easy_handles;
96  pthread_mutex_t d_get_easy_handle_mutex;
97 
98  friend class Lock;
100 
101 public:
102 
103  explicit CurlHandlePool(unsigned int max_handles);
104 
105  ~CurlHandlePool()
106  {
107  for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
108  delete *i;
109  }
110  }
111 
113  unsigned int get_max_handles() const
114  { return d_max_easy_handles; }
115 
116  unsigned int get_handles_available() const
117  {
118  unsigned int n = 0;
119  for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
120  if (!(*i)->d_in_use) {
121  n++;
122 
123  }
124  }
125  return n;
126  }
127 
128  dmrpp_easy_handle *get_easy_handle(Chunk *chunk);
129 
130  void release_handle(dmrpp_easy_handle *h);
131 
132  void release_handle(Chunk *chunk);
133 
134  void release_all_handles();
135 };
136 
145 class SwimLane {
146  CurlHandlePool &d_pool;
147  std::vector<dmrpp_easy_handle *> d_handles;
148 public:
149  SwimLane(CurlHandlePool &pool) : d_pool(pool)
150  {}
151 
152  SwimLane(CurlHandlePool &pool, dmrpp_easy_handle *h) : d_pool(pool)
153  {
154  d_handles.push_back(h);
155  }
156 
157  virtual ~SwimLane()
158  {
159  for (auto i = d_handles.begin(), e = d_handles.end(); i != e; ++i) {
160  d_pool.release_handle(*i);
161  }
162  }
163 
164  void add_handle(dmrpp_easy_handle *h)
165  {
166  d_handles.push_back(h);
167  }
168 };
169 
170 } // namespace dmrpp
171 
172 #endif // _HandlePool_h
void release_handle(dmrpp_easy_handle *h)
unsigned int get_max_handles() const
Get the number of handles in the pool.
dmrpp_easy_handle * get_easy_handle(Chunk *chunk)
Add the given header & value to the curl slist.
Bundle a libcurl easy handle with other information.
void read_data()
This is the read_data() method for all transfers.
dmrpp_easy_handle()
Build a string with hex info about stuff libcurl gets.