Updated license
[anna.git] / include / anna / diameter / stack / Engine.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_diameter_stack_Engine_hpp
38 #define anna_diameter_stack_Engine_hpp
39
40
41 // Local
42 #include <anna/diameter/stack/Dictionary.hpp>
43
44 #include <anna/xml/DTDMemory.hpp>
45 #include <anna/core/RuntimeException.hpp>
46 #include <anna/core/Singleton.hpp>
47
48 // STL
49 #include <string>
50 #include <vector>
51
52
53 namespace anna {
54
55 namespace diameter {
56
57 namespace stack {
58
59
60
61 //------------------------------------------------------------------------------
62 //----------------------------------------------------------------- class Engine
63 //------------------------------------------------------------------------------
64 /**
65 * Engine information
66 */
67 class Engine : public anna::Singleton <Engine> {
68
69
70   const anna::xml::DTDMemory * getDictionariesDTD() const throw() { return &a_dtd; }
71
72 public:
73
74   typedef std::map<int, Dictionary*> stack_container;
75   typedef stack_container::const_iterator const_stack_iterator;
76   typedef stack_container::iterator stack_iterator;
77
78
79   // get
80   /**
81   * Returns the dictionary registered with the provided identifier
82   *
83   * @param stackId Stack identifier.
84   * @return Dictionary reference, NULL if no stack found
85   */
86   const Dictionary * getDictionary(int stackId) const throw();
87
88   /** Beginning stack iterator */
89   const_stack_iterator stack_begin() const throw() { return a_stacks.begin(); }
90   /** Ending stack iterator */
91   const_stack_iterator stack_end() const throw() { return a_stacks.end(); }
92   /** Stack size */
93   int stack_size() const throw() { return a_stacks.size(); }
94
95   // helpers
96   /**
97   * Gets boolean about empty stack condition
98   *
99   * @return Boolean about empty stack condition
100   */
101   bool isEmpty(void) const throw() { return (!a_stacks.size()); }
102
103   /**
104   * Class string representation
105   *
106   * @return String with class content
107   */
108   std::string asString(void) const throw();
109
110   // set
111
112   /**
113   * Creates a diameter dictionary based on xml document file and store it over the provided stack identifier.
114   * If the stack is currently used, an exception will be launched to notify the stack collision (a dictionary
115   * could be removed with #removeStack).
116   * If missing xml path file, the dictionary will be created without loading data.
117   *
118   * <pre>
119   * Method 1 - Create and load xml data (commonly used for mono-load stacks):
120   *     engine->createDictionary(MMS_STACK_ID, "/var/tmp/mms.xml");
121   *
122   * Method 2 - Create and then, load xml data (useful for multi-load stacks):
123   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
124   *     // Loading data:
125   *     d->load("/var/tmp/mms.xml");
126   *
127   * Method 3 - Create and then, load data through Dictionary API (*):
128   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
129   *     // Loading data:
130   *     d->addFormat(...);
131   *     d->addVendor(...);
132   *     ...
133   *
134   * (*) Applications could create a dictionary with its own API (#Dictionary) but it is much more difficult,
135   *     and usually, the method version with xml document load is used instead of this.
136   *
137   * </pre>
138   *
139   * @param stackId Stack identifier for created dictionary.
140   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
141   *
142   * @return Dictionary registered. When exception happen, dictionary can be accessed by #getDictionary
143   */
144   Dictionary * createDictionary(int stackId, const std::string & xmlPathFile = "") throw(anna::RuntimeException);
145
146   /**
147   * Loads an XML dictionary document over the diameter stack identifiers (one or more stack id's).
148   * Passing more than one stack id, synchronized loadings are performed, which could be useful when
149   * an application loads many dictionaries with common avps.
150   *
151   * @param stacks Stacks identifiers over which the dictionary will be load.
152   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
153   */
154   void loadDictionary(const std::vector<int> & stacks, const std::string & xmlPathFile) throw(anna::RuntimeException);
155
156   /**
157   * Loads an XML dictionary document over all the diameter engine registered stacks. When more than one stack id is
158   * used, synchronized loadings are performed, which could be useful when an application loads many dictionaries with
159   * common avps.
160   *
161   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
162   */
163   void loadDictionary(const std::string & xmlPathFile) throw(anna::RuntimeException);
164
165   /**
166   * Remove all stacks registered on diameter stack engine
167   */
168   void removeStacks(void) throw() { a_stacks.clear(); }
169
170   /**
171   * Remove the stack provided.
172   *
173   * @param stackId Stack identifier for created dictionary
174   */
175   void removeStack(int stackId) throw();
176
177 private:
178
179   anna::xml::DTDMemory a_dtd;
180   stack_container a_stacks;
181
182   Engine();
183
184   friend class anna::Singleton <Engine>;
185   friend class Dictionary; // access to 'getDictionariesDTD()'
186 };
187
188
189 }
190 }
191 }
192
193
194 #endif