00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __YATENGINE_H
00026 #define __YATENGINE_H
00027
00028 #ifndef __cplusplus
00029 #error C++ is required
00030 #endif
00031
00032 #include <yateclass.h>
00033
00037 namespace TelEngine {
00038
00043 class YATE_API Configuration : public String
00044 {
00045 public:
00049 Configuration();
00050
00056 Configuration(const char* filename, bool warn = true);
00057
00061 inline Configuration& operator=(const String& value)
00062 { String::operator=(value); return *this; }
00063
00068 inline unsigned int sections() const
00069 { return m_sections.length(); }
00070
00076 NamedList* getSection(unsigned int index) const;
00077
00083 NamedList* getSection(const String& sect) const;
00084
00091 NamedString* getKey(const String& sect, const String& key) const;
00092
00100 const char* getValue(const String& sect, const String& key, const char* defvalue = 0) const;
00101
00109 int getIntValue(const String& sect, const String& key, int defvalue = 0) const;
00110
00119 int getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue = 0) const;
00120
00128 double getDoubleValue(const String& sect, const String& key, double defvalue = 0.0) const;
00129
00137 bool getBoolValue(const String& sect, const String& key, bool defvalue = false) const;
00138
00143 void clearSection(const char* sect = 0);
00144
00149 inline void createSection(const String& sect)
00150 { if (sect) makeSectHolder(sect); }
00151
00157 void clearKey(const String& sect, const String& key);
00158
00165 void addValue(const String& sect, const char* key, const char* value = 0);
00166
00173 void setValue(const String& sect, const char* key, const char* value = 0);
00174
00181 void setValue(const String& sect, const char* key, int value);
00182
00189 void setValue(const String& sect, const char* key, bool value);
00190
00196 bool load(bool warn = true);
00197
00202 bool save() const;
00203
00204 private:
00205 Configuration(const Configuration& value);
00206 Configuration& operator=(const Configuration& value);
00207 ObjList *getSectHolder(const String& sect) const;
00208 ObjList *makeSectHolder(const String& sect);
00209 ObjList m_sections;
00210 };
00211
00212 class MessageDispatcher;
00213
00218 class YATE_API Message : public NamedList
00219 {
00220 friend class MessageDispatcher;
00221 public:
00228 Message(const char* name, const char* retval = 0);
00229
00235 Message(const Message& original);
00236
00240 ~Message();
00241
00247 virtual void* getObject(const String& name) const;
00248
00253 inline String& retValue()
00254 { return m_return; }
00255
00260 inline const String& retValue() const
00261 { return m_return; }
00262
00267 inline RefObject* userData() const
00268 { return m_data; }
00269
00276 void userData(RefObject* data);
00277
00283 inline void* userObject(const String& name) const
00284 { return m_data ? m_data->getObject(name) : 0; }
00285
00286
00292 inline void setNotify(bool notify = true)
00293 { m_notify = notify; }
00294
00299 inline Time& msgTime()
00300 { return m_time; }
00301
00306 inline const Time& msgTime() const
00307 { return m_time; }
00308
00312 inline Message& operator=(const char* value)
00313 { String::operator=(value); return *this; }
00314
00320 String encode(const char* id) const;
00321
00328 String encode(bool received, const char* id) const;
00329
00338 int decode(const char* str, String& id);
00339
00349 int decode(const char* str, bool& received, const char* id);
00350
00351 protected:
00358 virtual void dispatched(bool accepted);
00359
00360 private:
00361 Message();
00362 Message& operator=(const Message& value);
00363 String m_return;
00364 Time m_time;
00365 RefObject* m_data;
00366 bool m_notify;
00367 void commonEncode(String& str) const;
00368 int commonDecode(const char* str, int offs);
00369 };
00370
00377 class YATE_API MessageHandler : public String
00378 {
00379 friend class MessageDispatcher;
00380 public:
00386 MessageHandler(const char* name, unsigned priority = 100);
00387
00391 virtual ~MessageHandler();
00392
00396 virtual void destruct();
00397
00403 virtual bool received(Message& msg) = 0;
00404
00409 inline unsigned priority() const
00410 { return m_priority; }
00411
00415 inline const NamedString* filter() const
00416 { return m_filter; }
00417
00423 void setFilter(NamedString* filter);
00424
00430 inline void setFilter(const char* name, const char* value)
00431 { setFilter(new NamedString(name,value)); }
00432
00436 void clearFilter();
00437
00438 private:
00439 void cleanup();
00440 unsigned m_priority;
00441 MessageDispatcher* m_dispatcher;
00442 NamedString* m_filter;
00443 };
00444
00449 class YATE_API MessageReceiver : public GenObject
00450 {
00451 public:
00458 virtual bool received(Message& msg, int id) = 0;
00459 };
00460
00465 class YATE_API MessageRelay : public MessageHandler
00466 {
00467 public:
00475 MessageRelay(const char* name, MessageReceiver* receiver, int id, int priority = 100)
00476 : MessageHandler(name,priority), m_receiver(receiver), m_id(id) { }
00477
00483 virtual bool received(Message& msg)
00484 { return m_receiver ? m_receiver->received(msg,m_id) : false; }
00485
00490 inline int id() const
00491 { return m_id; }
00492
00493 private:
00494 MessageReceiver* m_receiver;
00495 int m_id;
00496 };
00497
00504 class YATE_API MessageNotifier
00505 {
00506 public:
00510 virtual ~MessageNotifier();
00511
00517 virtual void dispatched(const Message& msg, bool handled) = 0;
00518 };
00519
00526 class YATE_API MessagePostHook : public GenObject, public MessageNotifier
00527 {
00528 };
00529
00536 class YATE_API MessageDispatcher : public GenObject
00537 {
00538 public:
00542 MessageDispatcher();
00543
00547 ~MessageDispatcher();
00548
00554 bool install(MessageHandler* handler);
00555
00561 bool uninstall(MessageHandler* handler);
00562
00568 bool dispatch(Message& msg);
00569
00575 bool enqueue(Message* msg);
00576
00580 void dequeue();
00581
00586 bool dequeueOne();
00587
00592 inline void warnTime(u_int64_t usec)
00593 { m_warnTime = usec; }
00594
00598 inline void clear()
00599 { m_handlers.clear(); m_hooks.clear(); }
00600
00605 unsigned int messageCount();
00606
00611 unsigned int handlerCount();
00612
00618 void setHook(MessagePostHook* hook, bool remove = false);
00619
00620 private:
00621 ObjList m_handlers;
00622 ObjList m_messages;
00623 ObjList m_hooks;
00624 Mutex m_mutex;
00625 unsigned int m_changes;
00626 u_int64_t m_warnTime;
00627 };
00628
00639 class YATE_API Plugin : public GenObject
00640 {
00641 public:
00647 Plugin(const char* name, bool earlyInit = false);
00648
00653 Plugin();
00654
00660 virtual ~Plugin();
00661
00667 virtual void* getObject(const String& name) const;
00668
00672 virtual void initialize() = 0;
00673
00678 virtual bool isBusy() const
00679 { return false; }
00680
00685 bool earlyInit() const
00686 { return m_early; }
00687
00688 private:
00689 bool m_early;
00690 };
00691
00692 #if 0
00693
00697 void INIT_PLUGIN(class pclass);
00698
00704 bool UNLOAD_PLUGIN(bool unloadNow);
00705 #endif
00706
00707 #define INIT_PLUGIN(pclass) static pclass __plugin
00708 #ifdef _WINDOWS
00709 #define UNLOAD_PLUGIN(arg) extern "C" __declspec(dllexport) bool _unload(bool arg)
00710 #else
00711 #define UNLOAD_PLUGIN(arg) extern "C" bool _unload(bool arg)
00712 #endif
00713
00720 class YATE_API Engine
00721 {
00722 friend class EnginePrivate;
00723 friend class EngineCommand;
00724 public:
00728 enum RunMode {
00729 Stopped = 0,
00730 Console = 1,
00731 Client = 2,
00732 Server = 3,
00733 };
00734
00741 enum PluginMode {
00742 LoadFail = 0,
00743 LoadLate,
00744 LoadEarly
00745 };
00746
00756 static int main(int argc, const char** argv, const char** env,
00757 RunMode mode = Console, bool fail = false);
00758
00764 static void help(bool client, bool errout = false);
00765
00770 int run();
00771
00776 static Engine* self();
00777
00782 static RunMode mode()
00783 { return s_mode; }
00784
00789 inline static bool clientMode()
00790 { return s_mode == Client; }
00791
00798 static bool Register(const Plugin* plugin, bool reg = true);
00799
00804 inline static const String& nodeName()
00805 { return s_node; }
00806
00811 inline static const String& sharedPath()
00812 { return s_shrpath; }
00813
00820 static String configFile(const char* name, bool user = false);
00821
00826 inline static const String& configPath()
00827 { return s_cfgpath; }
00828
00833 inline static const String& configSuffix()
00834 { return s_cfgsuffix; }
00835
00839 inline static const String& modulePath()
00840 { return s_modpath; }
00841
00847 static void extraPath(const String& path);
00848
00853 inline static const String& moduleSuffix()
00854 { return s_modsuffix; }
00855
00860 static const char* pathSeparator();
00861
00869 static const Configuration& config();
00870
00875 static unsigned int runId();
00876
00881 inline static const NamedList& runParams()
00882 { return s_params; }
00883
00887 static void init();
00888
00893 static void halt(unsigned int code);
00894
00901 static bool restart(unsigned int code, bool gracefull = false);
00902
00907 static bool exiting()
00908 { return (s_haltcode != -1); }
00909
00915 static bool install(MessageHandler* handler);
00916
00922 static bool uninstall(MessageHandler* handler);
00923
00929 static bool enqueue(Message* msg);
00930
00937 inline static bool enqueue(const char* name)
00938 { return (name && *name) ? enqueue(new Message(name)) : false; }
00939
00945 static bool dispatch(Message* msg);
00946
00952 static bool dispatch(Message& msg);
00953
00960 static bool dispatch(const char* name);
00961
00967 inline void setHook(MessagePostHook* hook, bool remove = false)
00968 { m_dispatcher.setHook(hook,remove); }
00969
00974 int usedPlugins();
00975
00980 inline unsigned int messageCount()
00981 { return m_dispatcher.messageCount(); }
00982
00987 inline unsigned int handlerCount()
00988 { return m_dispatcher.handlerCount(); }
00989
00995 bool loadPluginDir(const String& relPath);
00996
01001 static void pluginMode(PluginMode mode);
01002
01003 protected:
01008 ~Engine();
01009
01016 bool loadPlugin(const char* file, bool local = false);
01017
01021 void loadPlugins();
01022
01026 void initPlugins();
01027
01028 private:
01029 Engine();
01030 ObjList m_libs;
01031 MessageDispatcher m_dispatcher;
01032 static Engine* s_self;
01033 static String s_node;
01034 static String s_shrpath;
01035 static String s_cfgpath;
01036 static String s_cfgsuffix;
01037 static String s_modpath;
01038 static String s_modsuffix;
01039 static ObjList s_extramod;
01040 static NamedList s_params;
01041 static int s_haltcode;
01042 static RunMode s_mode;
01043 };
01044
01045 };
01046
01047 #endif
01048
01049