From 6404b73e706abb163bea18b0aa92aaf2d50c5574 Mon Sep 17 00:00:00 2001 From: paulc Date: Wed, 16 Jun 2010 10:46:22 +0000 Subject: [PATCH] Improved regexroute by allowing multiple extra handlers with different priorities and target contexts. A match parameter can be specified for such an extra handler. Original patch (that changed config syntax) by Allan Sandfeld Jensen. git-svn-id: http://voip.null.ro/svn/yate@3382 acf43c95-373e-0410-b603-e72c3f656dc1 --- conf.d/regexroute.conf.sample | 13 ++++++---- modules/regexroute.cpp | 48 +++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/conf.d/regexroute.conf.sample b/conf.d/regexroute.conf.sample index 87f23978..71cc3db0 100644 --- a/conf.d/regexroute.conf.sample +++ b/conf.d/regexroute.conf.sample @@ -94,11 +94,14 @@ [extra] ; This section allows installing handlers for any message name. ; Each line must be of the form: -; message.name=priority -; You can only install one handler for any given message name. -; For each handler create a corresponding [message.name] section in which -; implement handling for that specific message. You will need to match -; parameters explicitely or set a new match string. +; message.name=priority[,[paramname][,context]] +; For each handler create a corresponding [context] or [message.name] section +; in which implement handling for that specific message. If paramname is not +; set you will need to match parameters explicitely or set a new match string. +; Examples: +; engine.command=90 +; call.execute=120,callto +; call.route=200,called,reroute [contexts] diff --git a/modules/regexroute.cpp b/modules/regexroute.cpp index 008f2fac..8f96b42e 100644 --- a/modules/regexroute.cpp +++ b/modules/regexroute.cpp @@ -504,7 +504,8 @@ static bool oneContext(Message &msg, String &str, const String &context, String DDebug("RegexRoute",DebugAll,"Returning false at end of context '%s'", context.c_str()); return false; } - + + bool RouteHandler::received(Message &msg) { u_int64_t tmr = Time::now(); @@ -522,7 +523,8 @@ bool RouteHandler::received(Message &msg) called.c_str(),context,Time::now()-tmr); return false; }; - + + class PrerouteHandler : public MessageHandler { public: @@ -554,29 +556,42 @@ bool PrerouteHandler::received(Message &msg) caller.c_str(),Time::now()-tmr); return false; }; - + + class GenericHandler : public MessageHandler { public: - GenericHandler(const char* name, int prio) - : MessageHandler(name,prio) + GenericHandler(const char* name, int prio, const char* context, const char* match) + : MessageHandler(name,prio), + m_context(context), m_match(match) { - Debug(DebugAll,"Installing generic handler for '%s' prio %d [%p]",c_str(),prio,this); + Debug(DebugAll,"Generic handler for '%s' prio %d to [%s] match '%s%s%s' [%p]", + c_str(),prio,context, + (match ? "${" : ""),(match ? match : c_str()),(match ? "}" : ""), + this); s_extra.append(this); } ~GenericHandler() { s_extra.remove(this,false); } virtual bool received(Message &msg); +private: + String m_context; + String m_match; }; bool GenericHandler::received(Message &msg) { DDebug(DebugAll,"Handling message '%s' [%p]",c_str(),this); - String what(*this); + String what(m_match); + if (what) + what = msg.getValue(what); + else + what = *this; Lock lock(s_mutex); - return oneContext(msg,what,*this,msg.retValue()); + return oneContext(msg,what,m_context,msg.retValue()); } + class RegexRoutePlugin : public Plugin { public: @@ -638,8 +653,21 @@ void RegexRoutePlugin::initialize() unsigned int len = l->length(); for (unsigned int i=0; igetParam(i); - if (n) - Engine::install(new GenericHandler(n->name(),n->toInteger())); + if (n) { + // message=prio[:[context][:[parameter]]] + ObjList* o = n->split(','); + const String* s = static_cast(o->at(0)); + int prio = s ? s->toInteger(100) : 100; + const char* match = TelEngine::c_str(static_cast(o->at(1))); + const char* context = TelEngine::c_str(static_cast(o->at(2))); + if (TelEngine::null(context)) + context = n->name().c_str(); + if (s_cfg.getSection(context)) + Engine::install(new GenericHandler(n->name(),prio,context,match)); + else + Debug(DebugWarn,"Missing context [%s] for handling %s",context,n->name().c_str()); + TelEngine::destruct(o); + } } } }