Add infos into target window
[ryzomcore.git] / ryzom / server / src / ai_data_service / ai_manager.h
blob339e044bc4bea3e6aa3d25b9fd72042523f9f65b
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef RYAI_AI_MANAGER_H
20 #define RYAI_AI_MANAGER_H
22 #define RYAI_AI_MANAGER_MAX_MANAGERS 1024
24 #include "nel/misc/types_nl.h"
25 #include "nel/misc/stream.h"
26 #include "nel/net/unified_network.h"
27 #include "ai_share/ai_actions.h"
31 ---------------------------------------------------------------------------
33 This class defines both the singleton that manages the managers and the
34 class type of the managers themselves. The singleton interface can be found
35 at the end of the class
37 ---------------------------------------------------------------------------
40 class CAIManager
43 //===================================================================
44 // *** SUB-CLASSES FOR MGR DEFINITON DATA TREE ***
45 //===================================================================
47 public:
48 struct SMgrDfnElm
50 SMgrDfnElm() {}
51 SMgrDfnElm(uint64 action,const std::vector <CAIActions::CArg> &args): Action(action)
53 Args.resize(args.size());
54 for (uint i=0;i<args.size();++i)
55 Args[i]=args[i];
57 SMgrDfnElm(const SMgrDfnElm &other): Action(other.Action), Args(other.Args) {}
59 void serial(NLMISC::IStream &f)
61 //f.xmlPushBegin("CMD");
62 std::string s=getAction();
63 //f.xmlSetAttrib("Action");
64 f.serial(s);
65 setAction(s);
66 //f.xmlPushEnd();
67 f.serialCont(Args);
68 //f.xmlPop();
71 void serialToString(std::string &s) throw()
73 s+=char(Args.size());
74 s+=NLMISC::toString("%*s",sizeof(uint64),"");
75 ((uint64*)&(s[s.size()]))[-1]=Action;
76 for (uint i=0;i<Args.size();++i)
77 Args[i].serialToString(s);
80 std::string getAction()
82 std::string s;
83 for (uint i=0;i<8 && ((char *)&Action)[i];++i) s+=((char *)&Action)[i];
84 return s;
87 void setAction(std::string action)
89 Action=0;
90 for (uint i=0;i<8 && action[i];++i) ((char *)&Action)[i]=action[i];
93 uint64 Action;
94 std::vector <CAIActions::CArg> Args;
97 struct SMgrDfnNode : public NLMISC::CRefCount
99 SMgrDfnNode() {}
100 SMgrDfnNode(const std::string &name)
101 : Name(name)
103 SMgrDfnNode(const SMgrDfnNode &other)
104 : Name(other.Name),
105 Data(other.Data),
106 Child(other.Child)
109 void serial(NLMISC::IStream &f)
111 f.serial(Name);
112 f.serialCont(Data);
114 uint32 count;
115 f.serial(count);
116 Child.resize(count);
117 if (f.isReading())
119 for (uint i=0; i<count; ++i)
121 Child[i] = new SMgrDfnNode;
122 f.serial(*Child[i]);
125 else
127 for (uint i=0; i<count; ++i)
129 f.serial(*Child[i]);
134 void serialToString(std::string &s) throw()
136 uint i;
137 for (i=0;i<Data.size();++i)
138 Data[i].serialToString(s);
139 for (i=0;i<Child.size();++i)
140 Child[i]->serialToString(s);
143 std::string Name;
144 std::vector <SMgrDfnElm> Data;
145 std::vector <NLMISC::CSmartPtr<SMgrDfnNode> > Child;
147 bool Visited; // used to identify modified nodes in file re-parse operations
151 //===================================================================
152 // *** START OF THE INSTANTIATED CLASS ***
153 //===================================================================
156 public:
157 //---------------------------------------------------
158 // INSTANTIATED CLASS: Public methods
160 //-------------
161 // a few read accessors (static properties)
163 // the manager id (0..255)
164 sint id() const;
165 // the manager name .. ie the source file name minus extension
166 const std::string &name() const;
168 // the CPU load rating of the manager for auto-load ballancing purposes
169 uint weightCPU() const;
170 // the RAM load rating of the manager for auto-load ballancing purposes
171 uint weightRAM() const;
174 //-------------
175 // a few read accessors (state of the files on disk)
177 // indicates whether newer source files than object files have been located
178 bool needCompile() const;
179 // indicate whether an object file has been located in the object directory
180 bool objExists() const;
183 //-------------
184 // a few read accessors (relating to assignment to & execution by an ai service)
186 // has the manager been opened (it may still be waiting to be assigned)
187 bool isOpen() const;
188 // has the manager been assigned to a service
189 bool isAssigned() const;
190 // is the manager up and running on the assigned service
191 bool isUp() const;
193 // the id of the service to which the manager is assigned
194 NLNET::TServiceId serviceId() const;
197 //-------------
198 // a few basic actions (relating to disk files)
200 // compile the source files to generate new object files
201 void compile();
202 // delete the object files (but not the save files)
203 void clean();
206 //-------------
207 // a few basic actions (relating to assignment to & execution by an ai service)
209 // open the manager on an unspecified service
210 // (may be queued until a service is available)
211 void open();
212 // assign manager to a specified service and begin execution
213 void assign(NLNET::TServiceId serviceId);
214 // stop execution on the current service and assign to a new service
215 void reassign(NLNET::TServiceId serviceId);
216 // stop execution of a manager
217 void close();
220 //-------------
221 // a few basic actions (miscelaneous)
223 // display information about the state of the manager
224 void display() const;
227 //-------------
228 // a few write accessors (miscelaneous)
230 // set the name assigned to manager
231 // if no name previously assigned then reset all manager properties
232 // if a name already exists and does not match new name then do nohing and return false
233 bool set(const std::string &name);
234 // set the state of the needCompile flag
235 void setNeedCompile(bool val);
236 // set the state of the objFileExists flag
237 void setObjFileExists(bool val);
238 // set the state of the isUp flag
239 void setIsUp(bool val);
240 // set the state of the isOpen flag
241 void setIsOpen(bool val);
244 private:
245 //---------------------------------------------------
246 // INSTANTIATED CLASS: Private methods
248 // default constructor - may only be instantiated by the singleton
249 CAIManager();
250 // reset properties to initial values
251 void _reset();
253 private:
254 //---------------------------------------------------
255 // INSTANTIATED CLASS: Private data
257 // manager name (file name of source file minus extension)
258 std::string _name;
259 // cpu rating for load ballancing
260 uint _weightCPU;
261 // ram rating for load ballancing
262 uint _weightRAM;
263 // do we need to recompile
264 bool _needCompile;
265 // does an object file exist
266 bool _objExists;
267 // is the manager open (at least queued in CAIService singleton)
268 bool _isOpen;
269 // is manager assigned to a service
270 bool _isAssigned;
271 // is manager up and running on the assigned service
272 bool _isUp;
273 // id of the service manager is assigned to
274 NLNET::TServiceId _service;
277 public:
278 //---------------------------------------------------
279 // INSTANTIATED CLASS: Public data
281 // the tree of definition data read from (and written to) data files
282 SMgrDfnNode MgrDfnRootNode;
285 //===================================================================
286 // *** END OF THE INSTANTIATED CLASS *** START OF THE SINGLETON ***
287 //===================================================================
290 public:
291 //---------------------------------------------------
292 // SINGLETON: Public methods
294 // get the number of valid handles (ie the maximum number of managers allowed)
295 static uint maxManagers() { return RYAI_AI_MANAGER_MAX_MANAGERS; }
297 // get the number of allocated managers
298 static uint numManagers();
300 // get a pointer to the manager with given handle (0..maxManagers-1)
301 static CAIManager *getManagerById(sint id);
303 // get a pointer to the manager with given index (0..numManagers-1)
304 static CAIManager *getManagerByIdx(uint idx);
306 // get the handle for the manager of given name and optionally create a new
307 // handle if none found - return -1 if none found or no free slots
308 static sint nameToId(std::string name, bool assignNewIfNotFound=false);
310 // clear file name assignments for managers that aren't currently running on
311 // ai services
312 static void liberateUnassignedManagers();
314 private:
315 //---------------------------------------------------
316 // SINGLETON: Private methods
319 private:
320 //---------------------------------------------------
321 // SINGLETON: Private data
322 static CAIManager _managers[RYAI_AI_MANAGER_MAX_MANAGERS];
325 //===================================================================
326 // *** END OF THE SINGLETON ***
327 //===================================================================
331 #endif