Commit 9564f55a authored by jpbl's avatar jpbl

better connection handling

parent 2dcc14a0
......@@ -18,6 +18,9 @@ PhoneLineManagerImpl::PhoneLineManagerImpl()
{
EventFactory::instance().registerEvent< HangupEvent >("002");
EventFactory::instance().registerEvent< IncommingEvent >("001");
QObject::connect(this, SIGNAL(connected()),
this, SLOT(startSession()));
}
PhoneLineManagerImpl::~PhoneLineManagerImpl()
......@@ -42,6 +45,14 @@ PhoneLineManagerImpl::initialize()
}
}
void
PhoneLineManagerImpl::start()
{
isInitialized();
mSession->connect();
}
void PhoneLineManagerImpl::isInitialized()
{
QMutexLocker guard(&mIsInitializedMutex);
......@@ -50,6 +61,12 @@ void PhoneLineManagerImpl::isInitialized()
}
}
void
PhoneLineManagerImpl::connect()
{
mSession->connect();
}
void
PhoneLineManagerImpl::startSession()
{
......
......@@ -44,6 +44,8 @@ public:
signals:
void unselected(unsigned int);
void selected(unsigned int);
void connected();
void disconnected();
public slots:
/**
......@@ -56,11 +58,18 @@ public slots:
void initialize();
/**
* This will send all the command needed when a
* connection has just been established.
* This function will make the process to start.
* It will connect to the server, and start the
* event handling.
*/
void startSession();
void start();
/**
* This will ask the session to connect
* to the sflphone server.
*/
void connect();
void sendKey(Qt::Key c);
/**
......@@ -135,6 +144,13 @@ public slots:
*/
PhoneLine *selectNextAvailableLine();
private slots:
/**
* This will send all the command needed when a
* connection has just been established.
*/
void startSession();
private:
void isInitialized();
......
......@@ -86,6 +86,18 @@ RequesterImpl::registerSession(const std::string &id,
mSessions.insert(std::make_pair(id, s));
}
void
RequesterImpl::connect(const std::string &id)
{
std::map< std::string, SessionIO * >::iterator pos = mSessions.find(id);
if(pos == mSessions.end()) {
throw std::logic_error("Trying to connect an unknown session.");
}
pos->second->connect();
}
int
RequesterImpl::getCodeCategory(const std::string &code)
{
......
......@@ -79,6 +79,11 @@ class RequesterImpl
*/
void registerSession(const std::string &id, SessionIO *io);
/**
* Will ask the session IO with id to connect.
*/
void connect(const std::string &id);
/**
* This function is used to notify that the SessionIO
* input of a session is down. It means that we no longer
......
......@@ -29,6 +29,7 @@ SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
Requester::instance().registerObject< CallRelatedRequest >(std::string("hold"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("unhold"));
Requester::instance().registerObject< CallRelatedRequest >(std::string("hangup"));
PhoneLineManager::instance().start();
}
void
......@@ -62,4 +63,9 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
&PhoneLineManager::instance(), SLOT(clear()));
QObject::connect(w, SIGNAL(keyPressed(Qt::Key)),
&PhoneLineManager::instance(), SLOT(sendKey(Qt::Key)));
QObject::connect(&PhoneLineManager::instance(), SIGNAL(disconnected()),
w, SLOT(askReconnect()));
QObject::connect(w, SIGNAL(reconnectAsked()),
&PhoneLineManager::instance(), SLOT(connect()));
}
#include "SFLPhoneWindow.hpp"
#include <QLabel>
#include <QMessageBox>
#include <QPixmap>
#include <QKeyEvent>
#include <iostream>
......@@ -96,3 +97,21 @@ SFLPhoneWindow::keyPressEvent(QKeyEvent *e) {
// Misc. key
emit keyPressed(Qt::Key(e->key()));
}
void
SFLPhoneWindow::askReconnect()
{
int ret = QMessageBox::critical(NULL,
tr("SFLPhone disconnected"),
tr("The link between SFLPhone and SFLPhoned is broken.\n"
"Do you want to try to reconnect? If not, the application\n"
"will close."),
QMessageBox::Retry | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Retry) {
emit reconnectAsked();
}
else {
close();
}
}
......@@ -21,6 +21,14 @@ private:
signals:
void keyPressed(Qt::Key);
void reconnectAsked();
public slots:
/**
* This function will prompt a message box, to ask
* if the user want to reconnect to sflphoned.
*/
void askReconnect();
protected:
void keyPressEvent(QKeyEvent *e);
......
......@@ -49,6 +49,13 @@ Session::playTone() const
return Requester::instance().send(mId, "playtone", std::list< std::string >());
}
void
Session::connect() const
{
return Requester::instance().connect(mId);
}
std::string
Session::getEvents() const
{
......
......@@ -49,6 +49,11 @@ class Session
*/
std::string getEvents() const;
/**
* This function will ask to the SessionIO
* linked to this session to connect.
*/
void connect() const;
Call createCall() const;
......
......@@ -35,6 +35,9 @@ class SessionIO : public QObject
public:
virtual ~SessionIO(){}
public slots:
virtual void connect() {}
/**
* You can use this function for sending request.
* The sending is non-blocking. This function will
......
......@@ -18,8 +18,6 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QMessageBox>
#include "globals.h"
#include "Requester.hpp"
#include "TCPSessionIO.hpp"
......@@ -35,28 +33,22 @@ TCPSessionIO::TCPSessionIO(const QString &hostname, quint16 port)
this, SLOT(sendWaitingRequests()));
QObject::connect(mSocket, SIGNAL(connected()),
this, SIGNAL(connected()));
QObject::connect(mSocket, SIGNAL(disconnected()),
this, SLOT(askReconnect()));
QObject::connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(askReconnect()));
this, SLOT(error()));
QObject::connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SIGNAL(disconnected()));
}
TCPSessionIO::~TCPSessionIO()
{}
void
TCPSessionIO::askReconnect()
TCPSessionIO::error()
{
_debug("TCPSessionIO: Link broken.\n");
int ret = QMessageBox::critical(NULL,
tr("SFLPhone disconnected"),
tr("The link between SFLPhone and SFLPhoned is broken.\n"
"Do you want to try to reconnect?"),
QMessageBox::Retry | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape);
if (ret == QMessageBox::Retry) {
connect();
}
_debug("TCPSessionIO: %s. %d\n",
mSocket->errorString().toStdString().c_str(),
mSocket->state());
mSocket->close();
}
void
......
......@@ -42,6 +42,7 @@ public:
signals:
void connected();
void disconnected();
public slots:
/**
......@@ -57,10 +58,6 @@ public slots:
virtual void send(const QString &request);
virtual void send(const std::string &request);
/**
*/
void askReconnect();
/**
* This function is called when we have
* incomming data on the socket.
......@@ -75,6 +72,13 @@ public slots:
virtual void receive(std::string &answer);
virtual void connect();
private slots:
/**
* This function is called when we have an error
* on the socket.
*/
void error();
private:
QTcpSocket *mSocket;
QString mHostname;
......
......@@ -12,7 +12,8 @@ TCPSessionIOCreator::create()
{
TCPSessionIO *t = new TCPSessionIO(mHostname, mPort);
QObject::connect(t, SIGNAL(connected()),
&PhoneLineManager::instance(), SLOT(startSession()));
t->connect();
&PhoneLineManager::instance(), SIGNAL(connected()));
QObject::connect(t, SIGNAL(disconnected()),
&PhoneLineManager::instance(), SIGNAL(disconnected()));
return t;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment