69e063e71c8e7dc8db8296651a16a723037b0f7c
[anna.git] / include / anna / diameter / codec / EngineManager.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_diameter_codec_EngineManager_hpp
10 #define anna_diameter_codec_EngineManager_hpp
11
12
13 // Project
14 #include <anna/core/Singleton.hpp>
15 #include <anna/diameter/defines.hpp>
16
17 // Standard
18 #include <map>
19
20
21 namespace anna {
22
23 namespace xml {
24   class Node;
25
26 }
27 namespace diameter {
28
29 namespace codec {
30
31 class Engine;
32
33 typedef std::map<anna::diameter::ApplicationId, Engine*> appid_codec_engines_t;
34 typedef std::map<anna::diameter::ApplicationId, Engine*>::const_iterator appid_codec_engines_it;
35 typedef std::map<anna::diameter::ApplicationId, Engine*>::iterator appid_codec_engines_nc_it;
36
37 /**
38  * Helper class to centralize application codec engines and ease the automatic codec engine selection from
39  * codec resources (mainly Message class, but also Avp).
40  *
41  * This is useful if you enable the auto selection from messages application id, avoiding to pre-initialize
42  * such codec messages with a specific engine before interpreting the data sources or building.
43  * At multithread application, it is recommended to specialize the message codec for each stack without using
44  * this engine manager.
45  */
46 class EngineManager : public anna::Singleton <EngineManager> {
47
48 private:
49
50   bool a_autoSelectFromApplicationId;
51   appid_codec_engines_t a_appid_codec_engines;
52
53   // private constructor
54   EngineManager() : a_autoSelectFromApplicationId(true) {};
55
56 public:
57
58   /**
59    * First element iterator
60    */
61   appid_codec_engines_it begin() const throw() { return a_appid_codec_engines.begin(); }
62
63   /**
64    * Last element iterator
65    */
66   appid_codec_engines_it end() const throw() { return a_appid_codec_engines.end(); }
67
68   /**
69    * Number of registered engines
70    */
71   int size() const throw() { return a_appid_codec_engines.size(); }
72
73   /**
74    * Registers a new codec engine (externally allocated) associated to an application id.
75    * If the application id exists, the new engine pointer will replace the existing one.
76    *
77    * @param appid Application-Id
78    * @param engine Associated codec engine
79    */
80   void registerCodecEngine(const ApplicationId &appid, Engine* engine) throw();
81
82   /**
83    * Get the associated codec engine for a provided Application-Id.
84    *
85    * @param appid Application-Id
86    *
87    * @return Found codec engine, NULL if not found
88    */
89   Engine *getCodecEngine(const ApplicationId &appid) const throw();
90
91   /**
92    * If only one codec engine is registered (mono-stack application), it will be returned
93    * (NULL in other case). This is because mono-stack applications could merge compatible
94    * stacks into one unique dictionary, using a global value for stack id with no relation
95    * regarding application-id concept.
96    *
97    * @return Unique codec engine, NULL if not unique (empty manager or more than one)
98    */
99   Engine *getMonoStackCodecEngine() const throw() {
100     return ((size() != 1) ? NULL : begin()->second);
101   }
102
103   /**
104    * The user could select the appropriate codec engine depending on the context, but some applications could
105    * consider interesting automatic codec engine selection from managed messages (decoded from any source,
106    * or built from scratch). Then, this engine manager will be used registering all the supported stacks
107    * with their corresponding codec engines, and automatic selection will be done during decoding of any
108    * supported source of data (hexadecimal buffers, xml documents) or build operations.
109    *
110    * If the user pre-initializes the codec engine for a #Message or #Avp element, this selection is ignored
111    * even if enabled: the codec engine only can be established one time, for security reasons.
112    *
113    * @param enable Activates/deactivates the codec engine selection from the Application-Id value. True by default
114    * and applicable when this manager store is not empty.
115    */
116   void selectFromApplicationId (bool enable) throw() { a_autoSelectFromApplicationId = enable; }
117
118   /**
119     Gets the currently configured behaviour regarding codec engine selection.
120
121     @return True if selection is done with the Application-Id (default behaviour). False to disable (the manager
122     could be used for some other things which could be also interesting).
123    */
124   bool selectFromApplicationId (void) throw() { return a_autoSelectFromApplicationId; }
125
126   /**
127      Class XML representation.
128      \param parent XML node over which we will put instance information.
129      \return XML documentcon with class content.
130   */
131   virtual anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
132
133
134   friend class anna::Singleton <EngineManager>;
135 };
136
137 }
138 }
139 }
140
141 #endif
142