Core.h

Go to the documentation of this file.
00001 #ifndef __CORE_H__
00002 #define __CORE_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include <map>
00011 
00012 #include "Singleton.h"
00013 #include "ServiceBase.h"
00014 #include "Timer.h"
00015 #include "Poller.h"
00016 #include "String.h"
00017 #include "StringList.h"
00018 #include "NamedObject.h"
00019 
00020 // Messages.
00021 static const char *const MSG_CORE_SERVICE_ALREADY_REGISTERED      = "Service '%s' already registered.";
00022 static const char *const MSG_CORE_SERVICE_UNKNOWN                 = "Service '%s' is unknown.";
00023 static const char *const MSG_CORE_SERVICE_CANNOT_SPAWN            = "Cannot spawn service '%s'.";
00024 static const char *const MSG_CORE_SERVICE_CANNOT_FORK             = "Cannot fork service '%s'.";
00025 static const char *const MSG_CORE_SERVICE_NOT_FORKABLE            = "Service '%s' is not forkable.";
00026 static const char *const MSG_CORE_SERVICE_NSTANCES_LIMIT_EXCEEDED = "Instances limit number for service '%s' exceeded. Limit is %u, already runned %u.";
00027 static const char *const MSG_CORE_UNEXPECTED_ID                   = "Unexpected object id %llu.";
00028 
00029 using namespace std;
00030 
00031 typedef ServiceBase *(*Fabrica)(const ValueList &);
00032 
00033 class Core : public Poller,
00034              public Singleton<Core>
00035 {
00036 
00037 friend class Singleton<Core>;
00038 friend class NamedObject;
00039 
00040 public:
00041 
00042   typedef unsigned int (*MethodInstancesNumber)();
00043   typedef String (*MethodManual)();
00044 
00045   static int init();
00046 
00047   int dispatch();
00048   int main();
00049 
00050   int registerService(const String &name, Fabrica fabrica,
00051                       MethodInstancesNumber instancesNumberLimit, MethodInstancesNumber instancesNumber,
00052                       MethodManual manual);
00053 
00054   int spawn(const String &serviceName, const ValueList &params, ServiceBase **instance = NULL);
00055   int spawn(const String &serviceName, ServiceBase **instance = NULL);
00056 
00057   int fork(const ObjectId id, ServiceBase **instance = NULL);
00058 
00059   int kill(ServiceBase *instance);
00060   int kill(const ObjectId id);
00061   int killAll(const String &serviceName);
00062 
00063   int suspend(const ObjectId id);
00064   int suspendAll(const String &serviceName);
00065 
00066   int resume(const ObjectId id);
00067   int resumeAll(const String &serviceName);
00068 
00069   int suspendTimers();
00070   int resumeTimers();
00071 
00072   int notify(ServiceBase *instance, const ValueList &params = ValueList());
00073   int notify(const ObjectId id, const ValueList &params = ValueList());
00074   int notifyAll(const String &serviceName, const ValueList &params = ValueList());
00075 
00076   int quit();
00077 
00078   static int listServices(String &text);
00079   static int listInstances(String &text);
00080 
00081   SIGNAL(idle);
00082 
00083 private:
00084 
00085   typedef struct
00086   {
00087     Fabrica fabrica;
00088     MethodInstancesNumber instancesNumberLimit;
00089     MethodInstancesNumber instancesNumber;
00090     MethodManual manual;
00091   } SpawnInfo;
00092 
00093   typedef map<String, SpawnInfo> ServiceList;
00094 
00095   Core();
00096   virtual ~Core();
00097 
00098   static ServiceList _services;
00099   bool _quit;
00100 };
00101 
00102 inline
00103 Core *CORE()
00104 {
00105   return Core::instance();
00106 }
00107 
00108 inline
00109 String CORE_COLOR_TITLE(const String &str)
00110 {
00111   return COLOR_STRING(str, LIGHT_CYAN, NONE);
00112 }
00113 
00114 template<class T>
00115 int REGISTER_SERVICE()
00116 {
00117   int res;
00118 
00119   if (FAILED(res = CORE()->registerService(T::NAME(), reinterpret_cast<Fabrica>(T::spawn),
00120                                            T::instancesNumberLimit, T::instancesNumber,
00121                                            T::MANUAL)))
00122     return ERROR_BACKTRACE(res);
00123 
00124   return OK;
00125 }
00126 
00127 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1