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
00394 virtual void destruct();
00395
00401 virtual bool received(Message& msg) = 0;
00402
00407 inline unsigned priority() const
00408 { return m_priority; }
00409
00413 inline const NamedString* filter() const
00414 { return m_filter; }
00415
00421 void setFilter(NamedString* filter);
00422
00428 inline void setFilter(const char* name, const char* value)
00429 { setFilter(new NamedString(name,value)); }
00430
00434 void clearFilter();
00435
00436 private:
00437 void cleanup();
00438 unsigned m_priority;
00439 MessageDispatcher* m_dispatcher;
00440 NamedString* m_filter;
00441 };
00442
00447 class YATE_API MessageReceiver : public GenObject
00448 {
00449 public:
00456 virtual bool received(Message& msg, int id) = 0;
00457 };
00458
00463 class YATE_API MessageRelay : public MessageHandler
00464 {
00465 public:
00473 MessageRelay(const char* name, MessageReceiver* receiver, int id, int priority = 100)
00474 : MessageHandler(name,priority), m_receiver(receiver), m_id(id) { }
00475
00481 virtual bool received(Message& msg)
00482 { return m_receiver ? m_receiver->received(msg,m_id) : false; }
00483
00484 private:
00485 MessageReceiver* m_receiver;
00486 int m_id;
00487 };
00488
00495 class YATE_API MessageNotifier
00496 {
00497 public:
00501 virtual ~MessageNotifier();
00502
00508 virtual void dispatched(const Message& msg, bool handled) = 0;
00509 };
00510
00517 class YATE_API MessagePostHook : public GenObject, public MessageNotifier
00518 {
00519 };
00520
00527 class YATE_API MessageDispatcher : public GenObject
00528 {
00529 public:
00533 MessageDispatcher();
00534
00538 ~MessageDispatcher();
00539
00545 bool install(MessageHandler* handler);
00546
00552 bool uninstall(MessageHandler* handler);
00553
00559 bool dispatch(Message& msg);
00560
00566 bool enqueue(Message* msg);
00567
00571 void dequeue();
00572
00577 bool dequeueOne();
00578
00583 inline void warnTime(u_int64_t usec)
00584 { m_warnTime = usec; }
00585
00589 inline void clear()
00590 { m_handlers.clear(); m_hooks.clear(); }
00591
00596 unsigned int messageCount();
00597
00602 unsigned int handlerCount();
00603
00609 void setHook(MessagePostHook* hook, bool remove = false);
00610
00611 private:
00612 ObjList m_handlers;
00613 ObjList m_messages;
00614 ObjList m_hooks;
00615 Mutex m_mutex;
00616 unsigned int m_changes;
00617 u_int64_t m_warnTime;
00618 };
00619
00630 class YATE_API Plugin : public GenObject
00631 {
00632 public:
00637 Plugin(const char* name);
00638
00643 Plugin();
00644
00650 virtual ~Plugin();
00651
00657 virtual void* getObject(const String& name) const;
00658
00662 virtual void initialize() = 0;
00663
00668 virtual bool isBusy() const
00669 { return false; }
00670 };
00671
00672 #if 0
00673
00677 void INIT_PLUGIN(class pclass);
00678 #endif
00679
00680 #define INIT_PLUGIN(pclass) static pclass __plugin
00681
00688 class YATE_API Engine
00689 {
00690 friend class EnginePrivate;
00691 public:
00695 enum RunMode {
00696 Stopped = 0,
00697 Console = 1,
00698 Client = 2,
00699 Server = 3,
00700 };
00701
00711 static int main(int argc, const char** argv, const char** env,
00712 RunMode mode = Console, bool fail = false);
00713
00719 static void help(bool client, bool errout = false);
00720
00725 int run();
00726
00731 static Engine* self();
00732
00737 static RunMode mode()
00738 { return s_mode; }
00739
00744 inline static bool clientMode()
00745 { return s_mode == Client; }
00746
00753 static bool Register(const Plugin* plugin, bool reg = true);
00754
00761 static String configFile(const char* name, bool user = false);
00762
00767 inline static String& configPath()
00768 { return s_cfgpath; }
00769
00774 inline static String& configSuffix()
00775 { return s_cfgsuffix; }
00776
00780 inline static String& modulePath()
00781 { return s_modpath; }
00782
00788 static void extraPath(const String& path);
00789
00794 inline static String& moduleSuffix()
00795 { return s_modsuffix; }
00796
00801 static const char* pathSeparator();
00802
00810 static const Configuration& config();
00811
00816 static unsigned int runId();
00817
00821 static void init();
00822
00827 static void halt(unsigned int code);
00828
00835 static bool restart(unsigned int code, bool gracefull = false);
00836
00841 static bool exiting()
00842 { return (s_haltcode != -1); }
00843
00849 static bool install(MessageHandler* handler);
00850
00856 static bool uninstall(MessageHandler* handler);
00857
00863 static bool enqueue(Message* msg);
00864
00871 inline static bool enqueue(const char* name)
00872 { return (name && *name) ? enqueue(new Message(name)) : false; }
00873
00879 static bool dispatch(Message* msg);
00880
00886 static bool dispatch(Message& msg);
00887
00894 static bool dispatch(const char* name);
00895
00901 inline void setHook(MessagePostHook* hook, bool remove = false)
00902 { m_dispatcher.setHook(hook,remove); }
00903
00908 int usedPlugins();
00909
00914 inline unsigned int messageCount()
00915 { return m_dispatcher.messageCount(); }
00916
00921 inline unsigned int handlerCount()
00922 { return m_dispatcher.handlerCount(); }
00923
00929 bool loadPluginDir(const String& relPath);
00930
00931 protected:
00936 ~Engine();
00937
00944 bool loadPlugin(const char* file, bool local = false);
00945
00949 void loadPlugins();
00950
00954 void initPlugins();
00955
00956 private:
00957 Engine();
00958 ObjList m_libs;
00959 MessageDispatcher m_dispatcher;
00960 static Engine* s_self;
00961 static String s_cfgpath;
00962 static String s_cfgsuffix;
00963 static String s_modpath;
00964 static String s_modsuffix;
00965 static ObjList s_extramod;
00966 static int s_haltcode;
00967 static RunMode s_mode;
00968 };
00969
00970 };
00971
00972 #endif
00973
00974