1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
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 
9 /* DEBUG: section 16    Cache Manager API */
10 
11 #ifndef SQUID_MGR_ACTION_H
12 #define SQUID_MGR_ACTION_H
13 
14 #include "ipc/forward.h"
15 #include "mgr/forward.h"
16 
17 class StoreEntry;
18 
19 namespace Mgr
20 {
21 
22 /// Base API for organizing the processing of a compiled cache manager command.
23 /// Not a job because all methods are synchronous (but they may start jobs).
24 class Action: public RefCountable
25 {
26 public:
27     typedef RefCount<Action> Pointer;
28 
29 public:
30     Action(const CommandPointer &aCmd);
31     virtual ~Action();
32 
33     /* for local Cache Manager use */
34 
35     /// collect + fillEntry: collect local information and fill the store entry
36     void run(StoreEntry *entry, bool writeHttpHeader);
37 
38     /// prepare store entry, dump info, close store entry (if possible)
39     void fillEntry(StoreEntry *entry, bool writeHttpHeader);
40 
41     /* for global Coordinator use */
42 
43     /// incrementally merge in remote information (of the same action type)
44     virtual void add(const Action &action);
45 
46     /* global-local communication */
47 
48     /// respond to Coordinator request; default is to collect and sendResponse
49     virtual void respond(const Request &request);
50 
51     /// pack collected action info into a message to be sent to Coordinator
pack(Ipc::TypedMsgHdr &)52     virtual void pack(Ipc::TypedMsgHdr &) const {}
53 
54     /// unpack action info from the message received by Coordinator
unpack(const Ipc::TypedMsgHdr &)55     virtual void unpack(const Ipc::TypedMsgHdr &) {}
56 
57     /// notify Coordinator that this action is done with local processing
58     void sendResponse(unsigned int requestId);
59 
60     /* Action properties */
61 
62     /// whether at least some local kid info can be combined and, hence, the
63     /// combined data should be written at the end of the coordinated response
aggregatable()64     virtual bool aggregatable() const { return true; } // most kid classes are
65 
66     bool atomic() const; ///< dump() call writes everything before returning
67     const char *name() const; ///< label as seen in the cache manager menu
68     const Command &command() const; ///< the cause of this action
69 
70     StoreEntry *createStoreEntry() const; ///< creates store entry from params
71 
72     ///< Content-Type: header value for this report
contentType()73     virtual const char *contentType() const {return "text/plain;charset=utf-8";}
74 
75 protected:
76     /// calculate and keep local action-specific information
collect()77     virtual void collect() {}
78 
79     /** start writing action-specific info to Store entry;
80      * may collect info during dump, especially if collect() did nothing
81      * non-atomic() actions may continue writing asynchronously after returning
82      */
dump(StoreEntry *)83     virtual void dump(StoreEntry *) {}
84 
85 private:
86     const CommandPointer cmd; ///< the command that caused this action
87 
88 private:
89     Action(const Action &); // not implemented
90     Action &operator= (const Action &); // not implemented
91 };
92 
93 } // namespace Mgr
94 
95 #endif /* SQUID_MGR_ACTION_H */
96 
97