2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_ADAPTATION__SERVICE_GROUPS_H
10 #define SQUID_ADAPTATION__SERVICE_GROUPS_H
12 #include "adaptation/Elements.h"
13 #include "adaptation/forward.h"
14 #include "base/RefCount.h"
15 #include "SquidString.h"
22 // Interface for grouping adaptation services together.
23 // Specific groups differ in how the first and the next services are selected
24 class ServiceGroup
: public RefCountable
27 typedef RefCount
<ServiceGroup
> Pointer
;
29 typedef std::vector
<String
> Store
;
31 typedef unsigned int Pos
; // vector<>::position_type
32 friend class ServicePlan
;
35 ServiceGroup(const String
&aKind
, bool areAllServicesSame
);
36 virtual ~ServiceGroup();
39 virtual void finalize(); // called after all are parsed
41 bool wants(const ServiceFilter
&filter
) const;
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
50 /// these methods control group iteration; used by ServicePlan
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;
58 ServicePointer
at(const Pos pos
) const;
59 bool findService(const ServiceFilter
&filter
, Pos
&pos
) const;
61 void checkUniqueness(const Pos checkedPos
) const;
62 void finalizeMsg(const char *msg
, const String
&culprit
, bool error
) const;
68 Store removedServices
;///< the disabled services in the case ecap or icap is disabled
70 Method method
; /// based on the first added service
71 VectPoint point
; /// based on the first added service
73 const bool allServicesSame
; // whether we can freely substitute services
76 // a group of equivalent services; one service per set is usually used
77 class ServiceSet
: public ServiceGroup
83 virtual bool replace(Pos
&pos
) const { return has(++pos
); }
84 virtual bool advance(Pos
&) const { return false; }
87 // corner case: a group consisting of one service
88 class SingleService
: public ServiceGroup
91 SingleService(const String
&aServiceKey
);
94 virtual bool replace(Pos
&) const { return false; }
95 virtual bool advance(Pos
&) const { return false; }
98 /// a group of services that must be used one after another
99 class ServiceChain
: public ServiceGroup
105 virtual bool replace(Pos
&) const { return false; }
106 virtual bool advance(Pos
&pos
) const { return has(++pos
); }
109 /// a temporary service chain built upon another service request
110 class DynamicServiceChain
: public ServiceChain
113 DynamicServiceChain(const DynamicGroupCfg
&cfg
, const ServiceFilter
&f
);
115 /// separates dynamic services matching current location from future ones
116 static void Split(const ServiceFilter
&filter
, const String
&ids
,
117 DynamicGroupCfg
¤t
, DynamicGroupCfg
&future
);
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 */
125 typedef unsigned int Pos
; // vector<>::position_type
129 explicit ServicePlan(const ServiceGroupPointer
&g
, const ServiceFilter
&filter
);
131 ///< true iff there are no more services planned
132 bool exhausted() const { return atEof
; }
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
139 std::ostream
&print(std::ostream
&os
) const;
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
148 std::ostream
&operator <<(std::ostream
&os
, const ServicePlan
&p
)
153 typedef std::vector
<ServiceGroupPointer
> Groups
;
155 ServiceGroupPointer
FindGroup(const ServiceGroup::Id
&id
);
157 } // namespace Adaptation
159 #endif /* SQUID_ADAPTATION__SERVICE_GROUPS_H */