]> git.ipfire.org Git - thirdparty/squid.git/blame - src/adaptation/ServiceGroups.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / adaptation / ServiceGroups.h
CommitLineData
bbc27441 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
bbc27441
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
62c7f90e
AR
9#ifndef SQUID_ADAPTATION__SERVICE_GROUPS_H
10#define SQUID_ADAPTATION__SERVICE_GROUPS_H
11
a22e6cd3 12#include "adaptation/Elements.h"
62c7f90e 13#include "adaptation/forward.h"
8bf217bd 14#include "base/RefCount.h"
602d9612 15#include "SquidString.h"
62c7f90e 16
c8ea3cc0
FC
17#include <vector>
18
26ac0430
AJ
19namespace Adaptation
20{
62c7f90e
AR
21
22// Interface for grouping adaptation services together.
23// Specific groups differ in how the first and the next services are selected
a22e6cd3 24class ServiceGroup: public RefCountable
62c7f90e
AR
25{
26public:
a22e6cd3
AR
27 typedef RefCount<ServiceGroup> Pointer;
28
c8ea3cc0 29 typedef std::vector<String> Store;
62c7f90e 30 typedef String Id;
c8ea3cc0 31 typedef unsigned int Pos; // vector<>::position_type
a22e6cd3 32 friend class ServicePlan;
62c7f90e
AR
33
34public:
a22e6cd3 35 ServiceGroup(const String &aKind, bool areAllServicesSame);
62c7f90e
AR
36 virtual ~ServiceGroup();
37
38 virtual void parse();
39 virtual void finalize(); // called after all are parsed
40
a22e6cd3
AR
41 bool wants(const ServiceFilter &filter) const;
42
43protected:
44 ///< whether this group has a service at the specified pos
45 bool has(const Pos pos) const {
46 // does not check that the service at pos still exists
47 return pos < services.size(); // unsigned pos is never negative
48 }
49
50 /// these methods control group iteration; used by ServicePlan
51
52 /// find next to try after failure, starting with pos
53 bool findReplacement(const ServiceFilter &filter, Pos &pos) const;
54 /// find next to link after success, starting with pos
55 bool findLink(const ServiceFilter &filter, Pos &pos) const;
56
57private:
58 ServicePointer at(const Pos pos) const;
59 bool findService(const ServiceFilter &filter, Pos &pos) const;
60
61 void checkUniqueness(const Pos checkedPos) const;
62 void finalizeMsg(const char *msg, const String &culprit, bool error) const;
62c7f90e
AR
63
64public:
65 String kind;
66 Id id;
67 Store services;
7e8c4ee9 68 Store removedServices;///< the disabled services in the case ecap or icap is disabled
a22e6cd3
AR
69
70 Method method; /// based on the first added service
71 VectPoint point; /// based on the first added service
72
73 const bool allServicesSame; // whether we can freely substitute services
62c7f90e
AR
74};
75
76// a group of equivalent services; one service per set is usually used
77class ServiceSet: public ServiceGroup
78{
79public:
80 ServiceSet();
a22e6cd3
AR
81
82protected:
83 virtual bool replace(Pos &pos) const { return has(++pos); }
ced8def3 84 virtual bool advance(Pos &) const { return false; }
62c7f90e
AR
85};
86
87// corner case: a group consisting of one service
88class SingleService: public ServiceGroup
89{
90public:
91 SingleService(const String &aServiceKey);
a22e6cd3
AR
92
93protected:
ced8def3
AJ
94 virtual bool replace(Pos &) const { return false; }
95 virtual bool advance(Pos &) const { return false; }
a22e6cd3
AR
96};
97
98/// a group of services that must be used one after another
99class ServiceChain: public ServiceGroup
100{
101public:
102 ServiceChain();
103
104protected:
ced8def3 105 virtual bool replace(Pos &) const { return false; }
a22e6cd3
AR
106 virtual bool advance(Pos &pos) const { return has(++pos); }
107};
108
109/// a temporary service chain built upon another service request
110class DynamicServiceChain: public ServiceChain
111{
112public:
53340485
AR
113 DynamicServiceChain(const DynamicGroupCfg &cfg, const ServiceFilter &f);
114
115 /// separates dynamic services matching current location from future ones
116 static void Split(const ServiceFilter &filter, const String &ids,
117 DynamicGroupCfg &current, DynamicGroupCfg &future);
62c7f90e
AR
118};
119
a22e6cd3
AR
120/** iterates services stored in a group; iteration is not linear because we
121 need to both replace failed services and advance to the next chain link */
e1381638
AJ
122class ServicePlan
123{
a22e6cd3 124public:
c8ea3cc0 125 typedef unsigned int Pos; // vector<>::position_type
a22e6cd3
AR
126
127public:
128 ServicePlan();
129 explicit ServicePlan(const ServiceGroupPointer &g, const ServiceFilter &filter);
130
131 ///< true iff there are no more services planned
132 bool exhausted() const { return atEof; }
133
134 /// returns nil if the plan is complete
135 ServicePointer current() const; ///< current service
136 ServicePointer replacement(const ServiceFilter &filter); ///< next to try after failure
137 ServicePointer next(const ServiceFilter &filter); ///< next in chain after success
138
139 std::ostream &print(std::ostream &os) const;
140
141private:
142 ServiceGroupPointer group; ///< the group we are iterating
143 Pos pos; ///< current service position within the group
144 bool atEof; ///< cached information for better performance
145};
146
147inline
148std::ostream &operator <<(std::ostream &os, const ServicePlan &p)
149{
150 return p.print(os);
151}
62c7f90e 152
c8ea3cc0 153typedef std::vector<ServiceGroupPointer> Groups;
82afb125
FC
154Groups &AllGroups();
155ServiceGroupPointer FindGroup(const ServiceGroup::Id &id);
62c7f90e 156
62c7f90e
AR
157} // namespace Adaptation
158
159#endif /* SQUID_ADAPTATION__SERVICE_GROUPS_H */
160