Change to start messing with QSharedPointer instead of regular pointers to deal with events correctly. Thanks Math.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16316 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
João Mesquita 2010-01-15 03:30:11 +00:00
parent 21234215f0
commit 6abe882e8c
6 changed files with 81 additions and 74 deletions

View File

@ -28,6 +28,7 @@
*/
#include "call.h"
#include <fshost.h>
Call::Call()
{

View File

@ -31,12 +31,12 @@
#include <QtCore>
#include <QString>
#include <fshost.h>
typedef enum {
FSCOMM_CALL_STATE_RINGING = 0,
FSCOMM_CALL_STATE_TRYING = 1,
FSCOMM_CALL_STATE_ANSWERED = 2
FSCOMM_CALL_STATE_ANSWERED = 2,
FSCOMM_CALL_STATE_FAILED = 3
} fscomm_call_state_t;
typedef enum {
@ -57,11 +57,14 @@ public:
fscomm_call_direction_t getDirection() { return _direction; }
fscomm_call_state_t getState() { return _state; }
void setState(fscomm_call_state_t state) { _state = state; }
void setCause(QString cause) { _cause = cause; }
QString getCause() { return _cause; }
private:
int _call_id;
QString _cid_name;
QString _cid_number;
QString _cause;
fscomm_call_direction_t _direction;
QString _uuid;
QString _buuid;

View File

@ -29,7 +29,6 @@
#include <QtGui>
#include "fshost.h"
#include "call.h"
#include "mod_qsettings/mod_qsettings.h"
/* Declare it globally */
@ -43,7 +42,7 @@ FSHost::FSHost(QObject *parent) :
switch_core_setrlimits();
switch_core_set_globals();
qRegisterMetaType<Call>("Call");
qRegisterMetaType<QSharedPointer<Call> >("QSharedPointer<Call>");
}
@ -160,17 +159,17 @@ void FSHost::run(void)
switch_status_t FSHost::processAlegEvent(switch_event_t * event, QString uuid)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
Call * call = _active_calls.value(uuid);
QSharedPointer<Call> call = _active_calls.value(uuid);
/* Inbound call */
if (call->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
if (call.data()->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
{
switch(event->event_id) {
case SWITCH_EVENT_CHANNEL_ANSWER:
{
call->setbUUID(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
call.data()->setbUUID(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
_bleg_uuids.insert(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"), uuid);
call->setState(FSCOMM_CALL_STATE_ANSWERED);
emit answered(uuid);
call.data()->setState(FSCOMM_CALL_STATE_ANSWERED);
emit answered(call);
break;
}
case SWITCH_EVENT_CHANNEL_HANGUP_COMPLETE:
@ -180,7 +179,7 @@ switch_status_t FSHost::processAlegEvent(switch_event_t * event, QString uuid)
}
case SWITCH_EVENT_CHANNEL_STATE:
{
printf("CHANNEL_STATE Answer-State: %s | Channel-State: %s | %s | %s\n", switch_event_get_header_nil(event, "Answer-State"),switch_event_get_header_nil(event, "Channel-State"), uuid.toAscii().constData(), switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
qDebug() << QString("CHANNEL_STATE Answer-State: %1 | Channel-State: %2 | %3 | %4\n").arg(switch_event_get_header_nil(event, "Answer-State"),switch_event_get_header_nil(event, "Channel-State"), uuid.toAscii().constData(), switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
break;
}
default:
@ -196,21 +195,24 @@ switch_status_t FSHost::processAlegEvent(switch_event_t * event, QString uuid)
{
case SWITCH_EVENT_CHANNEL_BRIDGE:
{
_active_calls.value(uuid)->setbUUID(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
_active_calls.value(uuid).data()->setbUUID(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"));
_bleg_uuids.insert(switch_event_get_header_nil(event, "Other-Leg-Unique-ID"), uuid);
break;
}
case SWITCH_EVENT_CHANNEL_HANGUP_COMPLETE:
{
if (call->getState() == FSCOMM_CALL_STATE_TRYING)
if (call.data()->getState() == FSCOMM_CALL_STATE_TRYING)
{
emit callFailed(uuid);
QString cause = switch_event_get_header_nil(event, "Hangup-Cause");
call.data()->setState(FSCOMM_CALL_STATE_FAILED);
call.data()->setCause(cause);
emit callFailed(call);
_active_calls.take(uuid);
}
break;
}
default:
printf("A leg: %s(%s)\n",switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
qDebug() << QString("A leg: %1(%2)\n").arg(switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
break;
}
}
@ -221,9 +223,9 @@ switch_status_t FSHost::processBlegEvent(switch_event_t * event, QString buuid)
{
QString uuid = _bleg_uuids.value(buuid);
switch_status_t status = SWITCH_STATUS_SUCCESS;
Call * call = _active_calls.value(uuid);
QSharedPointer<Call> call = _active_calls.value(uuid);
/* Inbound call */
if (call->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
if (call.data()->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
{
qDebug() << " Inbound call";
}
@ -234,12 +236,13 @@ switch_status_t FSHost::processBlegEvent(switch_event_t * event, QString buuid)
{
case SWITCH_EVENT_CHANNEL_ANSWER:
{
emit answered(uuid);
emit answered(call);
break;
}
case SWITCH_EVENT_CHANNEL_HANGUP_COMPLETE:
{
emit hungup(_active_calls.take(uuid));
_active_calls.take(uuid);
emit hungup(call);
_bleg_uuids.take(buuid);
break;
}
@ -247,15 +250,15 @@ switch_status_t FSHost::processBlegEvent(switch_event_t * event, QString buuid)
{
if (QString(switch_event_get_header_nil(event, "Answer-State")) == "early")
{
call->setState(FSCOMM_CALL_STATE_RINGING);
emit ringing(uuid);
call.data()->setState(FSCOMM_CALL_STATE_RINGING);
emit ringing(call);
}
//printEventHeaders(event);
break;
}
default:
printf("B leg: %s(%s)\n",switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
qDebug() << QString("B leg: %1(%2)\n").arg(switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
break;
}
}
@ -264,6 +267,7 @@ switch_status_t FSHost::processBlegEvent(switch_event_t * event, QString buuid)
void FSHost::generalEventHandler(switch_event_t *event)
{
/*printEventHeaders(event);*/
QString uuid = switch_event_get_header_nil(event, "Unique-ID");
if (_bleg_uuids.contains(uuid))
@ -287,24 +291,26 @@ void FSHost::generalEventHandler(switch_event_t *event)
{
if (strcmp(event->subclass_name, "portaudio::ringing") == 0 && !_active_calls.contains(uuid))
{
Call *call = new Call(atoi(switch_event_get_header_nil(event, "call_id")),
Call *callPtr = new Call(atoi(switch_event_get_header_nil(event, "call_id")),
switch_event_get_header_nil(event, "Caller-Caller-ID-Name"),
switch_event_get_header_nil(event, "Caller-Caller-ID-Number"),
FSCOMM_CALL_DIRECTION_INBOUND,
uuid);
QSharedPointer<Call> call(callPtr);
_active_calls.insert(uuid, call);
call->setState(FSCOMM_CALL_STATE_RINGING);
emit ringing(uuid);
call.data()->setState(FSCOMM_CALL_STATE_RINGING);
emit ringing(call);
}
else if (strcmp(event->subclass_name, "portaudio::makecall") == 0)
{
Call *call = new Call(atoi(switch_event_get_header_nil(event, "call_id")),NULL,
Call *callPtr = new Call(atoi(switch_event_get_header_nil(event, "call_id")),NULL,
switch_event_get_header_nil(event, "Caller-Destination-Number"),
FSCOMM_CALL_DIRECTION_OUTBOUND,
uuid);
QSharedPointer<Call> call(callPtr);
_active_calls.insert(uuid, call);
call->setState(FSCOMM_CALL_STATE_TRYING);
emit newOutgoingCall(uuid);
call.data()->setState(FSCOMM_CALL_STATE_TRYING);
emit newOutgoingCall(call);
}
else if (strcmp(event->subclass_name, "sofia::gateway_state") == 0)
{
@ -331,7 +337,7 @@ void FSHost::generalEventHandler(switch_event_t *event)
}
else
{
//printf("We got a not treated custom event: %s\n", (!zstr(event->subclass_name) ? event->subclass_name : "NULL"));
//qDebug() << QString("We got a not treated custom event: %1\n").arg(!zstr(event->subclass_name) ? event->subclass_name : "NULL"));
}
break;
}
@ -347,6 +353,7 @@ switch_status_t FSHost::sendCmd(const char *cmd, const char *args, QString *res)
SWITCH_STANDARD_STREAM(stream);
status = switch_api_execute(cmd, args, NULL, &stream);
*res = switch_str_nil((char *) stream.data);
switch_safe_free(stream.data);
return status;
}
@ -354,9 +361,9 @@ switch_status_t FSHost::sendCmd(const char *cmd, const char *args, QString *res)
void FSHost::printEventHeaders(switch_event_t *event)
{
switch_event_header_t *hp;
printf("Received event: %s(%s)\n", switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
qDebug() << QString("Received event: %1(%2)\n").arg(switch_event_name(event->event_id), switch_event_get_header_nil(event, "Event-Subclass"));
for (hp = event->headers; hp; hp = hp->next) {
printf("%s=%s\n", hp->name, hp->value);
qDebug() << hp->name << "=" << hp->value;
}
printf("\n\n");
qDebug() << "\n\n";
}

View File

@ -31,9 +31,9 @@
#include <QThread>
#include <QHash>
#include <QSharedPointer>
#include <switch.h>
class Call;
#include "call.h"
#define FSCOMM_GW_STATE_TRYING 0
#define FSCOMM_GW_STATE_REGISTER 1
@ -65,7 +65,7 @@ public:
explicit FSHost(QObject *parent = 0);
switch_status_t sendCmd(const char *cmd, const char *args, QString *res);
void generalEventHandler(switch_event_t *event);
Call * getCallByUUID(QString uuid) { return _active_calls.value(uuid, NULL); }
QSharedPointer<Call> getCallByUUID(QString uuid) { return _active_calls.value(uuid); }
QString getGwStateName(int id) { return fscomm_gw_state_names[id]; }
protected:
@ -74,11 +74,11 @@ protected:
signals:
void coreLoadingError(QString);
void ready(void);
void ringing(QString);
void answered(QString);
void newOutgoingCall(QString);
void callFailed(QString);
void hungup(Call*);
void ringing(QSharedPointer<Call>);
void answered(QSharedPointer<Call>);
void newOutgoingCall(QSharedPointer<Call>);
void callFailed(QSharedPointer<Call>);
void hungup(QSharedPointer<Call>);
void gwStateChange(QString, int);
private:
@ -86,7 +86,7 @@ private:
switch_status_t processAlegEvent(switch_event_t *, QString);
void createFolders();
void printEventHeaders(switch_event_t *event);
QHash<QString, Call*> _active_calls;
QHash<QString, QSharedPointer<Call> > _active_calls;
QHash<QString, QString> _bleg_uuids;
};

View File

@ -76,10 +76,10 @@ MainWindow::MainWindow(QWidget *parent) :
connect(dialpadMapper, SIGNAL(mapped(QString)), this, SLOT(dialDTMF(QString)));
connect(&g_FSHost, SIGNAL(ready()),this, SLOT(fshostReady()));
connect(&g_FSHost, SIGNAL(ringing(QString)), this, SLOT(ringing(QString)));
connect(&g_FSHost, SIGNAL(answered(QString)), this, SLOT(answered(QString)));
connect(&g_FSHost, SIGNAL(hungup(Call*)), this, SLOT(hungup(Call*)));
connect(&g_FSHost, SIGNAL(newOutgoingCall(QString)), this, SLOT(newOutgoingCall(QString)));
connect(&g_FSHost, SIGNAL(ringing(QSharedPointer<Call>)), this, SLOT(ringing(QSharedPointer<Call>)));
connect(&g_FSHost, SIGNAL(answered(QSharedPointer<Call>)), this, SLOT(answered(QSharedPointer<Call>)));
connect(&g_FSHost, SIGNAL(hungup(QSharedPointer<Call>)), this, SLOT(hungup(QSharedPointer<Call>)));
connect(&g_FSHost, SIGNAL(newOutgoingCall(QSharedPointer<Call>)), this, SLOT(newOutgoingCall(QSharedPointer<Call>)));
connect(&g_FSHost, SIGNAL(gwStateChange(QString,int)), this, SLOT(gwStateChanged(QString,int)));
/*connect(&g_FSHost, SIGNAL(coreLoadingError(QString)), this, SLOT(coreLoadingError(QString)));*/
@ -152,11 +152,11 @@ void MainWindow::dialDTMF(QString dtmf)
void MainWindow::callListDoubleClick(QListWidgetItem *item)
{
Call *call = g_FSHost.getCallByUUID(item->data(Qt::UserRole).toString());
QString switch_str = QString("switch %1").arg(call->getCallID());
QSharedPointer<Call> call = g_FSHost.getCallByUUID(item->data(Qt::UserRole).toString());
QString switch_str = QString("switch %1").arg(call.data()->getCallID());
QString result;
if (g_FSHost.sendCmd("pa", switch_str.toAscii(), &result) == SWITCH_STATUS_FALSE) {
ui->textEdit->setText(QString("Error switching to call %1").arg(call->getCallID()));
ui->textEdit->setText(QString("Error switching to call %1").arg(call.data()->getCallID()));
return;
}
ui->hangupBtn->setEnabled(true);
@ -219,54 +219,51 @@ void MainWindow::paHangup()
ui->hangupBtn->setEnabled(false);
}
void MainWindow::newOutgoingCall(QString uuid)
void MainWindow::newOutgoingCall(QSharedPointer<Call> call)
{
Call *call = g_FSHost.getCallByUUID(uuid);
ui->textEdit->setText(QString("Calling %1 (%2)").arg(call->getCidName(), call->getCidNumber()));
QListWidgetItem *item = new QListWidgetItem(tr("%1 (%2) - Calling").arg(call->getCidName(), call->getCidNumber()));
item->setData(Qt::UserRole, uuid);
ui->textEdit->setText(QString("Calling %1 (%2)").arg(call.data()->getCidName(), call.data()->getCidNumber()));
QListWidgetItem *item = new QListWidgetItem(tr("%1 (%2) - Calling").arg(call.data()->getCidName(), call.data()->getCidNumber()));
item->setData(Qt::UserRole, call.data()->getUUID());
ui->listCalls->addItem(item);
ui->hangupBtn->setEnabled(true);
}
void MainWindow::ringing(QString uuid)
void MainWindow::ringing(QSharedPointer<Call> call)
{
Call *call = g_FSHost.getCallByUUID(uuid);
for (int i=0; i<ui->listCalls->count(); i++)
{
QListWidgetItem *item = ui->listCalls->item(i);
if (item->data(Qt::UserRole).toString() == uuid)
if (item->data(Qt::UserRole).toString() == call.data()->getUUID())
{
item->setText(tr("%1 - Ringing").arg(call->getCidNumber()));
ui->textEdit->setText(QString("Call from %1 (%2)").arg(call->getCidName(), call->getCidNumber()));
item->setText(tr("%1 - Ringing").arg(call.data()->getCidNumber()));
ui->textEdit->setText(QString("Call from %1 (%2)").arg(call.data()->getCidName(), call.data()->getCidNumber()));
return;
}
}
ui->textEdit->setText(QString("Call from %1 (%2)").arg(call->getCidName(), call->getCidNumber()));
QListWidgetItem *item = new QListWidgetItem(tr("%1 (%2) - Ringing").arg(call->getCidName(), call->getCidNumber()));
item->setData(Qt::UserRole, uuid);
ui->textEdit->setText(QString("Call from %1 (%2)").arg(call.data()->getCidName(), call.data()->getCidNumber()));
QListWidgetItem *item = new QListWidgetItem(tr("%1 (%2) - Ringing").arg(call.data()->getCidName(), call.data()->getCidNumber()));
item->setData(Qt::UserRole, call.data()->getUUID());
ui->listCalls->addItem(item);
ui->answerBtn->setEnabled(true);
}
void MainWindow::answered(QString uuid)
void MainWindow::answered(QSharedPointer<Call> call)
{
Call *call = g_FSHost.getCallByUUID(uuid);
for (int i=0; i<ui->listCalls->count(); i++)
{
QListWidgetItem *item = ui->listCalls->item(i);
if (item->data(Qt::UserRole).toString() == uuid)
if (item->data(Qt::UserRole).toString() == call.data()->getUUID())
{
if (call->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
if (call.data()->getDirection() == FSCOMM_CALL_DIRECTION_INBOUND)
{
item->setText(tr("%1 (%2) - Active").arg(call->getCidName(), call->getCidNumber()));
item->setText(tr("%1 (%2) - Active").arg(call.data()->getCidName(), call.data()->getCidNumber()));
break;
}
else
{
item->setText(tr("%1 - Active").arg(call->getCidNumber()));
item->setText(tr("%1 - Active").arg(call.data()->getCidNumber()));
break;
}
}
@ -289,18 +286,18 @@ void MainWindow::answered(QString uuid)
ui->dtmfPoundBtn->setEnabled(true);
}
void MainWindow::hungup(Call* call)
void MainWindow::hungup(QSharedPointer<Call> call)
{
for (int i=0; i<ui->listCalls->count(); i++)
{
QListWidgetItem *item = ui->listCalls->item(i);
if (item->data(Qt::UserRole).toString() == call->getUUID())
if (item->data(Qt::UserRole).toString() == call.data()->getUUID())
{
delete ui->listCalls->takeItem(i);
break;
}
}
ui->textEdit->setText(tr("Call with %1 (%2) hungup.").arg(call->getCidName(), call->getCidNumber()));
ui->textEdit->setText(tr("Call with %1 (%2) hungup.").arg(call.data()->getCidName(), call.data()->getCidNumber()));
/* TODO: Will cause problems if 2 calls are received at the same time */
ui->answerBtn->setEnabled(false);
ui->hangupBtn->setEnabled(false);
@ -320,7 +317,6 @@ void MainWindow::hungup(Call* call)
ui->dtmfDBtn->setEnabled(false);
ui->dtmfAstBtn->setEnabled(false);
ui->dtmfPoundBtn->setEnabled(false);
delete call;
}
void MainWindow::changeEvent(QEvent *e)

View File

@ -68,10 +68,10 @@ private slots:
void paAnswer();
void paCall(QString);
void paHangup();
void newOutgoingCall(QString);
void ringing(QString);
void answered(QString);
void hungup(Call*);
void newOutgoingCall(QSharedPointer<Call>);
void ringing(QSharedPointer<Call>);
void answered(QSharedPointer<Call>);
void hungup(QSharedPointer<Call>);
private:
Ui::MainWindow *ui;