]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/ServiceRep.cc
2 * DEBUG: section 93 eCAP Interface
6 #include <libecap/adapter/service.h>
7 #include <libecap/common/options.h>
8 #include <libecap/common/name.h>
9 #include <libecap/common/named_values.h>
10 #include "adaptation/ecap/Config.h"
11 #include "adaptation/ecap/Host.h"
12 #include "adaptation/ecap/ServiceRep.h"
13 #include "adaptation/ecap/XactionRep.h"
14 #include "base/TextException.h"
16 // configured eCAP service wrappers
17 static std::list
<Adaptation::Ecap::ServiceRep::AdapterService
> TheServices
;
24 /// wraps Adaptation::Ecap::ServiceConfig to allow eCAP visitors
25 class ConfigRep
: public libecap::Options
28 typedef Adaptation::Ecap::ServiceConfig Master
;
29 typedef libecap::Name Name
;
30 typedef libecap::Area Area
;
32 ConfigRep(const Master
&aMaster
);
34 // libecap::Options API
35 virtual const libecap::Area
option(const libecap::Name
&name
) const;
36 virtual void visitEachOption(libecap::NamedValueVisitor
&visitor
) const;
38 const Master
&master
; ///< the configuration being wrapped
42 } // namespace Adaptation
45 Adaptation::Ecap::ConfigRep::ConfigRep(const Master
&aMaster
): master(aMaster
)
50 Adaptation::Ecap::ConfigRep::option(const libecap::Name
&name
) const
52 // we may supply the params we know about, but only when names have host ID
53 if (name
== metaBypassable
)
54 return Area(master
.bypass
? "1" : "0", 1);
56 // TODO: We could build a by-name index, but is it worth it? Good adapters
57 // should use visitEachOption() instead, to check for name typos/errors.
58 typedef Master::Extensions::const_iterator MECI
;
59 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
) {
61 return Area(i
->second
.data(), i
->second
.size());
68 Adaptation::Ecap::ConfigRep::visitEachOption(libecap::NamedValueVisitor
&visitor
) const
70 // we may supply the params we know about too, but only if we set host ID
71 visitor
.visit(metaBypassable
, Area(master
.bypass
? "1" : "0", 1));
73 // visit adapter-specific options (i.e., those not recognized by Squid)
74 typedef Master::Extensions::const_iterator MECI
;
75 for (MECI i
= master
.extensions
.begin(); i
!= master
.extensions
.end(); ++i
)
76 visitor
.visit(Name(i
->first
), Area::FromTempString(i
->second
));
81 Adaptation::Ecap::ServiceRep::ServiceRep(const ServiceConfigPointer
&cfg
):
82 /*AsyncJob("Adaptation::Ecap::ServiceRep"),*/ Adaptation::Service(cfg
),
87 Adaptation::Ecap::ServiceRep::~ServiceRep()
91 void Adaptation::Ecap::ServiceRep::noteFailure()
93 assert(false); // XXX: should this be ICAP-specific?
97 Adaptation::Ecap::ServiceRep::finalize()
99 Adaptation::Service::finalize();
100 theService
= FindAdapterService(cfg().uri
);
103 tryConfigureAndStart();
105 } catch (const std::exception
&e
) { // standardized exceptions
106 if (!handleFinalizeFailure(e
.what()))
107 throw; // rethrow for upper layers to handle
108 } catch (...) { // all other exceptions
109 if (!handleFinalizeFailure("unrecognized exception"))
110 throw; // rethrow for upper layers to handle
112 return; // success or handled exception
114 debugs(93,DBG_IMPORTANT
, "WARNING: configured ecap_service was not loaded: " << cfg().uri
);
118 /// attempts to configure and start eCAP service; the caller handles exceptions
120 Adaptation::Ecap::ServiceRep::tryConfigureAndStart()
122 debugs(93,2, HERE
<< "configuring eCAP service: " << theService
->uri());
123 const ConfigRep
cfgRep(dynamic_cast<const ServiceConfig
&>(cfg()));
124 theService
->configure(cfgRep
);
126 debugs(93,DBG_IMPORTANT
, "Starting eCAP service: " << theService
->uri());
130 /// handles failures while configuring or starting an eCAP service;
131 /// returns false if the error must be propagated to higher levels
133 Adaptation::Ecap::ServiceRep::handleFinalizeFailure(const char *error
)
135 const bool salvage
= cfg().bypass
;
136 const int level
= salvage
? DBG_IMPORTANT
:DBG_CRITICAL
;
137 const char *kind
= salvage
? "optional" : "essential";
138 debugs(93, level
, "ERROR: failed to start " << kind
<< " eCAP service: " <<
139 cfg().uri
<< ":\n" << error
);
142 return false; // we cannot handle the problem; the caller may escalate
144 // make up() false, preventing new adaptation requests and enabling bypass
146 debugs(93, level
, "WARNING: " << kind
<< " eCAP service is " <<
147 "down after initialization failure: " << cfg().uri
);
149 return true; // tell the caller to ignore the problem because we handled it
152 bool Adaptation::Ecap::ServiceRep::probed() const
154 return true; // we "probe" the adapter in finalize().
157 bool Adaptation::Ecap::ServiceRep::up() const
159 return theService
!= NULL
;
162 bool Adaptation::Ecap::ServiceRep::wantsUrl(const String
&urlPath
) const
165 return theService
->wantsUrl(urlPath
.termedBuf());
168 Adaptation::Initiate
*
169 Adaptation::Ecap::ServiceRep::makeXactLauncher(HttpMsg
*virgin
,
173 XactionRep
*rep
= new XactionRep(virgin
, cause
, Pointer(this));
174 XactionRep::AdapterXaction
x(theService
->makeXaction(rep
));
179 // returns a temporary string depicting service status, for debugging
180 const char *Adaptation::Ecap::ServiceRep::status() const
182 // TODO: move generic stuff from eCAP and ICAP to Adaptation
191 buf
.append("down", 4);
194 buf
.append(",detached", 9);
199 return buf
.content();
202 void Adaptation::Ecap::ServiceRep::detach()
207 bool Adaptation::Ecap::ServiceRep::detached() const
212 Adaptation::Ecap::ServiceRep::AdapterService
213 Adaptation::Ecap::FindAdapterService(const String
& serviceUri
)
215 typedef std::list
<ServiceRep::AdapterService
>::const_iterator ASCI
;
216 for (ASCI s
= TheServices
.begin(); s
!= TheServices
.end(); ++s
) {
218 if (serviceUri
== (*s
)->uri().c_str())
221 return ServiceRep::AdapterService();
225 Adaptation::Ecap::RegisterAdapterService(const Adaptation::Ecap::ServiceRep::AdapterService
& adapterService
)
227 typedef std::list
<ServiceRep::AdapterService
>::iterator ASI
;
228 for (ASI s
= TheServices
.begin(); s
!= TheServices
.end(); ++s
) {
230 if (adapterService
->uri() == (*s
)->uri()) {
232 debugs(93, 3, "updated eCAP module service: " <<
233 adapterService
->uri());
237 TheServices
.push_back(adapterService
);
238 debugs(93, 3, "registered eCAP module service: " << adapterService
->uri());
242 Adaptation::Ecap::UnregisterAdapterService(const String
& serviceUri
)
244 typedef std::list
<ServiceRep::AdapterService
>::iterator ASI
;
245 for (ASI s
= TheServices
.begin(); s
!= TheServices
.end(); ++s
) {
246 if (serviceUri
== (*s
)->uri().c_str()) {
247 TheServices
.erase(s
);
248 debugs(93, 3, "unregistered eCAP module service: " << serviceUri
);
252 debugs(93, 3, "failed to unregister eCAP module service: " << serviceUri
);
256 Adaptation::Ecap::CheckUnusedAdapterServices(const Adaptation::Services
& cfgs
)
258 typedef std::list
<ServiceRep::AdapterService
>::const_iterator ASCI
;
259 for (ASCI loaded
= TheServices
.begin(); loaded
!= TheServices
.end();
262 for (Services::const_iterator cfged
= cfgs
.begin();
263 cfged
!= cfgs
.end() && !found
; ++cfged
) {
264 found
= (*cfged
)->cfg().uri
== (*loaded
)->uri().c_str();
267 debugs(93, 1, "Warning: loaded eCAP service has no matching " <<
268 "ecap_service config option: " << (*loaded
)->uri());