bes  Updated for version 3.20.13
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 <memory>
28 #include <string>
29 #include <vector>
30 #include <mutex>
31 
32 #include <pthread.h>
33 
34 #include <curl/curl.h>
35 
36 #include "url_impl.h"
37 
38 namespace dmrpp {
39 
40 class Chunk;
41 
50  bool d_in_use;
51  std::shared_ptr<http::url> d_url;
52  Chunk *d_chunk;
53  char d_errbuf[CURL_ERROR_SIZE];
54  CURL *d_handle;
55  curl_slist *d_request_headers;
56 
57  friend class CurlHandlePool;
58 
59 public:
61 
63 
64  void read_data();
65 };
66 
80 private:
81  unsigned int d_max_easy_handles;
82  std::vector<dmrpp_easy_handle *> d_easy_handles;
83  std::recursive_mutex d_get_easy_handle_mutex;
84 
85  friend class Lock;
86 
87 public:
88  CurlHandlePool() = delete;
89  explicit CurlHandlePool(unsigned int max_handles);
90 
92  {
93  for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
94  delete *i;
95  }
96  }
97 
99  unsigned int get_max_handles() const
100  { return d_max_easy_handles; }
101 
102  unsigned int get_handles_available() const
103  {
104  unsigned int n = 0;
105  for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
106  if (!(*i)->d_in_use) {
107  n++;
108 
109  }
110  }
111  return n;
112  }
113 
114  dmrpp_easy_handle *get_easy_handle(Chunk *chunk);
115 
116  void release_handle(dmrpp_easy_handle *h);
117 
118  void release_handle(Chunk *chunk);
119 
120  void release_all_handles();
121 };
122 
123 } // namespace dmrpp
124 
125 #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)
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.