2005-03-20 03:11:53 +00:00
|
|
|
/**
|
|
|
|
* Channel.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2005-04-29 22:05:07 +00:00
|
|
|
* Copyright (C) 2004, 2005 Null Team
|
2005-03-20 03:11:53 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "yatephone.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
// this is to protect against two threads trying to (dis)connect a pair
|
|
|
|
// of call endpoints at the same time
|
|
|
|
static Mutex s_mutex(true);
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
CallEndpoint::CallEndpoint(const char* id)
|
2005-07-28 01:37:19 +00:00
|
|
|
: m_peer(0), m_id(id), m_mutex(0)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
2005-03-29 01:50:20 +00:00
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
CallEndpoint::~CallEndpoint()
|
2005-03-29 01:50:20 +00:00
|
|
|
{
|
2005-04-11 21:20:12 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
ObjList* l = m_data.skipNull();
|
|
|
|
for (; l; l=l->skipNext()) {
|
|
|
|
DataEndpoint* e = static_cast<DataEndpoint*>(l->get());
|
|
|
|
Debug(DebugAll,"Endpoint at %p type '%s' refcount=%d",e,e->name().c_str(),e->refcount());
|
|
|
|
}
|
|
|
|
#endif
|
2005-03-20 03:11:53 +00:00
|
|
|
disconnect(true,0);
|
|
|
|
m_data.clear();
|
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
void* CallEndpoint::getObject(const String& name) const
|
2005-04-14 03:14:20 +00:00
|
|
|
{
|
2005-04-25 22:19:54 +00:00
|
|
|
if (name == "CallEndpoint")
|
|
|
|
return const_cast<CallEndpoint*>(this);
|
2005-04-14 03:14:20 +00:00
|
|
|
return RefObject::getObject(name);
|
|
|
|
}
|
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
bool CallEndpoint::connect(CallEndpoint* peer, const char* reason)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
if (!peer) {
|
2005-09-02 16:39:00 +00:00
|
|
|
disconnect(reason);
|
2005-03-20 03:11:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (peer == m_peer)
|
|
|
|
return true;
|
2005-07-01 22:35:06 +00:00
|
|
|
DDebug(DebugAll,"CallEndpoint '%s' connecting peer %p to [%p]",m_id.c_str(),peer,this);
|
2005-03-20 03:11:53 +00:00
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
#if 0
|
|
|
|
if (!s_mutex.lock(5000000)) {
|
|
|
|
Debug(DebugFail,"Call connect failed - deadlock on call endpoint mutex!");
|
|
|
|
Engine::restart(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
ref();
|
2005-09-02 16:39:00 +00:00
|
|
|
disconnect(reason);
|
2005-03-20 03:11:53 +00:00
|
|
|
peer->ref();
|
2005-09-02 16:39:00 +00:00
|
|
|
peer->disconnect(reason);
|
2005-03-20 03:11:53 +00:00
|
|
|
|
2005-04-11 21:20:12 +00:00
|
|
|
ObjList* l = m_data.skipNull();
|
|
|
|
for (; l; l=l->skipNext()) {
|
|
|
|
DataEndpoint* e = static_cast<DataEndpoint*>(l->get());
|
|
|
|
e->connect(peer->getEndpoint(e->name()));
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
m_peer = peer;
|
2005-08-02 02:20:00 +00:00
|
|
|
peer->setPeer(this,reason);
|
|
|
|
connected(reason);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
s_mutex.unlock();
|
|
|
|
#endif
|
2005-03-20 03:11:53 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
bool CallEndpoint::disconnect(bool final, const char* reason)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
if (!m_peer)
|
2005-08-02 02:20:00 +00:00
|
|
|
return false;
|
2005-07-01 22:35:06 +00:00
|
|
|
DDebug(DebugAll,"CallEndpoint '%s' disconnecting peer %p from [%p]",m_id.c_str(),m_peer,this);
|
2005-03-20 03:11:53 +00:00
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
Lock lock(s_mutex,5000000);
|
|
|
|
if (!lock.mutex()) {
|
|
|
|
Debug(DebugFail,"Call disconnect failed - deadlock on call endpoint mutex!");
|
|
|
|
Engine::restart(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
CallEndpoint *temp = m_peer;
|
2005-03-20 03:11:53 +00:00
|
|
|
m_peer = 0;
|
2005-08-02 02:20:00 +00:00
|
|
|
if (!temp)
|
|
|
|
return false;
|
2005-04-11 21:20:12 +00:00
|
|
|
|
|
|
|
ObjList* l = m_data.skipNull();
|
|
|
|
for (; l; l=l->skipNext()) {
|
|
|
|
DataEndpoint* e = static_cast<DataEndpoint*>(l->get());
|
|
|
|
DDebug(DebugAll,"Endpoint at %p type '%s' peer %p",e,e->name().c_str(),e->getPeer());
|
|
|
|
e->disconnect();
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
temp->setPeer(0,reason);
|
2005-08-02 02:20:00 +00:00
|
|
|
if (final)
|
|
|
|
disconnected(true,reason);
|
|
|
|
lock.drop();
|
2005-03-20 03:11:53 +00:00
|
|
|
temp->deref();
|
2005-08-02 02:20:00 +00:00
|
|
|
return deref();
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
void CallEndpoint::setPeer(CallEndpoint* peer, const char* reason)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
m_peer = peer;
|
|
|
|
if (m_peer)
|
2005-08-02 02:20:00 +00:00
|
|
|
connected(reason);
|
2005-03-20 03:11:53 +00:00
|
|
|
else
|
|
|
|
disconnected(false,reason);
|
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
DataEndpoint* CallEndpoint::getEndpoint(const char* type) const
|
|
|
|
{
|
|
|
|
if (null(type))
|
|
|
|
return 0;
|
|
|
|
const ObjList* pos = m_data.find(type);
|
|
|
|
return pos ? static_cast<DataEndpoint*>(pos->get()) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEndpoint* CallEndpoint::setEndpoint(const char* type)
|
|
|
|
{
|
|
|
|
if (null(type))
|
|
|
|
return 0;
|
|
|
|
DataEndpoint* dat = getEndpoint(type);
|
|
|
|
if (!dat) {
|
|
|
|
dat = new DataEndpoint(this,type);
|
|
|
|
if (m_peer)
|
|
|
|
dat->connect(m_peer->getEndpoint(type));
|
|
|
|
}
|
|
|
|
return dat;
|
|
|
|
}
|
|
|
|
|
2005-09-06 02:51:09 +00:00
|
|
|
void CallEndpoint::clearEndpoint(const char* type)
|
|
|
|
{
|
|
|
|
if (null(type)) {
|
|
|
|
ObjList* l = m_data.skipNull();
|
|
|
|
for (; l; l=l->skipNext()) {
|
|
|
|
DataEndpoint* e = static_cast<DataEndpoint*>(l->get());
|
|
|
|
DDebug(DebugAll,"Endpoint at %p type '%s' peer %p",e,e->name().c_str(),e->getPeer());
|
|
|
|
e->disconnect();
|
|
|
|
}
|
|
|
|
m_data.clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DataEndpoint* dat = getEndpoint(type);
|
|
|
|
if (dat) {
|
|
|
|
m_data.remove(dat,false);
|
|
|
|
dat->disconnect();
|
|
|
|
dat->destruct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
void CallEndpoint::setSource(DataSource* source, const char* type)
|
|
|
|
{
|
|
|
|
DataEndpoint* dat = source ? setEndpoint(type) : getEndpoint(type);
|
|
|
|
if (dat)
|
|
|
|
dat->setSource(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource* CallEndpoint::getSource(const char* type) const
|
|
|
|
{
|
|
|
|
DataEndpoint* dat = getEndpoint(type);
|
|
|
|
return dat ? dat->getSource() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallEndpoint::setConsumer(DataConsumer* consumer, const char* type)
|
|
|
|
{
|
|
|
|
DataEndpoint* dat = consumer ? setEndpoint(type) : getEndpoint(type);
|
|
|
|
if (dat)
|
|
|
|
dat->setConsumer(consumer);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataConsumer* CallEndpoint::getConsumer(const char* type) const
|
|
|
|
{
|
|
|
|
DataEndpoint* dat = getEndpoint(type);
|
|
|
|
return dat ? dat->getConsumer() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::Channel(Driver* driver, const char* id, bool outgoing)
|
|
|
|
: CallEndpoint(id),
|
2005-11-04 19:30:47 +00:00
|
|
|
m_driver(driver), m_outgoing(outgoing), m_timeout(0), m_maxcall(0)
|
2005-04-25 22:19:54 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::Channel(Driver& driver, const char* id, bool outgoing)
|
|
|
|
: CallEndpoint(id),
|
2005-11-04 19:30:47 +00:00
|
|
|
m_driver(&driver), m_outgoing(outgoing), m_timeout(0), m_maxcall(0)
|
2005-04-25 22:19:54 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::~Channel()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
Debugger debug(DebugAll,"Channel::~Channel()"," '%s' [%p]",m_id.c_str(),this);
|
|
|
|
#endif
|
2005-08-02 02:20:00 +00:00
|
|
|
cleanup();
|
2005-04-25 22:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* Channel::getObject(const String& name) const
|
|
|
|
{
|
|
|
|
if (name == "Channel")
|
|
|
|
return const_cast<Channel*>(this);
|
|
|
|
return CallEndpoint::getObject(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::init()
|
|
|
|
{
|
|
|
|
status(direction());
|
2005-07-28 01:37:19 +00:00
|
|
|
m_mutex = m_driver;
|
2005-04-25 22:19:54 +00:00
|
|
|
if (m_driver) {
|
2005-08-02 02:20:00 +00:00
|
|
|
m_driver->lock();
|
2005-06-20 20:51:17 +00:00
|
|
|
debugName(m_driver->debugName());
|
2005-04-25 22:19:54 +00:00
|
|
|
debugChain(m_driver);
|
|
|
|
if (m_id.null())
|
|
|
|
m_id << m_driver->prefix() << m_driver->nextid();
|
2005-06-14 20:53:20 +00:00
|
|
|
m_driver->m_total++;
|
2005-04-25 22:19:54 +00:00
|
|
|
m_driver->channels().append(this);
|
|
|
|
m_driver->changed();
|
2005-08-02 02:20:00 +00:00
|
|
|
m_driver->unlock();
|
2005-04-25 22:19:54 +00:00
|
|
|
}
|
2005-06-20 20:51:17 +00:00
|
|
|
DDebug(this,DebugInfo,"Channel::init() '%s' [%p]",m_id.c_str(),this);
|
2005-04-25 22:19:54 +00:00
|
|
|
}
|
|
|
|
|
2005-08-02 02:20:00 +00:00
|
|
|
void Channel::cleanup()
|
|
|
|
{
|
|
|
|
m_timeout = 0;
|
2005-11-04 19:30:47 +00:00
|
|
|
m_maxcall = 0;
|
2005-08-02 02:20:00 +00:00
|
|
|
status("deleted");
|
|
|
|
m_targetid.clear();
|
|
|
|
dropChan();
|
|
|
|
m_driver = 0;
|
|
|
|
m_mutex = 0;
|
|
|
|
}
|
|
|
|
|
2005-07-18 21:47:18 +00:00
|
|
|
void Channel::dropChan()
|
2005-06-14 20:53:20 +00:00
|
|
|
{
|
|
|
|
if (!m_driver)
|
|
|
|
return;
|
2005-08-02 02:20:00 +00:00
|
|
|
m_driver->lock();
|
2005-07-18 21:47:18 +00:00
|
|
|
if (m_driver->channels().remove(this,false))
|
|
|
|
m_driver->changed();
|
2005-08-02 02:20:00 +00:00
|
|
|
m_driver->unlock();
|
2005-07-18 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::zeroRefs()
|
|
|
|
{
|
|
|
|
// remove us from driver's list before calling the destructor
|
|
|
|
dropChan();
|
|
|
|
CallEndpoint::zeroRefs();
|
2005-06-14 20:53:20 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 22:46:59 +00:00
|
|
|
void Channel::disconnected(bool final, const char* reason)
|
|
|
|
{
|
2005-07-20 00:42:10 +00:00
|
|
|
if (final || Engine::exiting())
|
2005-04-28 22:46:59 +00:00
|
|
|
return;
|
|
|
|
// last chance to get reconnected to something
|
|
|
|
Message* m = message("chan.disconnected");
|
|
|
|
m_targetid.clear();
|
|
|
|
// we will remain referenced until the message is destroyed
|
|
|
|
m->userData(this);
|
|
|
|
if (reason)
|
|
|
|
m->setParam("reason",reason);
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
|
|
|
|
2005-04-25 22:19:54 +00:00
|
|
|
const char* Channel::direction() const
|
|
|
|
{
|
|
|
|
return m_outgoing ? "outgoing" : "incoming";
|
|
|
|
}
|
|
|
|
|
2005-11-04 19:30:47 +00:00
|
|
|
void Channel::setMaxcall(const Message* msg)
|
|
|
|
{
|
|
|
|
int tout = msg ? msg->getIntValue("maxcall") : 0;
|
|
|
|
if (tout > 0)
|
|
|
|
maxcall(Time::now() + tout*(u_int64_t)1000);
|
|
|
|
else
|
|
|
|
maxcall(0);
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
void Channel::complete(Message& msg, bool minimal) const
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
msg.setParam("id",m_id);
|
2005-03-25 01:05:44 +00:00
|
|
|
if (m_driver)
|
2005-03-28 22:27:26 +00:00
|
|
|
msg.setParam("module",m_driver->name());
|
|
|
|
|
|
|
|
if (minimal)
|
|
|
|
return;
|
|
|
|
|
2005-03-25 01:05:44 +00:00
|
|
|
if (m_status)
|
|
|
|
msg.setParam("status",m_status);
|
2005-03-28 22:27:26 +00:00
|
|
|
if (m_address)
|
|
|
|
msg.setParam("address",m_address);
|
2005-03-20 03:11:53 +00:00
|
|
|
if (m_targetid)
|
|
|
|
msg.setParam("targetid",m_targetid);
|
|
|
|
if (m_billid)
|
|
|
|
msg.setParam("billid",m_billid);
|
2005-04-25 22:19:54 +00:00
|
|
|
if (getPeer())
|
|
|
|
msg.setParam("peerid",getPeer()->id());
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
2005-06-14 20:53:20 +00:00
|
|
|
Message* Channel::message(const char* name, bool minimal, bool data)
|
2005-03-28 22:27:26 +00:00
|
|
|
{
|
|
|
|
Message* msg = new Message(name);
|
2005-06-14 20:53:20 +00:00
|
|
|
if (data)
|
|
|
|
msg->userData(this);
|
2005-03-28 22:27:26 +00:00
|
|
|
complete(*msg,minimal);
|
|
|
|
return msg;
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
2005-04-23 23:52:08 +00:00
|
|
|
bool Channel::startRouter(Message* msg)
|
2005-04-09 22:10:00 +00:00
|
|
|
{
|
|
|
|
if (!msg)
|
|
|
|
return false;
|
|
|
|
if (m_driver) {
|
|
|
|
Router* r = new Router(m_driver,m_id,msg);
|
|
|
|
if (r->startup())
|
|
|
|
return true;
|
|
|
|
delete r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete msg;
|
2005-07-12 16:05:29 +00:00
|
|
|
callRejected("failure","Internal server error");
|
2005-04-23 23:52:08 +00:00
|
|
|
// dereference and die if the channel is dynamic
|
|
|
|
if (m_driver && m_driver->varchan())
|
|
|
|
deref();
|
2005-04-09 22:10:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-12 16:05:29 +00:00
|
|
|
bool Channel::msgProgress(Message& msg)
|
|
|
|
{
|
|
|
|
status("progressing");
|
|
|
|
if (m_billid.null())
|
|
|
|
m_billid = msg.getValue("billid");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool Channel::msgRinging(Message& msg)
|
|
|
|
{
|
|
|
|
status("ringing");
|
2005-05-10 14:54:04 +00:00
|
|
|
if (m_billid.null())
|
|
|
|
m_billid = msg.getValue("billid");
|
2005-04-29 21:07:41 +00:00
|
|
|
return true;
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Channel::msgAnswered(Message& msg)
|
|
|
|
{
|
2005-11-04 19:30:47 +00:00
|
|
|
m_maxcall = 0;
|
2005-03-20 03:11:53 +00:00
|
|
|
status("answered");
|
2005-05-10 14:54:04 +00:00
|
|
|
if (m_billid.null())
|
|
|
|
m_billid = msg.getValue("billid");
|
2005-04-29 21:07:41 +00:00
|
|
|
return true;
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Channel::msgTone(Message& msg, const char* tone)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Channel::msgText(Message& msg, const char* text)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-09 22:10:00 +00:00
|
|
|
bool Channel::msgDrop(Message& msg, const char* reason)
|
|
|
|
{
|
2005-11-04 19:30:47 +00:00
|
|
|
m_timeout = m_maxcall = 0;
|
|
|
|
status(null(reason) ? "dropped" : reason);
|
2005-04-29 21:07:41 +00:00
|
|
|
disconnect(reason);
|
|
|
|
return true;
|
2005-04-09 22:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Channel::msgTransfer(Message& msg)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-14 20:03:38 +00:00
|
|
|
bool Channel::callRouted(Message& msg)
|
2005-04-29 21:07:41 +00:00
|
|
|
{
|
|
|
|
status("routed");
|
2005-05-10 14:54:04 +00:00
|
|
|
if (m_billid.null())
|
|
|
|
m_billid = msg.getValue("billid");
|
2005-05-14 20:03:38 +00:00
|
|
|
return true;
|
2005-04-29 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
void Channel::callAccept(Message& msg)
|
|
|
|
{
|
|
|
|
status("accepted");
|
2005-04-28 22:46:59 +00:00
|
|
|
int tout = msg.getIntValue("timeout", m_driver ? m_driver->timeout() : 0);
|
|
|
|
if (tout > 0)
|
|
|
|
timeout(Time::now() + tout*(u_int64_t)1000);
|
2005-05-10 14:54:04 +00:00
|
|
|
if (m_billid.null())
|
|
|
|
m_billid = msg.getValue("billid");
|
2005-04-09 22:10:00 +00:00
|
|
|
m_targetid = msg.getValue("targetid");
|
2005-09-02 16:39:00 +00:00
|
|
|
if (msg.getBoolValue("autoanswer"))
|
|
|
|
msgAnswered(msg);
|
|
|
|
else if (msg.getBoolValue("autoring"))
|
|
|
|
msgRinging(msg);
|
|
|
|
else if (m_targetid.null() && msg.getBoolValue("autoanswer",true)) {
|
|
|
|
// no preference exists in the message so issue a notice
|
|
|
|
Debug(this,DebugNote,"Answering now call %s because we have no targetid [%p]",
|
|
|
|
m_id.c_str(),this);
|
2005-04-09 22:10:00 +00:00
|
|
|
msgAnswered(msg);
|
|
|
|
}
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
2005-07-12 16:05:29 +00:00
|
|
|
void Channel::callRejected(const char* error, const char* reason, const Message* msg)
|
2005-03-28 22:27:26 +00:00
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
Debug(this,DebugMild,"Call rejected error='%s' reason='%s' [%p]",error,reason,this);
|
2005-03-28 22:27:26 +00:00
|
|
|
status("rejected");
|
|
|
|
}
|
|
|
|
|
2005-11-09 22:12:28 +00:00
|
|
|
bool Channel::dtmfInband(const char* tone)
|
|
|
|
{
|
|
|
|
if (null(tone))
|
|
|
|
return false;
|
|
|
|
Message m("chan.attach");
|
|
|
|
complete(m,true);
|
|
|
|
m.userData(this);
|
|
|
|
String tmp("tone/dtmfstr/");
|
|
|
|
tmp += tone;
|
|
|
|
m.setParam("override",tmp);
|
|
|
|
m.setParam("single","yes");
|
|
|
|
return Engine::dispatch(m);
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
bool Channel::setDebug(Message& msg)
|
|
|
|
{
|
|
|
|
String str = msg.getValue("line");
|
|
|
|
if (str.startSkip("level")) {
|
|
|
|
int dbg = debugLevel();
|
|
|
|
str >> dbg;
|
|
|
|
debugLevel(dbg);
|
|
|
|
}
|
|
|
|
else if (str == "reset")
|
|
|
|
debugChain(m_driver);
|
2005-06-20 20:51:17 +00:00
|
|
|
else if (str == "engine")
|
|
|
|
debugCopy();
|
|
|
|
else if (str.isBoolean())
|
|
|
|
debugEnabled(str.toBoolean(debugEnabled()));
|
2005-03-28 22:27:26 +00:00
|
|
|
msg.retValue() << "Channel " << m_id
|
|
|
|
<< " debug " << (debugEnabled() ? "on" : "off")
|
2005-06-20 20:51:17 +00:00
|
|
|
<< " level " << debugLevel() << (debugChained() ? " chained" : "") << "\n";
|
2005-03-28 22:27:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-07 20:31:22 +00:00
|
|
|
TokenDict Module::s_messages[] = {
|
|
|
|
{ "engine.status", Module::Status },
|
|
|
|
{ "engine.timer", Module::Timer },
|
2005-05-06 18:13:33 +00:00
|
|
|
{ "engine.debug", Module::Level },
|
2005-04-07 20:31:22 +00:00
|
|
|
{ "engine.command", Module::Command },
|
|
|
|
{ "engine.help", Module::Help },
|
2005-04-19 00:45:40 +00:00
|
|
|
{ "engine.halt", Module::Halt },
|
2005-04-07 20:31:22 +00:00
|
|
|
{ "call.execute", Module::Execute },
|
|
|
|
{ "call.drop", Module::Drop },
|
2005-07-12 16:05:29 +00:00
|
|
|
{ "call.progress", Module::Progress },
|
2005-04-07 20:31:22 +00:00
|
|
|
{ "call.ringing", Module::Ringing },
|
|
|
|
{ "call.answered", Module::Answered },
|
|
|
|
{ "chan.dtmf", Module::Tone },
|
|
|
|
{ "chan.text", Module::Text },
|
|
|
|
{ "chan.masquerade", Module::Masquerade },
|
|
|
|
{ "chan.locate", Module::Locate },
|
2005-04-09 22:10:00 +00:00
|
|
|
{ "chan.transfer", Module::Transfer },
|
2005-04-07 20:31:22 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2005-04-23 23:52:08 +00:00
|
|
|
unsigned int Module::s_delay = 5;
|
2005-03-28 22:27:26 +00:00
|
|
|
|
2005-04-07 20:31:22 +00:00
|
|
|
const char* Module::messageName(int id)
|
|
|
|
{
|
|
|
|
if ((id <= 0) || (id >PubLast))
|
|
|
|
return 0;
|
|
|
|
return lookup(id,s_messages);
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
Module::Module(const char* name, const char* type)
|
|
|
|
: Plugin(name), Mutex(true),
|
2005-04-07 20:31:22 +00:00
|
|
|
m_init(false), m_relays(0), m_name(name), m_type(type), m_changed(0)
|
|
|
|
{
|
2005-06-20 20:51:17 +00:00
|
|
|
debugName(m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module()
|
|
|
|
{
|
|
|
|
debugName(0);
|
2005-04-07 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2005-04-14 03:14:20 +00:00
|
|
|
void* Module::getObject(const String& name) const
|
|
|
|
{
|
|
|
|
if (name == "Module")
|
|
|
|
return const_cast<Module*>(this);
|
|
|
|
return Plugin::getObject(name);
|
|
|
|
}
|
|
|
|
|
2005-04-07 20:31:22 +00:00
|
|
|
bool Module::installRelay(const char* name, int id, unsigned priority)
|
|
|
|
{
|
|
|
|
if (!(id && name))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Lock lock(this);
|
|
|
|
if (m_relays & id)
|
|
|
|
return true;
|
|
|
|
m_relays |= id;
|
|
|
|
|
|
|
|
Engine::install(new MessageRelay(name,this,id,priority));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::installRelay(int id, unsigned priority)
|
|
|
|
{
|
|
|
|
return installRelay(messageName(id),id,priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::installRelay(const char* name, unsigned priority)
|
2005-03-28 22:27:26 +00:00
|
|
|
{
|
2005-04-07 20:31:22 +00:00
|
|
|
return installRelay(name,lookup(name,s_messages),priority);
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
2005-03-29 01:50:20 +00:00
|
|
|
void Module::initialize()
|
|
|
|
{
|
|
|
|
setup();
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
void Module::setup()
|
|
|
|
{
|
2005-07-01 22:35:06 +00:00
|
|
|
DDebug(this,DebugAll,"Module::setup()");
|
2005-03-28 22:27:26 +00:00
|
|
|
if (m_init)
|
|
|
|
return;
|
|
|
|
m_init = true;
|
2005-05-06 18:13:33 +00:00
|
|
|
installRelay(Timer,90);
|
|
|
|
installRelay(Status,110);
|
|
|
|
installRelay(Level,120);
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::changed()
|
|
|
|
{
|
|
|
|
if (s_delay && !m_changed)
|
2005-04-11 21:20:12 +00:00
|
|
|
m_changed = Time::now() + s_delay*(u_int64_t)1000000;
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::msgTimer(Message& msg)
|
|
|
|
{
|
|
|
|
if (m_changed && (msg.msgTime() > m_changed)) {
|
|
|
|
Message* m = new Message("module.update");
|
|
|
|
m->addParam("module",m_name);
|
|
|
|
m_changed = 0;
|
|
|
|
genUpdate(*m);
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::msgStatus(Message& msg)
|
|
|
|
{
|
|
|
|
String mod, par;
|
|
|
|
lock();
|
|
|
|
statusModule(mod);
|
|
|
|
statusParams(par);
|
|
|
|
unlock();
|
|
|
|
msg.retValue() << mod << ";" << par << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::statusModule(String& str)
|
|
|
|
{
|
|
|
|
str.append("name=",",") << m_name;
|
|
|
|
if (m_type)
|
|
|
|
str << ",type=" << m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::statusParams(String& str)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Module::genUpdate(Message& msg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::received(Message &msg, int id)
|
|
|
|
{
|
|
|
|
if (!m_name)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (id == Timer) {
|
|
|
|
lock();
|
|
|
|
msgTimer(msg);
|
|
|
|
unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String dest = msg.getValue("module");
|
|
|
|
|
|
|
|
if (id == Status) {
|
|
|
|
if (dest == m_name) {
|
|
|
|
msgStatus(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (dest.null() || (dest == m_type))
|
|
|
|
msgStatus(msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (id == Level)
|
|
|
|
return setDebug(msg,dest);
|
|
|
|
else
|
2005-06-20 20:51:17 +00:00
|
|
|
Debug(this,DebugGoOn,"Invalid relay id %d in module '%s', message '%s'",
|
2005-03-28 22:27:26 +00:00
|
|
|
id,m_name.c_str(),msg.c_str());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Module::setDebug(Message& msg, const String& target)
|
|
|
|
{
|
|
|
|
if (target != m_name)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String str = msg.getValue("line");
|
|
|
|
if (str.startSkip("level")) {
|
|
|
|
int dbg = debugLevel();
|
|
|
|
str >> dbg;
|
|
|
|
debugLevel(dbg);
|
|
|
|
}
|
|
|
|
else if (str == "reset") {
|
|
|
|
debugLevel(TelEngine::debugLevel());
|
|
|
|
debugEnabled(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bool dbg = debugEnabled();
|
|
|
|
str >> dbg;
|
|
|
|
debugEnabled(dbg);
|
|
|
|
}
|
|
|
|
msg.retValue() << "Module " << m_name
|
|
|
|
<< " debug " << (debugEnabled() ? "on" : "off")
|
|
|
|
<< " level " << debugLevel() << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
Driver::Driver(const char* name, const char* type)
|
2005-04-23 23:52:08 +00:00
|
|
|
: Module(name,type),
|
|
|
|
m_init(false), m_varchan(true),
|
2005-06-14 20:53:20 +00:00
|
|
|
m_routing(0), m_routed(0), m_total(0),
|
2005-04-29 21:07:41 +00:00
|
|
|
m_nextid(0), m_timeout(0),
|
|
|
|
m_maxroute(0), m_maxchans(0)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
2005-04-20 12:37:59 +00:00
|
|
|
m_prefix << name << "/";
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
2005-04-14 03:14:20 +00:00
|
|
|
void* Driver::getObject(const String& name) const
|
|
|
|
{
|
|
|
|
if (name == "Driver")
|
|
|
|
return const_cast<Driver*>(this);
|
|
|
|
return Module::getObject(name);
|
|
|
|
}
|
|
|
|
|
2005-04-09 22:10:00 +00:00
|
|
|
void Driver::initialize()
|
|
|
|
{
|
|
|
|
setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Driver::setup(const char* prefix, bool minimal)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
2005-06-20 20:51:17 +00:00
|
|
|
DDebug(this,DebugAll,"Driver::setup('%s',%d)",prefix,minimal);
|
2005-03-28 22:27:26 +00:00
|
|
|
Module::setup();
|
2005-03-20 03:11:53 +00:00
|
|
|
if (m_init)
|
|
|
|
return;
|
|
|
|
m_init = true;
|
2005-03-28 22:27:26 +00:00
|
|
|
m_prefix = prefix ? prefix : name().c_str();
|
|
|
|
if (m_prefix && !m_prefix.endsWith("/"))
|
|
|
|
m_prefix += "/";
|
2005-05-03 18:27:14 +00:00
|
|
|
XDebug(DebugAll,"setup name='%s' prefix='%s'",name().c_str(),m_prefix.c_str());
|
2005-04-28 22:46:59 +00:00
|
|
|
timeout(Engine::config().getIntValue("telephony","timeout"));
|
2005-04-29 21:07:41 +00:00
|
|
|
maxRoute(Engine::config().getIntValue("telephony","maxroute"));
|
|
|
|
maxChans(Engine::config().getIntValue("telephony","maxchans"));
|
2005-04-07 20:31:22 +00:00
|
|
|
installRelay(Masquerade,10);
|
2005-04-28 22:46:59 +00:00
|
|
|
installRelay(Locate,40);
|
|
|
|
installRelay(Drop,60);
|
|
|
|
installRelay(Execute,90);
|
2005-04-09 22:10:00 +00:00
|
|
|
if (minimal)
|
|
|
|
return;
|
|
|
|
installRelay(Tone);
|
|
|
|
installRelay(Text);
|
|
|
|
installRelay(Ringing);
|
|
|
|
installRelay(Answered);
|
2005-03-28 00:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Driver::isBusy() const
|
|
|
|
{
|
2005-03-28 22:27:26 +00:00
|
|
|
return (m_routing || m_chans.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel* Driver::find(const String& id) const
|
|
|
|
{
|
|
|
|
const ObjList* pos = m_chans.find(id);
|
|
|
|
return pos ? static_cast<Channel*>(pos->get()) : 0;
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Driver::received(Message &msg, int id)
|
|
|
|
{
|
2005-03-25 01:05:44 +00:00
|
|
|
if (!m_prefix)
|
|
|
|
return false;
|
|
|
|
// pick destination depending on message type
|
2005-03-20 03:11:53 +00:00
|
|
|
String dest;
|
|
|
|
switch (id) {
|
2005-03-28 22:27:26 +00:00
|
|
|
case Timer:
|
2005-04-28 22:46:59 +00:00
|
|
|
{
|
|
|
|
// check each channel for timeouts
|
|
|
|
lock();
|
|
|
|
Time t;
|
|
|
|
ObjList* l = &m_chans;
|
|
|
|
while (l) {
|
|
|
|
Channel* c = static_cast<Channel*>(l->get());
|
2005-11-04 19:30:47 +00:00
|
|
|
if (c) {
|
|
|
|
if (c->timeout() && (c->timeout() < t))
|
|
|
|
c->msgDrop(msg,"timeout");
|
|
|
|
else if (c->maxcall() && (c->maxcall() < t))
|
|
|
|
c->msgDrop(msg,"noanswer");
|
2005-04-28 22:46:59 +00:00
|
|
|
}
|
2005-11-04 19:30:47 +00:00
|
|
|
// advance the pointer only if not dropped synchronously
|
|
|
|
if (l->get() == c)
|
|
|
|
l = l->next();
|
2005-04-28 22:46:59 +00:00
|
|
|
}
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
case Status:
|
2005-03-28 22:27:26 +00:00
|
|
|
case Level:
|
|
|
|
return Module::received(msg,id);
|
2005-04-28 22:46:59 +00:00
|
|
|
case Halt:
|
2005-04-29 21:07:41 +00:00
|
|
|
dropAll(msg);
|
|
|
|
return false;
|
2005-03-20 03:11:53 +00:00
|
|
|
case Execute:
|
|
|
|
dest = msg.getValue("callto");
|
|
|
|
break;
|
|
|
|
case Drop:
|
|
|
|
case Masquerade:
|
2005-03-28 22:27:26 +00:00
|
|
|
case Locate:
|
2005-03-20 03:11:53 +00:00
|
|
|
dest = msg.getValue("id");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dest = msg.getValue("targetid");
|
|
|
|
break;
|
|
|
|
}
|
2005-05-03 18:27:14 +00:00
|
|
|
XDebug(DebugAll,"id=%d prefix='%s' dest='%s'",id,m_prefix.c_str(),dest.c_str());
|
2005-03-28 00:58:26 +00:00
|
|
|
|
2005-05-05 21:16:14 +00:00
|
|
|
if (id == Drop) {
|
|
|
|
bool exact = (dest == name());
|
|
|
|
if (exact || dest.null() || (dest == type())) {
|
|
|
|
dropAll(msg);
|
|
|
|
return exact;
|
|
|
|
}
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
// check if the message was for this driver
|
|
|
|
if (!dest.startsWith(m_prefix))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// handle call.execute which should start a new channel
|
2005-04-09 22:10:00 +00:00
|
|
|
if (id == Execute) {
|
2005-08-03 02:02:50 +00:00
|
|
|
if (!canAccept(false))
|
2005-04-29 21:07:41 +00:00
|
|
|
return false;
|
2005-04-09 22:10:00 +00:00
|
|
|
dest.startSkip(m_prefix,false);
|
2005-03-20 03:11:53 +00:00
|
|
|
return msgExecute(msg,dest);
|
2005-04-09 22:10:00 +00:00
|
|
|
}
|
2005-03-20 03:11:53 +00:00
|
|
|
|
|
|
|
Lock lock(this);
|
2005-03-28 22:27:26 +00:00
|
|
|
Channel* chan = find(dest);
|
2005-03-20 03:11:53 +00:00
|
|
|
if (!chan) {
|
2005-06-20 20:51:17 +00:00
|
|
|
DDebug(this,DebugMild,"Could not find channel '%s'",dest.c_str());
|
2005-03-20 03:11:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (id) {
|
2005-07-12 16:05:29 +00:00
|
|
|
case Progress:
|
|
|
|
return chan->isIncoming() && chan->msgProgress(msg);
|
2005-03-20 03:11:53 +00:00
|
|
|
case Ringing:
|
2005-05-07 23:47:37 +00:00
|
|
|
return chan->isIncoming() && chan->msgRinging(msg);
|
2005-03-20 03:11:53 +00:00
|
|
|
case Answered:
|
2005-05-07 23:47:37 +00:00
|
|
|
return chan->isIncoming() && chan->msgAnswered(msg);
|
2005-03-20 03:11:53 +00:00
|
|
|
case Tone:
|
|
|
|
return chan->msgTone(msg,msg.getValue("text"));
|
|
|
|
case Text:
|
|
|
|
return chan->msgText(msg,msg.getValue("text"));
|
|
|
|
case Drop:
|
2005-04-09 22:10:00 +00:00
|
|
|
return chan->msgDrop(msg,msg.getValue("reason"));
|
|
|
|
case Transfer:
|
|
|
|
return chan->msgTransfer(msg);
|
2005-03-20 03:11:53 +00:00
|
|
|
case Masquerade:
|
|
|
|
msg = msg.getValue("message");
|
|
|
|
msg.clearParam("message");
|
|
|
|
msg.userData(chan);
|
2005-09-02 16:39:00 +00:00
|
|
|
chan->complete(msg);
|
2005-03-20 03:11:53 +00:00
|
|
|
return false;
|
2005-03-28 22:27:26 +00:00
|
|
|
case Locate:
|
|
|
|
msg.userData(chan);
|
|
|
|
return true;
|
2005-03-20 03:11:53 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-28 22:46:59 +00:00
|
|
|
void Driver::dropAll(Message &msg)
|
2005-03-20 03:11:53 +00:00
|
|
|
{
|
|
|
|
lock();
|
2005-04-28 22:46:59 +00:00
|
|
|
const char* reason = msg.getValue("reason");
|
|
|
|
ObjList* l = &m_chans;
|
|
|
|
while (l) {
|
|
|
|
Channel* c = static_cast<Channel*>(l->get());
|
|
|
|
if (c) {
|
2005-06-20 20:51:17 +00:00
|
|
|
DDebug(this,DebugAll,"Dropping %s channel %p [%p]",name().c_str(),c,this);
|
2005-04-28 22:46:59 +00:00
|
|
|
c->msgDrop(msg,reason);
|
|
|
|
if (l->get() != c)
|
2005-07-28 01:37:19 +00:00
|
|
|
continue;
|
2005-04-28 22:46:59 +00:00
|
|
|
}
|
|
|
|
l = l->next();
|
|
|
|
}
|
2005-03-20 03:11:53 +00:00
|
|
|
unlock();
|
|
|
|
}
|
2005-03-28 00:58:26 +00:00
|
|
|
|
2005-08-03 02:02:50 +00:00
|
|
|
bool Driver::canAccept(bool routers)
|
2005-04-29 21:07:41 +00:00
|
|
|
{
|
|
|
|
if (Engine::exiting())
|
|
|
|
return false;
|
2005-08-03 02:02:50 +00:00
|
|
|
if (routers && !canRoute())
|
2005-04-29 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
if (m_maxchans) {
|
|
|
|
Lock mylock(this);
|
|
|
|
return ((signed)m_chans.count() < m_maxchans);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-03 02:02:50 +00:00
|
|
|
bool Driver::canRoute()
|
|
|
|
{
|
|
|
|
if (Engine::exiting())
|
|
|
|
return false;
|
|
|
|
if (m_maxroute && (m_routing >= m_maxroute))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
void Driver::genUpdate(Message& msg)
|
|
|
|
{
|
|
|
|
msg.addParam("routed",String(m_routed));
|
|
|
|
msg.addParam("routing",String(m_routing));
|
2005-06-14 20:53:20 +00:00
|
|
|
msg.addParam("total",String(m_total));
|
2005-03-28 22:27:26 +00:00
|
|
|
msg.addParam("chans",String(m_chans.count()));
|
|
|
|
}
|
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
void Driver::msgStatus(Message& msg)
|
|
|
|
{
|
|
|
|
String mod, par, c;
|
|
|
|
lock();
|
|
|
|
statusModule(mod);
|
|
|
|
statusParams(par);
|
|
|
|
statusChannels(c);
|
|
|
|
unlock();
|
|
|
|
msg.retValue() << mod << ";" << par << ";" << c << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Driver::statusModule(String& str)
|
|
|
|
{
|
2005-03-28 22:27:26 +00:00
|
|
|
Module::statusModule(str);
|
2005-06-17 18:34:16 +00:00
|
|
|
str.append("format=Status|Address|Peer",",");
|
2005-03-28 00:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Driver::statusParams(String& str)
|
|
|
|
{
|
2005-03-28 22:27:26 +00:00
|
|
|
Module::statusParams(str);
|
|
|
|
str.append("routed=",",") << m_routed;
|
|
|
|
str << ",routing=" << m_routing;
|
2005-06-14 20:53:20 +00:00
|
|
|
str << ",total=" << m_total;
|
2005-03-28 00:58:26 +00:00
|
|
|
str << ",chans=" << m_chans.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Driver::statusChannels(String& str)
|
|
|
|
{
|
2005-04-08 12:45:19 +00:00
|
|
|
ObjList* l = m_chans.skipNull();
|
|
|
|
for (; l; l=l->skipNext()) {
|
2005-03-28 00:58:26 +00:00
|
|
|
Channel* c = static_cast<Channel*>(l->get());
|
2005-06-17 18:34:16 +00:00
|
|
|
str.append(c->id(),",") << "=" << c->status() << "|" << c->address() << "|" << c->getPeerId();
|
2005-03-28 00:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
bool Driver::setDebug(Message& msg, const String& target)
|
|
|
|
{
|
|
|
|
if (!target.startsWith(m_prefix))
|
|
|
|
return Module::setDebug(msg,target);
|
|
|
|
|
|
|
|
Lock lock(this);
|
|
|
|
Channel* chan = find(target);
|
|
|
|
if (chan)
|
|
|
|
return chan->setDebug(msg);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-03-29 01:50:20 +00:00
|
|
|
unsigned int Driver::nextid()
|
|
|
|
{
|
|
|
|
Lock lock(this);
|
|
|
|
return ++m_nextid;
|
|
|
|
}
|
|
|
|
|
2005-03-28 22:27:26 +00:00
|
|
|
|
|
|
|
Router::Router(Driver* driver, const char* id, Message* msg)
|
2005-04-10 21:42:49 +00:00
|
|
|
: Thread("Call Router"), m_driver(driver), m_id(id), m_msg(msg)
|
2005-03-28 22:27:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Router::run()
|
|
|
|
{
|
|
|
|
if (!(m_driver && m_msg))
|
|
|
|
return;
|
|
|
|
m_driver->lock();
|
|
|
|
m_driver->m_routing++;
|
|
|
|
m_driver->changed();
|
|
|
|
m_driver->unlock();
|
|
|
|
bool ok = route();
|
|
|
|
m_driver->lock();
|
|
|
|
m_driver->m_routing--;
|
|
|
|
if (ok)
|
|
|
|
m_driver->m_routed++;
|
|
|
|
m_driver->changed();
|
|
|
|
m_driver->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Router::route()
|
|
|
|
{
|
2005-07-01 22:35:06 +00:00
|
|
|
DDebug(m_driver,DebugAll,"Routing thread for '%s' [%p]",m_id.c_str(),this);
|
2005-07-01 21:12:16 +00:00
|
|
|
|
|
|
|
String tmp(m_msg->getValue("callto"));
|
2005-07-03 10:51:19 +00:00
|
|
|
bool ok = !tmp.null();
|
2005-07-01 21:12:16 +00:00
|
|
|
if (ok)
|
|
|
|
m_msg->retValue() = tmp;
|
|
|
|
else
|
|
|
|
ok = Engine::dispatch(m_msg);
|
2005-03-28 22:27:26 +00:00
|
|
|
|
|
|
|
m_driver->lock();
|
|
|
|
Channel* chan = m_driver->find(m_id);
|
|
|
|
if (chan) {
|
2005-04-23 23:52:08 +00:00
|
|
|
// this will keep it referenced even if message user data is changed
|
|
|
|
chan->ref();
|
2005-03-28 22:27:26 +00:00
|
|
|
m_msg->userData(chan);
|
|
|
|
}
|
|
|
|
m_driver->unlock();
|
|
|
|
|
|
|
|
if (!chan) {
|
2005-07-01 22:35:06 +00:00
|
|
|
Debug(m_driver,DebugInfo,"Connection '%s' vanished while routing!",m_id.c_str());
|
2005-03-28 22:27:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
2005-06-12 14:40:29 +00:00
|
|
|
if (m_msg->retValue() == "-")
|
2005-07-12 16:05:29 +00:00
|
|
|
chan->callRejected(m_msg->getValue("error","unknown"),
|
|
|
|
m_msg->getValue("reason"),m_msg);
|
2005-06-12 14:48:21 +00:00
|
|
|
else if (m_msg->getIntValue("antiloop",1) <= 0)
|
2005-07-12 16:05:29 +00:00
|
|
|
chan->callRejected(m_msg->getValue("error","looping"),
|
|
|
|
m_msg->getValue("reason","Call is looping"),m_msg);
|
2005-05-14 21:59:07 +00:00
|
|
|
else if (chan->callRouted(*m_msg)) {
|
2005-05-14 20:03:38 +00:00
|
|
|
*m_msg = "call.execute";
|
|
|
|
m_msg->setParam("callto",m_msg->retValue());
|
|
|
|
m_msg->clearParam("error");
|
|
|
|
m_msg->retValue().clear();
|
|
|
|
ok = Engine::dispatch(m_msg);
|
|
|
|
if (ok)
|
|
|
|
chan->callAccept(*m_msg);
|
|
|
|
else
|
2005-07-12 16:05:29 +00:00
|
|
|
chan->callRejected(m_msg->getValue("error","noconn"),
|
|
|
|
m_msg->getValue("reason","Could not connect to target"),m_msg);
|
2005-05-14 20:03:38 +00:00
|
|
|
}
|
2005-03-28 22:27:26 +00:00
|
|
|
}
|
|
|
|
else
|
2005-07-12 16:05:29 +00:00
|
|
|
chan->callRejected(m_msg->getValue("error","noroute"),
|
|
|
|
m_msg->getValue("reason","No route to call target"),m_msg);
|
2005-03-28 22:27:26 +00:00
|
|
|
|
2005-04-23 23:52:08 +00:00
|
|
|
chan->deref();
|
|
|
|
// dereference again if the channel is dynamic
|
|
|
|
if (m_driver->varchan())
|
|
|
|
chan->deref();
|
2005-03-28 22:27:26 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Router::cleanup()
|
|
|
|
{
|
|
|
|
delete m_msg;
|
|
|
|
}
|
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|