From 6abe882e8c68bc59d22916a703c341f57a5093e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Mesquita?= Date: Fri, 15 Jan 2010 03:30:11 +0000 Subject: [PATCH] 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 --- fscomm/call.cpp | 1 + fscomm/call.h | 7 +++-- fscomm/fshost.cpp | 65 ++++++++++++++++++++++++------------------- fscomm/fshost.h | 18 ++++++------ fscomm/mainwindow.cpp | 56 +++++++++++++++++-------------------- fscomm/mainwindow.h | 8 +++--- 6 files changed, 81 insertions(+), 74 deletions(-) diff --git a/fscomm/call.cpp b/fscomm/call.cpp index fbb851d2d5..dc7a9c481d 100644 --- a/fscomm/call.cpp +++ b/fscomm/call.cpp @@ -28,6 +28,7 @@ */ #include "call.h" +#include Call::Call() { diff --git a/fscomm/call.h b/fscomm/call.h index 65ca83b379..11edee47f6 100644 --- a/fscomm/call.h +++ b/fscomm/call.h @@ -31,12 +31,12 @@ #include #include -#include 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; diff --git a/fscomm/fshost.cpp b/fscomm/fshost.cpp index d5583e516b..d7a7768232 100644 --- a/fscomm/fshost.cpp +++ b/fscomm/fshost.cpp @@ -29,7 +29,6 @@ #include #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"); + qRegisterMetaType >("QSharedPointer"); } @@ -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 = _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 = _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(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(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"; } diff --git a/fscomm/fshost.h b/fscomm/fshost.h index dff60e33ce..9fbb17533d 100644 --- a/fscomm/fshost.h +++ b/fscomm/fshost.h @@ -31,9 +31,9 @@ #include #include +#include #include - -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 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); + void answered(QSharedPointer); + void newOutgoingCall(QSharedPointer); + void callFailed(QSharedPointer); + void hungup(QSharedPointer); 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 _active_calls; + QHash > _active_calls; QHash _bleg_uuids; }; diff --git a/fscomm/mainwindow.cpp b/fscomm/mainwindow.cpp index b5918840d6..0e96d58a9c 100644 --- a/fscomm/mainwindow.cpp +++ b/fscomm/mainwindow.cpp @@ -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)), this, SLOT(ringing(QSharedPointer))); + connect(&g_FSHost, SIGNAL(answered(QSharedPointer)), this, SLOT(answered(QSharedPointer))); + connect(&g_FSHost, SIGNAL(hungup(QSharedPointer)), this, SLOT(hungup(QSharedPointer))); + connect(&g_FSHost, SIGNAL(newOutgoingCall(QSharedPointer)), this, SLOT(newOutgoingCall(QSharedPointer))); 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 = 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 = 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 = g_FSHost.getCallByUUID(uuid); for (int i=0; ilistCalls->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 = g_FSHost.getCallByUUID(uuid); for (int i=0; ilistCalls->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) { for (int i=0; ilistCalls->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) diff --git a/fscomm/mainwindow.h b/fscomm/mainwindow.h index 4f0420752e..63484a5f3f 100644 --- a/fscomm/mainwindow.h +++ b/fscomm/mainwindow.h @@ -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); + void ringing(QSharedPointer); + void answered(QSharedPointer); + void hungup(QSharedPointer); private: Ui::MainWindow *ui;