]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/ServiceRep.cc
Polished source-maintenance
[thirdparty/squid.git] / src / adaptation / ecap / ServiceRep.cc
1 /*
2 * DEBUG: section 93 eCAP Interface
3 */
4 #include "squid.h"
5 #include <list>
6 #include <libecap/adapter/service.h>
7 #include "adaptation/ecap/ServiceRep.h"
8 #include "adaptation/ecap/XactionRep.h"
9 #include "base/TextException.h"
10
11 // configured eCAP service wrappers
12 static std::list<Adaptation::Ecap::ServiceRep::AdapterService> TheServices;
13
14 Adaptation::Ecap::ServiceRep::ServiceRep(const Adaptation::ServiceConfig &cfg):
15 /*AsyncJob("Adaptation::Ecap::ServiceRep"),*/ Adaptation::Service(cfg),
16 isDetached(false)
17 {
18 }
19
20 Adaptation::Ecap::ServiceRep::~ServiceRep()
21 {
22 }
23
24 void Adaptation::Ecap::ServiceRep::noteFailure()
25 {
26 assert(false); // XXX: should this be ICAP-specific?
27 }
28
29 void
30 Adaptation::Ecap::ServiceRep::finalize()
31 {
32 Adaptation::Service::finalize();
33 theService = FindAdapterService(cfg().uri);
34 if (theService) {
35 debugs(93,3, HERE << "starting eCAP service: " << theService->uri());
36 theService->start();
37 } else {
38 debugs(93,1, "Warning: configured ecap_service was not loaded: " <<
39 cfg().uri);
40 }
41 }
42
43 bool Adaptation::Ecap::ServiceRep::probed() const
44 {
45 return true; // we "probe" the adapter in finalize().
46 }
47
48 bool Adaptation::Ecap::ServiceRep::up() const
49 {
50 return theService != NULL;
51 }
52
53 bool Adaptation::Ecap::ServiceRep::wantsUrl(const String &urlPath) const
54 {
55 Must(up());
56 return theService->wantsUrl(urlPath.termedBuf());
57 }
58
59 Adaptation::Initiate *
60 Adaptation::Ecap::ServiceRep::makeXactLauncher(Adaptation::Initiator *initiator,
61 HttpMsg *virgin, HttpRequest *cause)
62 {
63 Must(up());
64 XactionRep *rep = new XactionRep(initiator, virgin, cause, Pointer(this));
65 XactionRep::AdapterXaction x(theService->makeXaction(rep));
66 rep->master(x);
67 return rep;
68 }
69
70 // returns a temporary string depicting service status, for debugging
71 const char *Adaptation::Ecap::ServiceRep::status() const
72 {
73 // TODO: move generic stuff from eCAP and ICAP to Adaptation
74 static MemBuf buf;
75
76 buf.reset();
77 buf.append("[", 1);
78
79 if (up())
80 buf.append("up", 2);
81 else
82 buf.append("down", 4);
83
84 if (detached())
85 buf.append(",detached", 9);
86
87 buf.append("]", 1);
88 buf.terminate();
89
90 return buf.content();
91 }
92
93 void Adaptation::Ecap::ServiceRep::detach()
94 {
95 isDetached = true;
96 }
97
98 bool Adaptation::Ecap::ServiceRep::detached() const
99 {
100 return isDetached;
101 }
102
103 Adaptation::Ecap::ServiceRep::AdapterService
104 Adaptation::Ecap::FindAdapterService(const String& serviceUri)
105 {
106 typedef std::list<ServiceRep::AdapterService>::const_iterator ASCI;
107 for (ASCI s = TheServices.begin(); s != TheServices.end(); ++s) {
108 Must(*s);
109 if (serviceUri == (*s)->uri().c_str())
110 return *s;
111 }
112 return ServiceRep::AdapterService();
113 }
114
115 void
116 Adaptation::Ecap::RegisterAdapterService(const Adaptation::Ecap::ServiceRep::AdapterService& adapterService)
117 {
118 typedef std::list<ServiceRep::AdapterService>::iterator ASI;
119 for (ASI s = TheServices.begin(); s != TheServices.end(); ++s) {
120 Must(*s);
121 if (adapterService->uri() == (*s)->uri()) {
122 *s = adapterService;
123 debugs(93, 3, "updated eCAP module service: " <<
124 adapterService->uri());
125 return;
126 }
127 }
128 TheServices.push_back(adapterService);
129 debugs(93, 3, "registered eCAP module service: " << adapterService->uri());
130 }
131
132 void
133 Adaptation::Ecap::UnregisterAdapterService(const String& serviceUri)
134 {
135 typedef std::list<ServiceRep::AdapterService>::iterator ASI;
136 for (ASI s = TheServices.begin(); s != TheServices.end(); ++s) {
137 if (serviceUri == (*s)->uri().c_str()) {
138 TheServices.erase(s);
139 debugs(93, 3, "unregistered eCAP module service: " << serviceUri);
140 return;
141 }
142 }
143 debugs(93, 3, "failed to unregister eCAP module service: " << serviceUri);
144 }
145
146 void
147 Adaptation::Ecap::CheckUnusedAdapterServices(const Adaptation::Services& cfgs)
148 {
149 typedef std::list<ServiceRep::AdapterService>::const_iterator ASCI;
150 for (ASCI loaded = TheServices.begin(); loaded != TheServices.end();
151 ++loaded) {
152 bool found = false;
153 for (Services::const_iterator cfged = cfgs.begin();
154 cfged != cfgs.end() && !found; ++cfged) {
155 found = (*cfged)->cfg().uri == (*loaded)->uri().c_str();
156 }
157 if (!found)
158 debugs(93, 1, "Warning: loaded eCAP service has no matching " <<
159 "ecap_service config option: " << (*loaded)->uri());
160 }
161 }