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 YNOCOPY(Configuration);
00046 public:
00050 Configuration();
00051
00057 explicit Configuration(const char* filename, bool warn = true);
00058
00062 inline Configuration& operator=(const String& value)
00063 { String::operator=(value); return *this; }
00064
00069 inline unsigned int sections() const
00070 { return m_sections.length(); }
00071
00077 NamedList* getSection(unsigned int index) const;
00078
00084 NamedList* getSection(const String& sect) const;
00085
00092 NamedString* getKey(const String& sect, const String& key) const;
00093
00101 const char* getValue(const String& sect, const String& key, const char* defvalue = 0) const;
00102
00110 int getIntValue(const String& sect, const String& key, int defvalue = 0) const;
00111
00120 int getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue = 0) const;
00121
00129 double getDoubleValue(const String& sect, const String& key, double defvalue = 0.0) const;
00130
00138 bool getBoolValue(const String& sect, const String& key, bool defvalue = false) const;
00139
00144 void clearSection(const char* sect = 0);
00145
00151 NamedList* createSection(const String& sect);
00152
00158 void clearKey(const String& sect, const String& key);
00159
00166 void addValue(const String& sect, const char* key, const char* value = 0);
00167
00174 void setValue(const String& sect, const char* key, const char* value = 0);
00175
00182 void setValue(const String& sect, const char* key, int value);
00183
00190 void setValue(const String& sect, const char* key, bool value);
00191
00197 bool load(bool warn = true);
00198
00203 bool save() const;
00204
00205 private:
00206 ObjList *getSectHolder(const String& sect) const;
00207 ObjList *makeSectHolder(const String& sect);
00208 ObjList m_sections;
00209 };
00210
00211 class MessageDispatcher;
00212 class MessageRelay;
00213
00218 class YATE_API Message : public NamedList
00219 {
00220 friend class MessageDispatcher;
00221 public:
00229 explicit Message(const char* name, const char* retval = 0, bool broadcast = false);
00230
00236 Message(const Message& original);
00237
00244 Message(const Message& original, bool broadcast);
00245
00249 ~Message();
00250
00256 virtual void* getObject(const String& name) const;
00257
00262 inline String& retValue()
00263 { return m_return; }
00264
00269 inline const String& retValue() const
00270 { return m_return; }
00271
00276 inline RefObject* userData() const
00277 { return m_data; }
00278
00285 void userData(RefObject* data);
00286
00292 inline void* userObject(const String& name) const
00293 { return m_data ? m_data->getObject(name) : 0; }
00294
00295
00301 inline void setNotify(bool notify = true)
00302 { m_notify = notify; }
00303
00308 inline bool broadcast() const
00309 { return m_broadcast; }
00310
00315 inline Time& msgTime()
00316 { return m_time; }
00317
00322 inline const Time& msgTime() const
00323 { return m_time; }
00324
00328 inline Message& operator=(const char* value)
00329 { String::operator=(value); return *this; }
00330
00336 String encode(const char* id) const;
00337
00344 String encode(bool received, const char* id) const;
00345
00354 int decode(const char* str, String& id);
00355
00365 int decode(const char* str, bool& received, const char* id);
00366
00367 protected:
00374 virtual void dispatched(bool accepted);
00375
00376 private:
00377 Message();
00378 Message& operator=(const Message& value);
00379 String m_return;
00380 Time m_time;
00381 RefObject* m_data;
00382 bool m_notify;
00383 bool m_broadcast;
00384 void commonEncode(String& str) const;
00385 int commonDecode(const char* str, int offs);
00386 };
00387
00394 class YATE_API MessageHandler : public String
00395 {
00396 friend class MessageDispatcher;
00397 friend class MessageRelay;
00398 YNOCOPY(MessageHandler);
00399 public:
00405 explicit MessageHandler(const char* name, unsigned priority = 100);
00406
00410 virtual ~MessageHandler();
00411
00415 virtual void destruct();
00416
00422 virtual bool received(Message& msg) = 0;
00423
00428 inline unsigned priority() const
00429 { return m_priority; }
00430
00434 inline const NamedString* filter() const
00435 { return m_filter; }
00436
00442 void setFilter(NamedString* filter);
00443
00449 inline void setFilter(const char* name, const char* value)
00450 { setFilter(new NamedString(name,value)); }
00451
00455 void clearFilter();
00456
00457 protected:
00462 void cleanup();
00463
00464 private:
00465 virtual bool receivedInternal(Message& msg);
00466 void safeNow();
00467 unsigned m_priority;
00468 int m_unsafe;
00469 MessageDispatcher* m_dispatcher;
00470 NamedString* m_filter;
00471 };
00472
00477 class YATE_API MessageReceiver : public GenObject
00478 {
00479 public:
00486 virtual bool received(Message& msg, int id) = 0;
00487 };
00488
00493 class YATE_API MessageRelay : public MessageHandler
00494 {
00495 YNOCOPY(MessageRelay);
00496 public:
00504 MessageRelay(const char* name, MessageReceiver* receiver, int id, int priority = 100)
00505 : MessageHandler(name,priority), m_receiver(receiver), m_id(id) { }
00506
00513 virtual bool received(Message& msg)
00514 { return m_receiver && m_receiver->received(msg,m_id); }
00515
00520 inline int id() const
00521 { return m_id; }
00522
00523 private:
00524 virtual bool receivedInternal(Message& msg);
00525 MessageReceiver* m_receiver;
00526 int m_id;
00527 };
00528
00535 class YATE_API MessageNotifier
00536 {
00537 public:
00541 virtual ~MessageNotifier();
00542
00548 virtual void dispatched(const Message& msg, bool handled) = 0;
00549 };
00550
00557 class YATE_API MessagePostHook : public GenObject, public MessageNotifier
00558 {
00559 };
00560
00567 class YATE_API MessageDispatcher : public GenObject, public Mutex
00568 {
00569 YNOCOPY(MessageDispatcher);
00570 public:
00574 MessageDispatcher();
00575
00579 ~MessageDispatcher();
00580
00589 bool install(MessageHandler* handler);
00590
00596 bool uninstall(MessageHandler* handler);
00597
00609 bool dispatch(Message& msg);
00610
00616 bool enqueue(Message* msg);
00617
00621 void dequeue();
00622
00627 bool dequeueOne();
00628
00633 inline void warnTime(u_int64_t usec)
00634 { m_warnTime = usec; }
00635
00639 inline void clear()
00640 { m_handlers.clear(); m_hooks.clear(); }
00641
00646 unsigned int messageCount();
00647
00652 unsigned int handlerCount();
00653
00659 void setHook(MessagePostHook* hook, bool remove = false);
00660
00661 private:
00662 ObjList m_handlers;
00663 ObjList m_messages;
00664 ObjList m_hooks;
00665 unsigned int m_changes;
00666 u_int64_t m_warnTime;
00667 };
00668
00679 class YATE_API Plugin : public GenObject, public DebugEnabler
00680 {
00681 public:
00687 explicit Plugin(const char* name, bool earlyInit = false);
00688
00694 virtual ~Plugin();
00695
00700 virtual const String& toString() const
00701 { return m_name; }
00702
00708 virtual void* getObject(const String& name) const;
00709
00713 virtual void initialize() = 0;
00714
00719 virtual bool isBusy() const
00720 { return false; }
00721
00726 inline const String& name() const
00727 { return m_name; }
00728
00733 bool earlyInit() const
00734 { return m_early; }
00735
00736 private:
00737 Plugin();
00738 String m_name;
00739 bool m_early;
00740 };
00741
00742 #if 0
00743
00747 void INIT_PLUGIN(class pclass);
00748
00754 bool UNLOAD_PLUGIN(bool unloadNow);
00755 #endif
00756
00757 #define INIT_PLUGIN(pclass) static pclass __plugin
00758 #ifdef DISABLE_UNLOAD
00759 #define UNLOAD_PLUGIN(arg) static bool _unused_unload(bool arg)
00760 #else
00761 #ifdef _WINDOWS
00762 #define UNLOAD_PLUGIN(arg) extern "C" __declspec(dllexport) bool _unload(bool arg)
00763 #else
00764 #define UNLOAD_PLUGIN(arg) extern "C" bool _unload(bool arg)
00765 #endif
00766 #endif
00767
00774 class YATE_API EngineCheck
00775 {
00776 public:
00780 virtual ~EngineCheck()
00781 { }
00782
00789 virtual bool check(const ObjList* cmds) = 0;
00790
00795 static void setChecker(EngineCheck* ptr = 0);
00796 };
00797
00804 class YATE_API Engine
00805 {
00806 friend class EnginePrivate;
00807 friend class EngineCommand;
00808 YNOCOPY(Engine);
00809 public:
00813 enum RunMode {
00814 Stopped = 0,
00815 Console = 1,
00816 Server = 2,
00817 Client = 3,
00818 ClientProxy = 4,
00819 };
00820
00821 enum CallAccept {
00822 Accept = 0,
00823 Partial = 1,
00824 Congestion = 2,
00825 Reject = 3,
00826 };
00827
00834 enum PluginMode {
00835 LoadFail = 0,
00836 LoadLate,
00837 LoadEarly
00838 };
00839
00849 static int main(int argc, const char** argv, const char** env,
00850 RunMode mode = Console, bool fail = false);
00851
00857 static void help(bool client, bool errout = false);
00858
00863 int run();
00864
00869 static Engine* self();
00870
00875 static RunMode mode()
00876 { return s_mode; }
00877
00882 inline static CallAccept accept() {
00883 return s_accept;
00884 }
00885
00890 inline static void setAccept(CallAccept ca) {
00891 s_accept = ca;
00892 }
00893
00898 inline static const TokenDict* getCallAcceptStates() {
00899 return s_callAccept;
00900 }
00901
00906 inline static bool clientMode()
00907 { return (s_mode == Client) || (s_mode == ClientProxy); }
00908
00915 static bool Register(const Plugin* plugin, bool reg = true);
00916
00921 inline static const String& nodeName()
00922 { return s_node; }
00923
00928 inline static const String& sharedPath()
00929 { return s_shrpath; }
00930
00937 static String configFile(const char* name, bool user = false);
00938
00944 static const String& configPath(bool user = false);
00945
00950 inline static const String& configSuffix()
00951 { return s_cfgsuffix; }
00952
00956 inline static const String& modulePath()
00957 { return s_modpath; }
00958
00964 static void extraPath(const String& path);
00965
00972 static void userPath(const String& path);
00973
00978 inline static const String& moduleSuffix()
00979 { return s_modsuffix; }
00980
00985 static const char* pathSeparator();
00986
00994 static const Configuration& config();
00995
01000 static unsigned int runId();
01001
01006 inline static const NamedList& runParams()
01007 { return s_params; }
01008
01012 static void init();
01013
01019 static bool init(const String& name);
01020
01025 static void halt(unsigned int code);
01026
01033 static bool restart(unsigned int code, bool gracefull = false);
01034
01039 static bool exiting()
01040 { return (s_haltcode != -1); }
01041
01047 static bool install(MessageHandler* handler);
01048
01054 static bool uninstall(MessageHandler* handler);
01055
01061 static bool enqueue(Message* msg);
01062
01070 inline static bool enqueue(const char* name, bool broadcast = false)
01071 { return name && *name && enqueue(new Message(name,0,broadcast)); }
01072
01078 static bool dispatch(Message* msg);
01079
01085 static bool dispatch(Message& msg);
01086
01094 static bool dispatch(const char* name, bool broadcast = false);
01095
01101 inline void setHook(MessagePostHook* hook, bool remove = false)
01102 { m_dispatcher.setHook(hook,remove); }
01103
01108 int usedPlugins();
01109
01114 inline unsigned int messageCount()
01115 { return m_dispatcher.messageCount(); }
01116
01121 inline unsigned int handlerCount()
01122 { return m_dispatcher.handlerCount(); }
01123
01129 bool loadPluginDir(const String& relPath);
01130
01135 static void pluginMode(PluginMode mode);
01136
01137 protected:
01142 ~Engine();
01143
01151 bool loadPlugin(const char* file, bool local = false, bool nounload = false);
01152
01156 void loadPlugins();
01157
01161 void initPlugins();
01162
01163 private:
01164 Engine();
01165 ObjList m_libs;
01166 MessageDispatcher m_dispatcher;
01167 static Engine* s_self;
01168 static String s_node;
01169 static String s_shrpath;
01170 static String s_cfgsuffix;
01171 static String s_modpath;
01172 static String s_modsuffix;
01173 static ObjList s_extramod;
01174 static NamedList s_params;
01175 static int s_haltcode;
01176 static RunMode s_mode;
01177 static CallAccept s_accept;
01178 static const TokenDict s_callAccept[];
01179 };
01180
01181 };
01182
01183 #endif
01184
01185