bes Updated for version 3.20.10
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
31#include <pthread.h>
32
33#include <curl/curl.h>
34
35#include "url_impl.h"
36
37namespace dmrpp {
38
39class Chunk;
40
44class Lock {
45private:
46 pthread_mutex_t &m_mutex;
47
48 Lock();
49
50 Lock(const Lock &rhs);
51
52public:
53 Lock(pthread_mutex_t &lock);
54
55 virtual ~Lock();
56};
57
66 bool d_in_use;
67 std::shared_ptr<http::url> d_url;
68 Chunk *d_chunk;
69 char d_errbuf[CURL_ERROR_SIZE];
70 CURL *d_handle;
71 curl_slist *d_request_headers;
72
73 friend class CurlHandlePool;
74
75public:
77
79
80 void read_data();
81};
82
96private:
97 unsigned int d_max_easy_handles;
98 std::vector<dmrpp_easy_handle *> d_easy_handles;
99 pthread_mutex_t d_get_easy_handle_mutex;
100
101 friend class Lock;
103
104public:
105
106 explicit CurlHandlePool(unsigned int max_handles);
107
109 {
110 for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
111 delete *i;
112 }
113 }
114
116 unsigned int get_max_handles() const
117 { return d_max_easy_handles; }
118
119 unsigned int get_handles_available() const
120 {
121 unsigned int n = 0;
122 for (auto i = d_easy_handles.begin(), e = d_easy_handles.end(); i != e; ++i) {
123 if (!(*i)->d_in_use) {
124 n++;
125
126 }
127 }
128 return n;
129 }
130
131 dmrpp_easy_handle *get_easy_handle(Chunk *chunk);
132
133 void release_handle(dmrpp_easy_handle *h);
134
135 void release_handle(Chunk *chunk);
136
137 void release_all_handles();
138};
139
148class SwimLane {
149 CurlHandlePool &d_pool;
150 std::vector<dmrpp_easy_handle *> d_handles;
151public:
152 SwimLane(CurlHandlePool &pool) : d_pool(pool)
153 {}
154
155 SwimLane(CurlHandlePool &pool, dmrpp_easy_handle *h) : d_pool(pool)
156 {
157 d_handles.push_back(h);
158 }
159
160 virtual ~SwimLane()
161 {
162 for (auto i = d_handles.begin(), e = d_handles.end(); i != e; ++i) {
163 d_pool.release_handle(*i);
164 }
165 }
166
167 void add_handle(dmrpp_easy_handle *h)
168 {
169 d_handles.push_back(h);
170 }
171};
172
173} // namespace dmrpp
174
175#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.