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
00055 Configuration(const char* filename);
00056
00060 inline Configuration& operator=(const String& value)
00061 { String::operator=(value); return *this; }
00062
00067 inline unsigned int sections() const
00068 { return m_sections.length(); }
00069
00075 NamedList* getSection(unsigned int index) const;
00076
00082 NamedList* getSection(const String& sect) const;
00083
00090 NamedString* getKey(const String& sect, const String& key) const;
00091
00099 const char* getValue(const String& sect, const String& key, const char* defvalue = 0) const;
00100
00108 int getIntValue(const String& sect, const String& key, int defvalue = 0) const;
00109
00118 int getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue = 0) const;
00119
00127 double getDoubleValue(const String& sect, const String& key, double defvalue = 0.0) const;
00128
00136 bool getBoolValue(const String& sect, const String& key, bool defvalue = false) const;
00137
00142 void clearSection(const char* sect = 0);
00143
00148 inline void createSection(const String& sect)
00149 { if (sect) makeSectHolder(sect); }
00150
00156 void clearKey(const String& sect, const String& key);
00157
00164 void addValue(const String& sect, const char* key, const char* value = 0);
00165
00172 void setValue(const String& sect, const char* key, const char* value = 0);
00173
00180 void setValue(const String& sect, const char* key, int value);
00181
00188 void setValue(const String& sect, const char* key, bool value);
00189
00194 bool load();
00195
00200 bool save() const;
00201
00202 private:
00203 Configuration(const Configuration& value);
00204 Configuration& operator=(const Configuration& value);
00205 ObjList *getSectHolder(const String& sect) const;
00206 ObjList *makeSectHolder(const String& sect);
00207 ObjList m_sections;
00208 };
00209
00210 class MessageDispatcher;
00211
00216 class YATE_API Message : public NamedList
00217 {
00218 friend class MessageDispatcher;
00219 public:
00226 Message(const char* name, const char* retval = 0);
00227
00233 Message(const Message& original);
00234
00238 ~Message();
00239
00245 virtual void* getObject(const String& name) const;
00246
00251 inline String& retValue()
00252 { return m_return; }
00253
00258 inline const String& retValue() const
00259 { return m_return; }
00260
00265 inline RefObject* userData() const
00266 { return m_data; }
00267
00274 void userData(RefObject* data);
00275
00281 inline void* userObject(const String& name) const
00282 { return m_data ? m_data->getObject(name) : 0; }
00283
00284
00290 inline void setNotify(bool notify = true)
00291 { m_notify = notify; }
00292
00297 inline Time& msgTime()
00298 { return m_time; }
00299
00304 inline const Time& msgTime() const
00305 { return m_time; }
00306
00310 inline Message& operator=(const char* value)
00311 { String::operator=(value); return *this; }
00312
00318 String encode(const char* id) const;
00319
00326 String encode(bool received, const char* id) const;
00327
00336 int decode(const char* str, String& id);
00337
00347 int decode(const char* str, bool& received, const char* id);
00348
00349 protected:
00356 virtual void dispatched(bool accepted);
00357
00358 private:
00359 Message();
00360 Message& operator=(const Message& value);
00361 String m_return;
00362 Time m_time;
00363 RefObject* m_data;
00364 bool m_notify;
00365 void commonEncode(String& str) const;
00366 int commonDecode(const char* str, int offs);
00367 };
00368
00375 class YATE_API MessageHandler : public String
00376 {
00377 friend class MessageDispatcher;
00378 public:
00384 MessageHandler(const char* name, unsigned priority = 100);
00385
00389 virtual ~MessageHandler();
00390
00396 virtual bool received(Message& msg) = 0;
00397
00402 inline unsigned priority() const
00403 { return m_priority; }
00404
00408 inline const NamedString* filter() const
00409 { return m_filter; }
00410
00416 void setFilter(NamedString* filter);
00417
00423 inline void setFilter(const char* name, const char* value)
00424 { setFilter(new NamedString(name,value)); }
00425
00429 void clearFilter();
00430
00431 private:
00432 unsigned m_priority;
00433 MessageDispatcher* m_dispatcher;
00434 NamedString* m_filter;
00435 };
00436
00441 class YATE_API MessageReceiver : public GenObject
00442 {
00443 public:
00450 virtual bool received(Message& msg, int id) = 0;
00451 };
00452
00457 class YATE_API MessageRelay : public MessageHandler
00458 {
00459 public:
00467 MessageRelay(const char* name, MessageReceiver* receiver, int id, int priority = 100)
00468 : MessageHandler(name,priority), m_receiver(receiver), m_id(id) { }
00469
00475 virtual bool received(Message& msg)
00476 { return m_receiver ? m_receiver->received(msg,m_id) : false; }
00477
00478 private:
00479 MessageReceiver* m_receiver;
00480 int m_id;
00481 };
00482
00489 class YATE_API MessageNotifier
00490 {
00491 public:
00495 virtual ~MessageNotifier();
00496
00502 virtual void dispatched(const Message& msg, bool handled) = 0;
00503 };
00504
00511 class YATE_API MessagePostHook : public GenObject, public MessageNotifier
00512 {
00513 };
00514
00521 class YATE_API MessageDispatcher : public GenObject
00522 {
00523 public:
00527 MessageDispatcher();
00528
00532 ~MessageDispatcher();
00533
00539 bool install(MessageHandler* handler);
00540
00546 bool uninstall(MessageHandler* handler);
00547
00553 bool dispatch(Message& msg);
00554
00560 bool enqueue(Message* msg);
00561
00565 void dequeue();
00566
00571 bool dequeueOne();
00572
00577 inline void warnTime(u_int64_t usec)
00578 { m_warnTime = usec; }
00579
00583 inline void clear()
00584 { m_handlers.clear(); m_hooks.clear(); }
00585
00590 unsigned int messageCount();
00591
00596 unsigned int handlerCount();
00597
00603 void setHook(MessagePostHook* hook, bool remove = false);
00604
00605 private:
00606 ObjList m_handlers;
00607 ObjList m_messages;
00608 ObjList m_hooks;
00609 Mutex m_mutex;
00610 unsigned int m_changes;
00611 u_int64_t m_warnTime;
00612 };
00613
00624 class YATE_API Plugin : public GenObject
00625 {
00626 public:
00631 Plugin(const char* name);
00632
00637 Plugin();
00638
00644 virtual ~Plugin();
00645
00651 virtual void* getObject(const String& name) const;
00652
00656 virtual void initialize() = 0;
00657
00662 virtual bool isBusy() const
00663 { return false; }
00664 };
00665
00666 #if 0
00667
00671 void INIT_PLUGIN(class pclass);
00672 #endif
00673
00674 #define INIT_PLUGIN(pclass) static pclass __plugin
00675
00682 class YATE_API Engine
00683 {
00684 friend class EnginePrivate;
00685 public:
00689 enum RunMode {
00690 Console = 0,
00691 Client = 1,
00692 Server = 2
00693 };
00694
00704 static int main(int argc, const char** argv, const char** env,
00705 RunMode mode = Console, bool fail = false);
00706
00712 static void help(bool client, bool errout = false);
00713
00718 int run();
00719
00724 static Engine* self();
00725
00732 static bool Register(const Plugin* plugin, bool reg = true);
00733
00740 static String configFile(const char* name, bool user = false);
00741
00746 inline static String& configPath()
00747 { return s_cfgpath; }
00748
00753 inline static String& configSuffix()
00754 { return s_cfgsuffix; }
00755
00759 inline static String& modulePath()
00760 { return s_modpath; }
00761
00766 inline static String& extraPath()
00767 { return s_extramod; }
00768
00773 inline static String& moduleSuffix()
00774 { return s_modsuffix; }
00775
00780 static const char* pathSeparator();
00781
00789 static const Configuration& config();
00790
00795 static unsigned int runId();
00796
00800 static void init();
00801
00806 static void halt(unsigned int code);
00807
00814 static bool restart(unsigned int code, bool gracefull = false);
00815
00820 static bool exiting()
00821 { return (s_haltcode != -1); }
00822
00828 static bool install(MessageHandler* handler);
00829
00835 static bool uninstall(MessageHandler* handler);
00836
00842 static bool enqueue(Message* msg);
00843
00850 inline static bool enqueue(const char* name)
00851 { return (name && *name) ? enqueue(new Message(name)) : false; }
00852
00858 static bool dispatch(Message* msg);
00859
00865 static bool dispatch(Message& msg);
00866
00873 static bool dispatch(const char* name);
00874
00880 inline void setHook(MessagePostHook* hook, bool remove = false)
00881 { m_dispatcher.setHook(hook,remove); }
00882
00887 int usedPlugins();
00888
00893 inline unsigned int messageCount()
00894 { return m_dispatcher.messageCount(); }
00895
00900 inline unsigned int handlerCount()
00901 { return m_dispatcher.handlerCount(); }
00902
00908 bool loadPluginDir(const String& relPath);
00909
00910 protected:
00915 ~Engine();
00916
00923 bool loadPlugin(const char* file, bool local = false);
00924
00928 void loadPlugins();
00929
00933 void initPlugins();
00934
00935 private:
00936 Engine();
00937 ObjList m_libs;
00938 MessageDispatcher m_dispatcher;
00939 static Engine* s_self;
00940 static String s_cfgpath;
00941 static String s_cfgsuffix;
00942 static String s_modpath;
00943 static String s_extramod;
00944 static String s_modsuffix;
00945 static int s_haltcode;
00946 };
00947
00948 };
00949
00950 #endif
00951
00952